Files
skills/docs/security-stack/crypto/crypto-cipher/src/primitives/aes-256-gcm/aes-256-gcm.select/aes-256-gcm.select.md
2026-05-29 08:20:56 +01:00

2.7 KiB

aes-256-gcm.select

(no submodules)

Purpose

aes-256-gcm.select.ts is the build-time primitive selector for AES-256-GCM in @suaveplan/crypto-cipher. Generated by bun scripts/gen-crypto-selects.ts, it re-exports one backend — either aes-256-gcm.noble.ts or aes-256-gcm.native.ts — based on the aes-256-gcm field in crypto.config.ts.

Callers import through the stable aes-256-gcm.ts entry point. The selector indirection allows backend changes without touching any caller import paths. Currently the Noble backend is selected because the native stub throws PrimitiveNotImplemented.

Features

  • Re-exports encrypt, decrypt, KEY_LENGTH, NONCE_LENGTH, TAG_LENGTH.
  • Generated — never hand-edited.
  • Controlled by crypto.config.ts: set aes-256-gcm to 'noble' or 'native'.
  • Enables dead-code elimination of the inactive backend.

Usage: Basic Example

import { encrypt, KEY_LENGTH, NONCE_LENGTH } from "@suaveplan/crypto-cipher/aes-256-gcm";

const key = crypto.getRandomValues(new Uint8Array(KEY_LENGTH));
const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH));
const ct = encrypt(key, nonce, new TextEncoder().encode("hello"));
console.log(ct.length); // 16 + 5 = 21 (ciphertext + tag)

Usage: Advanced Example

import { decrypt, NONCE_LENGTH } from "@suaveplan/crypto-cipher/aes-256-gcm";
import { CryptoCipherError } from "@suaveplan/crypto-cipher/errors";

function safeDecrypt(key: Uint8Array, nonce: Uint8Array, ct: Uint8Array): Uint8Array | null {
    try {
        return decrypt(key, nonce, ct);
    } catch (err) {
        if (err instanceof CryptoCipherError) return null;
        throw err;
    }
}

API Reference

Re-exports from the active implementation:

  • encrypt(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad?: Uint8Array): Uint8Array
  • decrypt(key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, aad?: Uint8Array): Uint8Array
  • KEY_LENGTH: 32
  • NONCE_LENGTH: 12
  • TAG_LENGTH: 16

Implementation Notes

This file is a generated one-liner and is marked /* GENERATED — DO NOT EDIT */. The generator resolves the aes-256-gcm backend from crypto.config.ts and emits the corresponding export * line. Running the generator is idempotent. Do not add logic here.

Selection Logic

At build time crypto.config.ts chooses between the Noble @noble/ciphers/aes implementation and the native-engine stub for AES-256-GCM; the selector exported here reads the generated crypto-selects map and dispatches every encrypt / decrypt call to the chosen impl. Nonce is caller-supplied per the package-wide rule — this module does not generate nonces internally.