Add a crypto module

This commit is contained in:
2025-08-25 16:50:48 +03:00
Unverified
parent fbe802fc4e
commit a527bf23cd
7 changed files with 150 additions and 1 deletions
+19
View File
@@ -0,0 +1,19 @@
export interface AesGcmCiphertext {
iv: Uint8Array;
ciphertext: Uint8Array;
}
export async function aesGcmEncrypt(key: CryptoKey, plaintext: Uint8Array): Promise<AesGcmCiphertext> {
const iv = crypto.getRandomValues(new Uint8Array(12));
const ct = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext);
return { iv, ciphertext: new Uint8Array(ct) };
}
export async function aesGcmDecrypt(key: CryptoKey, iv: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
const pt = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, ciphertext);
return new Uint8Array(pt);
}
export async function importAesGcmKey(rawKey: Uint8Array): Promise<CryptoKey> {
return crypto.subtle.importKey("raw", rawKey, { name: "AES-GCM" }, false, ["encrypt", "decrypt"]);
}