diff options
Diffstat (limited to 'client/src/utils/encryption.js')
-rw-r--r-- | client/src/utils/encryption.js | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/client/src/utils/encryption.js b/client/src/utils/encryption.js index e0e6c33..3242422 100644 --- a/client/src/utils/encryption.js +++ b/client/src/utils/encryption.js @@ -1,26 +1,26 @@ async function generateAESKey() { try { const key = await window.crypto.subtle.generateKey( - { - name: "AES-GCM", - length: 256, - }, + { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"], ); return key; } catch (error) { console.error("Error generating AES key:", error); + throw error; } } async function keyToString(key) { try { const exportedKey = await window.crypto.subtle.exportKey("raw", key); - const keyString = btoa(String.fromCharCode(...new Uint8Array(exportedKey))); - return keyString.replace(/\+/g, "-").replace(/\//g, "_"); + return btoa(String.fromCharCode(...new Uint8Array(exportedKey))) + .replace(/\+/g, "-") + .replace(/\//g, "_"); } catch (error) { console.error("Error converting key to string:", error); + throw error; } } @@ -31,17 +31,16 @@ async function stringToKey(keyString) { c.charCodeAt(0), ).buffer; - const key = await window.crypto.subtle.importKey( + return await window.crypto.subtle.importKey( "raw", buffer, { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"], ); - - return key; } catch (error) { console.error("Error converting string to key:", error); + throw error; } } @@ -49,32 +48,28 @@ async function encryptAES(plaintext, key) { try { const iv = window.crypto.getRandomValues(new Uint8Array(12)); const encrypted = await window.crypto.subtle.encrypt( - { - name: "AES-GCM", - iv: iv, - }, + { name: "AES-GCM", iv: iv }, key, new TextEncoder().encode(plaintext), ); return { encrypted, iv }; } catch (error) { console.error("Error encrypting:", error); + throw error; } } async function decryptAES(encrypted, key, iv) { try { const decrypted = await window.crypto.subtle.decrypt( - { - name: "AES-GCM", - iv: iv, - }, + { name: "AES-GCM", iv: iv }, key, encrypted, ); return new TextDecoder().decode(decrypted); } catch (error) { console.error("Error decrypting:", error); + throw error; } } |