fix encrypt utils not found, unable to build

This commit is contained in:
Weixuan Fu
2024-08-04 16:56:46 +08:00
parent 8456ea44bf
commit 2f4d3c26a9
2 changed files with 19 additions and 1 deletions
+1 -1
View File
@@ -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
+18
View File
@@ -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);
}