Encryption key (16/24/32 bytes for AES-128/192/256)
Initialization vector (recommended 12 bytes)
Data to encrypt
Optionalaad: ArrayBuffer | Uint8Array<ArrayBufferLike>Optional additional authenticated data (authenticated but not encrypted)
OptionaltagLength: numberOptional authentication tag length in bytes (default: 16, range: 4-16)
Object containing ciphertext and authentication tag
const crypto = import.meta.use('crypto');
const key = crypto.randomBytes(32); // AES-256
const iv = crypto.randomBytes(12); // 12 bytes IV for GCM
const plaintext = new TextEncoder().encode('Hello, World!');
const aad = new TextEncoder().encode('Additional authenticated data');
// Without AAD
const encrypted1 = crypto.gcmEncrypt(key, iv, plaintext);
// With AAD
const encrypted2 = crypto.gcmEncrypt(key, iv, plaintext, aad);
// With custom tag length
const encrypted3 = crypto.gcmEncrypt(key, iv, plaintext, aad, 12);
Encrypt data using AES-GCM with optional AAD (Additional Authenticated Data)