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: CryptoKey

The AES-GCM CryptoKey to encrypt with.

plaintext: BufferSource

The data to encrypt.

options?: AesGcmOptions

Optional 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

Source