-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
168 lines (156 loc) · 4.28 KB
/
utils.js
File metadata and controls
168 lines (156 loc) · 4.28 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Promisified version of chrome.storage.sync.get.
* @param {string[]|Object} keys - Keys to retrieve.
* @returns {Promise<Object>} - Promise resolving to retrieved items.
*/
function getStorage(keys) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(keys, (items) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(items);
}
});
});
}
/**
* Promisified version of chrome.storage.sync.set.
* @param {Object} items - Items to store.
* @returns {Promise<void>}
*/
function setStorage(items) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set(items, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
});
}
/**
* Retrieves or generates the encryption key used for API keys.
* @returns {Promise<CryptoKey>} - The encryption key.
*/
async function getEncryptionKey() {
const localItems = await getLocalStorage(["encryptionKey"]);
let key = null;
if (localItems.encryptionKey) {
// Key exists, import it
const rawKey = base64ToArrayBuffer(localItems.encryptionKey);
key = await globalThis.crypto.subtle.importKey(
"raw",
rawKey,
{ name: "AES-GCM" },
true,
["encrypt", "decrypt"],
);
} else {
// Generate a new key
key = await globalThis.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"],
);
// Export and store the key
const exportedKey = await globalThis.crypto.subtle.exportKey("raw", key);
const exportedKeyBase64 = arrayBufferToBase64(exportedKey);
await setLocalStorage({ encryptionKey: exportedKeyBase64 });
}
return key;
}
/**
* Decrypts data using the provided encryption key.
* @param {CryptoKey} key - The encryption key.
* @param {string} ivBase64 - The Base64 encoded IV.
* @param {string} ciphertextBase64 - The Base64 encoded ciphertext.
* @returns {Promise<string>} - The decrypted plaintext data.
*/
async function decryptData(key, ivBase64, ciphertextBase64) {
const decoder = new TextDecoder();
const iv = new Uint8Array(base64ToArrayBuffer(ivBase64));
const ciphertext = base64ToArrayBuffer(ciphertextBase64);
try {
const decrypted = await globalThis.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv,
},
key,
ciphertext,
);
return decoder.decode(decrypted);
} catch (e) {
console.error("BabbelSky: Decryption failed:", e);
throw new Error("Failed to decrypt data. Possible data corruption.");
}
}
/**
* Converts an ArrayBuffer to a Base64 string.
* @param {ArrayBuffer} buffer - The buffer to convert.
* @returns {string} - Base64 encoded string.
*/
function arrayBufferToBase64(buffer) {
let binary = "";
const bytes = new Uint8Array(buffer);
bytes.forEach((b) => (binary += String.fromCharCode(b)));
return btoa(binary);
}
/**
* Converts a Base64 string to an ArrayBuffer.
* @param {string} base64 - The Base64 string to convert.
* @returns {ArrayBuffer} - The resulting ArrayBuffer.
*/
function base64ToArrayBuffer(base64) {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
/**
* Promisified version of chrome.storage.local.get.
* @param {string[]|Object} keys - Keys to retrieve.
* @returns {Promise<Object>} - Promise resolving to retrieved items.
*/
function getLocalStorage(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, (items) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(items);
}
});
});
}
/**
* Promisified version of chrome.storage.local.set.
* @param {Object} items - Items to store.
* @returns {Promise<void>}
*/
function setLocalStorage(items) {
return new Promise((resolve, reject) => {
chrome.storage.local.set(items, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
});
}
export {
getStorage,
setStorage,
setLocalStorage,
getEncryptionKey,
decryptData,
arrayBufferToBase64,
};