encryptAesGcm
import { encryptAesGcm } from "jsr:@ende/crypto";function
async function encryptAesGcm(key: CryptoKey, plaintext: BufferSource, options?: AesGcmOptions): Promise<Uint8Array_>Encrypts plaintext using AES-GCM with a random 96-bit nonce.
Returns nonce (12 bytes) || ciphertext || tag (16 bytes).
key: CryptoKeyThe AES-GCM
CryptoKeyto encrypt with.plaintext: BufferSourceThe data to encrypt.
options?: AesGcmOptionsOptional additional authenticated data.
Returns
The concatenated nonce, ciphertext, and authentication tag.
Example: Usage
import { encryptAesGcm } from "@ende/crypto/aes-gcm";
import { assertNotEquals } from "@ende/assert";
const key = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"],
);
const encrypted = await encryptAesGcm(
key,
new TextEncoder().encode("hello world"),
);
assertNotEquals(encrypted.length, 0);
See also
NIST SP 800-38D Section 8.3