-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (244 loc) · 7.94 KB
/
index.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
export class SafeStorage {
// Private instance fields
#mechanism;
#encryptionKey;
#dbName;
#storeName;
#keyInitialized;
// Static field for default storage type
static #defaultType = 'localStorage';
/**
* Static method to initialize the class
* @param {string} type - The type of storage to use ('localStorage' or 'sessionStorage').
*/
static init(type = SafeStorage.#defaultType) {
return new SafeStorage(type);
}
/**
* Constructor for SafeStorage class.
* @param {string} type - The type of storage to use ('localStorage' or 'sessionStorage').
*/
constructor(type = 'localStorage') {
this.#mechanism = type === 'sessionStorage' ? sessionStorage : localStorage;
this.#encryptionKey = null;
this.#dbName = 'client'; // Discreet IndexedDB name
this.#storeName = 'keys'; // Store name within IndexedDB
this.#keyInitialized = false; // Flag to indicate if the key has been initialized
}
/**
* Initializes IndexedDB for storing encryption keys.
* @returns {Promise<IDBDatabase>}
*/
async #initIndexDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.#dbName, 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
db.createObjectStore(this.#storeName);
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
/**
* Stores the encryption key in IndexedDB.
* @param {CryptoKey} key - The encryption key to store.
* @returns {Promise<void>}
*/
async #setKey(key) {
const exportedKey = await crypto.subtle.exportKey('raw', key);
const db = await this.#initIndexDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(this.#storeName, 'readwrite');
const store = tx.objectStore(this.#storeName);
const request = store.put(exportedKey, 'encryptionKey');
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
});
}
/**
* Retrieves the encryption key from IndexedDB.
* @returns {Promise<CryptoKey | null>}
*/
async #getKey() {
const db = await this.#initIndexDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(this.#storeName, 'readonly');
const store = tx.objectStore(this.#storeName);
const request = store.get('encryptionKey');
request.onsuccess = async () => {
if (request.result) {
const importedKey = await crypto.subtle.importKey(
'raw',
request.result,
{ name: 'AES-GCM' },
true,
['encrypt', 'decrypt']
);
resolve(importedKey);
} else {
resolve(null);
}
};
request.onerror = () => reject(request.error);
});
}
/**
* Generates a new AES-GCM encryption key and stores it in IndexedDB.
* @returns {Promise<void>}
*/
async #generateKey() {
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
this.#encryptionKey = key;
await this.#setKey(key);
}
/**
* Initializes the encryption key only if it hasn't been initialized yet.
*/
async #initKey() {
if (!this.#keyInitialized) {
const storedKey = await this.#getKey();
if (storedKey) {
this.#encryptionKey = storedKey;
} else {
await this.#generateKey();
}
this.#keyInitialized = true;
}
}
/**
* Encrypts the given data.
* @param {any} data - The data to encrypt.
* @returns {Promise<object>} The encrypted data with the IV.
*/
async #encrypt(data) {
await this.#initKey();
const iv = crypto.getRandomValues(new Uint8Array(12));
const encodedData = new TextEncoder().encode(JSON.stringify(data));
const encrypted = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: iv },
this.#encryptionKey,
encodedData
);
return {
iv: Array.from(iv),
data: Array.from(new Uint8Array(encrypted))
};
}
/**
* Decrypts the given data.
* @param {object} eData - The encrypted data.
* @returns {Promise<any>} The decrypted data.
*/
async #decrypt(eData) {
await this.#initKey();
const iv = new Uint8Array(eData.iv);
const encrypted = new Uint8Array(eData.data);
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: iv },
this.#encryptionKey,
encrypted
);
return JSON.parse(new TextDecoder().decode(decrypted));
}
/**
* Converts an array of bytes to a Base64 string.
* @param {Uint8Array} bytes - The byte array to encode.
* @returns {string} The Base64 string.
*/
#toBase64(bytes) {
return btoa(String.fromCharCode(...bytes));
}
/**
* Converts a Base64 string to an array of bytes.
* @param {string} base64 - The Base64 string to decode.
* @returns {Uint8Array} The decoded byte array.
*/
#fromBase64(base64) {
return new Uint8Array(atob(base64).split("").map(char => char.charCodeAt(0)));
}
/**
* Encrypts the given key and encodes it as a Base64 string.
* @param {string} key - The key to encrypt.
* @returns {Promise<string>} The encrypted key as a Base64 string.
*/
async #encryptKey(key) {
const encryptedKey = await this.#encrypt(key);
const base64Iv = this.#toBase64(new Uint8Array(encryptedKey.iv));
const base64Data = this.#toBase64(new Uint8Array(encryptedKey.data));
return `${base64Iv}:${base64Data}`;
}
/**
* Decrypts a Base64-encoded encrypted key.
* @param {string} encryptedKey - The Base64-encoded encrypted key.
* @returns {Promise<string>} The decrypted key.
*/
async #decryptKey(encryptedKey) {
const [base64Iv, base64Data] = encryptedKey.split(":");
const iv = this.#fromBase64(base64Iv);
const data = this.#fromBase64(base64Data);
return this.#decrypt({ iv: Array.from(iv), data: Array.from(data) });
}
/**
* Looks for a matching encrypted key by decrypting all stored keys
* and comparing to the incoming cleartext key.
* @param {string} key - The cleartext key.
* @returns {Promise<string|null>} The encrypted key or null if not found.
*/
async #findEncryptedKey(key) {
for (let i = 0; i < this.#mechanism.length; i++) {
const encryptedKey = this.#mechanism.key(i);
const decryptedKey = await this.#decryptKey(encryptedKey);
if (decryptedKey === key) {
return encryptedKey;
}
}
return null;
}
/**
* Set data in storage after checking if the key already exists.
* If it exists, overwrite the existing value; otherwise, create a new entry.
* @param {string} key - The storage key.
* @param {any} value - The data to store.
* @returns {Promise<void>}
*/
async setItem(key, value) {
let encryptedKey = await this.#findEncryptedKey(key);
if (!encryptedKey) {
encryptedKey = await this.#encryptKey(key); // Encrypt the key if it's new
}
const encryptedValue = await this.#encrypt(value);
this.#mechanism.setItem(encryptedKey, JSON.stringify(encryptedValue));
}
/**
* Get and decrypt data from storage by encrypting the key and retrieving the value.
* @param {string} key - The key to retrieve.
* @returns {Promise<any>}
*/
async getItem(key) {
const encryptedKey = await this.#findEncryptedKey(key);
if (!encryptedKey) return null; // If no encrypted key is found, return null
const encryptedValue = JSON.parse(this.#mechanism.getItem(encryptedKey)); // Retrieve the encrypted value
return this.#decrypt(encryptedValue); // Decrypt and return the value
}
/**
* Remove a specific item from storage by encrypting the key first.
* @param {string} key - The key to remove.
*/
async removeItem(key) {
const encryptedKey = await this.#findEncryptedKey(key);
if (encryptedKey) {
this.#mechanism.removeItem(encryptedKey);
}
}
/**
* Clear all storage.
*/
clear() {
this.#mechanism.clear();
}
}