-
-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15004f1
commit 690daff
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// These functions accept and return Uint8Arrays | ||
|
||
export async function encryptAESGCM(plaintext, key) { | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const algorithm = { name: "AES-GCM", iv: iv }; | ||
const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["encrypt"]); | ||
const ciphertext = await crypto.subtle.encrypt(algorithm, keyMaterial, plaintext); | ||
|
||
return new Uint8Array([...iv, ...new Uint8Array(ciphertext)]); | ||
} | ||
|
||
export async function decryptAESGCM(ciphertextArray, key) { | ||
const iv = new Uint8Array(ciphertextArray.slice(0, 12)); | ||
const algorithm = { name: "AES-GCM", iv: iv }; | ||
const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["decrypt"]); | ||
const decrypted = await crypto.subtle.decrypt(algorithm, keyMaterial, new Uint8Array(ciphertextArray.slice(12))); | ||
|
||
return decrypted; | ||
} |