diff --git a/.gitignore b/.gitignore index 3633f9c..13c495c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ pnpm-debug.log* lerna-debug.log* ui/node_modules -ui/src/utils/encryptUtils.ts +# ui/src/utils/encryptUtils.ts db dist-ssr *.local diff --git a/ui/src/utils/encryptUtils.ts b/ui/src/utils/encryptUtils.ts new file mode 100644 index 0000000..f57c855 --- /dev/null +++ b/ui/src/utils/encryptUtils.ts @@ -0,0 +1,18 @@ +// utils/encryptUtils +const defaultSalt = import.meta.env.VITE_SHARE_KEY_ENCODE_KEY; + +export function encodeKey(text: string, salt: string = defaultSalt): string { + const randomLength = Math.floor(Math.random() * 13) + 1; // Random length between 1 and 13 + const randomPadding = Math.random() + .toString(36) + .substring(2, 2 + randomLength); + const input = randomPadding + salt + text; + return btoa(encodeURIComponent(input)); +} + +export function decodeKey(encoded: string, salt: string = defaultSalt): string { + const decoded = decodeURIComponent(atob(encoded)); + const saltIndex = decoded.indexOf(salt); + if (saltIndex === -1) throw new Error("Invalid encoded string or salt"); + return decoded.slice(saltIndex + salt.length); +}