Skip to content

Aes Gcm

1 min read

Import: import system.crypto;

AesGcm

AES-256-GCM authenticated encryption (confidentiality + integrity). Host-backed via the aes-gcm crate natively and Web Crypto / node:crypto in the browser/Node hosts.

public static class AesGcm

nonce_size

Standard 96-bit (12-byte) GCM nonce size.

public static get nonce_size(): int

tag_size

128-bit (16-byte) authentication tag appended to every ciphertext.

public static get tag_size(): int

generate_nonce

Generates a fresh random 12-byte nonce. Never reuse a nonce with the same key.

public static fun generate_nonce(): Result<byte[], CryptoError>

encrypt

Encrypts plaintext under key/nonce, authenticating aad (additional authenticated data, not encrypted but tamper-checked) alongside it. Returns ciphertext with a 16-byte authentication tag appended. nonce must be exactly nonce_size bytes and must never be reused with the same key.

public static fun encrypt(
    key: AesGcmKey,
    nonce: byte[],
    plaintext: byte[],
    aad: byte[]
): Result<byte[], CryptoError>

decrypt

Decrypts and authenticates ciphertext (as produced by encrypt, tag included) under key/nonce/aad. Returns an error if the nonce length is wrong or authentication fails (tampered ciphertext, wrong key/nonce/aad, or truncated input).

public static fun decrypt(
    key: AesGcmKey,
    nonce: byte[],
    ciphertext: byte[],
    aad: byte[]
): Result<byte[], CryptoError>