-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.js
More file actions
61 lines (51 loc) · 1.47 KB
/
decrypt.js
File metadata and controls
61 lines (51 loc) · 1.47 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const keys = {
dev: "PUT_YOUR_DEV_ENCRYPTION_KEY",
qa: "PUT_YOUR_QA_ENCRYPTION_KEY",
prod: "PUT_YOUR_PROD_ENCRYPTION_KEY",
};
let currentKey = keys.dev;
function setEnvironment(env) {
if (keys[env]) {
currentKey = keys[env];
console.log("Switched to environment:", env);
}
}
function decryptPayload(encryptedBase64) {
try {
if (typeof CryptoJS === "undefined") {
console.warn(
"CryptoJS is not loaded. Put crypto-js.js next to decrypt.js and reload the extension.",
);
return encryptedBase64;
}
encryptedBase64 = encryptedBase64
.trim()
.replace(/^"(.*)"$/, "$1")
.replace(/\\\//g, "/")
.replace(/\\n/g, "")
.replace(/\\r/g, "");
const key = CryptoJS.SHA256(currentKey);
const rawData = CryptoJS.enc.Base64.parse(encryptedBase64);
const iv = CryptoJS.lib.WordArray.create(rawData.words.slice(0, 4));
const cipherText = CryptoJS.lib.WordArray.create(
rawData.words.slice(4),
rawData.sigBytes - 16,
);
const decrypted = CryptoJS.AES.decrypt({ ciphertext: cipherText }, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
const text = decrypted.toString(CryptoJS.enc.Utf8);
try {
return JSON.parse(text);
} catch {
return text;
}
} catch (e) {
console.warn("decryptPayload error:", e);
return encryptedBase64;
}
}
window.decryptPayload = decryptPayload;
window.setEnvironment = setEnvironment;