forked from netease-lcap/CodeWaveSummerCompetition2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
44 lines (43 loc) · 1.26 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import CryptoJS from "crypto-js";
import config from "./config.js";
export default {
isSupportStorage: () => {
return typeof Storage !== "undefined" ? true : false;
},
// 名称前自动添加前缀
autoAddPrefix: (key) => {
const prefix = config.prefix ? config.prefix + "_" : "";
if (key.startsWith(prefix)) {
return key;
} else {
return prefix + key;
}
},
encrypt: (data) => {
if (typeof data === "object") {
try {
data = JSON.stringify(data);
} catch (error) {
console.log("encrypt error:", error);
}
}
const dataHex = CryptoJS.enc.Utf8.parse(data);
const encrypted = CryptoJS.AES.encrypt(dataHex, config.SECRET_KEY, {
iv: config.SECRET_IV,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.ciphertext.toString();
},
decrypt: (data) => {
const encryptedHexStr = CryptoJS.enc.Hex.parse(data);
const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
const decrypt = CryptoJS.AES.decrypt(str, config.SECRET_KEY, {
iv: config.SECRET_IV,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
},
};