Skip to content
VaelJS Blog

vael:crypto

Symmetric AES-256 encryption, SHA-2 hashes/HMACs, PBKDF2, bcrypt, and Shamir secret sharing.

Available in the vjs (vaeljs) runtime with both synchronous APIs and worker-pool *Async APIs. In vjs-lite, only the synchronous exports are available (hashAsync, hmacAsync, pbkdf2Async, bcryptHashAsync, bcryptVerifyAsync, shamirSplitAsync, and shamirCombineAsync are absent).

import ... from "vael:crypto"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-crypto (default) in vaeljs-lite-core
Seven *Async worker-pool helpers (hashAsync, hmacAsync, pbkdf2Async, bcryptHashAsync, bcryptVerifyAsync, shamirSplitAsync, shamirCombineAsync) are V8-only (vjs). Sync exports (including BLAKE2 and BLAKE3) run on both vjs and vjs-lite.
import { createHash, decrypt, encrypt, generateEnvelopeKey } from "vael:crypto";
const key = generateEnvelopeKey();
const envelope = encrypt(key, new Uint8Array([1, 2, 3, 4]));
const plain = decrypt(key, envelope);
const digest = createHash("sha256").update("hello").digest("hex");
console.log(plain.length, digest);
API Symbols
Functions (25)
generateEnvelopeKeyv0.0.0-alpha.0
Generate a 32-byte AES-256 key from the operating system CSPRNG.
randomBytesv0.0.0-alpha.0
Synchronously read length bytes (0..=8_388_608) from the OS CSPRNG.
encryptv0.0.0-alpha.0
Synchronously encrypt plaintext (max 8 MiB) into a versioned NEO1 AES-256-GCM envelope with a fresh random 96-bit nonce.
decryptv0.0.0-alpha.0
Synchronously verify and decrypt a NEO1 AES-256-GCM envelope.
createEncryptorv0.0.0-alpha.0
Create a bounded stateful encryptor for "aes-256-cbc" or "aes-256-gcm".
createDecryptorv0.0.0-alpha.0
Create a bounded stateful decryptor for "aes-256-cbc" or "aes-256-gcm".
createHashv0.0.0-alpha.0
Create a synchronous incremental SHA-2 hash ("sha256", "sha384", or "sha512").
createHmacv0.0.0-alpha.0
Create a synchronous incremental HMAC ("sha256", "sha384", or "sha512").
hashAsyncvjs onlyv0.0.0-alpha.0
Compute a one-shot SHA-2 digest on the bounded worker pool (max 64 pending jobs / 64 MiB total across async crypto workers; input max 16...
hmacAsyncvjs onlyv0.0.0-alpha.0
Compute a one-shot HMAC digest on the bounded worker pool (max 64 pending jobs / 64 MiB total).
pbkdf2v0.0.0-alpha.0
Synchronously derive key material using PBKDF2-HMAC-SHA2.
pbkdf2Asyncvjs onlyv0.0.0-alpha.0
Derive key material using PBKDF2-HMAC-SHA2 on the bounded worker pool.
bcryptHashv0.0.0-alpha.0
Synchronously hash a password with bcrypt, returning a 60-character $2b$ modular-crypt string.
bcryptHashAsyncvjs onlyv0.0.0-alpha.0
Hash a password with bcrypt on the bounded worker pool ($2b$ format, max 72 bytes, cost in 4..=16, default 10).
bcryptVerifyv0.0.0-alpha.0
Synchronously verify password against a $2a$/$2b$/$2y$ bcrypt hash in constant time.
bcryptVerifyAsyncvjs onlyv0.0.0-alpha.0
Verify password against a bcrypt hash on the bounded worker pool.
shamirSplitv0.0.0-alpha.0
Synchronously split secret into shareCount GF(256) Shamir shares requiring threshold shares to reconstruct.
shamirSplitAsyncvjs onlyv0.0.0-alpha.0
Split secret into GF(256) Shamir shares on the bounded worker pool.
shamirCombinev0.0.0-alpha.0
Synchronously reconstruct a secret from GF(256) Shamir shares.
shamirCombineAsyncvjs onlyv0.0.0-alpha.0
Reconstruct a secret from GF(256) Shamir shares on the bounded worker pool.
blake3v0.0.0-alpha.2
Compute a synchronous BLAKE3 digest or extendable output (XOF) of data.
blake3Keyedv0.0.0-alpha.2
Compute a synchronous keyed BLAKE3 MAC/PRF over data using a 32-byte key.
blake3DeriveKeyv0.0.0-alpha.2
Synchronously derive key material from keyMaterial using BLAKE3 key derivation with domain-separation context.
blake2bv0.0.0-alpha.2
Compute a synchronous BLAKE2b digest or keyed MAC over data.
blake2sv0.0.0-alpha.2
Compute a synchronous BLAKE2s digest or keyed MAC over data.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function generateEnvelopeKey(): Uint8Array;

Generate a 32-byte AES-256 key from the operating system CSPRNG.

Retire keys well before 2^32 encryptions under AES-GCM.

Returns
Uint8Array — A fresh 32-byte random Uint8Array key.
Exceptions
  • TypeError — when any argument is supplied.
  • Error — with code = "VAEL_CRYPTO_RANDOM" when the CSPRNG fails.
import { generateEnvelopeKey } from "vael:crypto";
const key = generateEnvelopeKey();
console.log(key.byteLength); // 32
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function randomBytes(length: number): Uint8Array;

Synchronously read length bytes (0..=8_388_608) from the OS CSPRNG.

Parameters
NameTypeDescription
lengthnumberNon-negative integer up to 8 MiB (8_388_608).
Returns
Uint8Array — Random bytes of length length.
Exceptions
  • TypeError — when length is not a number or arity is wrong.
  • RangeError — when length is not an integer in 0..=8_388_608.
  • Error — with code = "VAEL_CRYPTO_RANDOM" when the CSPRNG fails.
import { randomBytes } from "vael:crypto";
const nonce = randomBytes(12);
console.log(nonce.byteLength); // 12
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function encrypt(
  key: Uint8Array,
  plaintext: Uint8Array,
  associatedData?: Uint8Array,
): Uint8Array;

Synchronously encrypt plaintext (max 8 MiB) into a versioned NEO1 AES-256-GCM envelope with a fresh random 96-bit nonce.

Parameters
NameTypeDescription
keyUint8ArrayExactly 32 bytes.
plaintextUint8ArrayUp to 8 MiB (8_388_608 bytes).
associatedData optionalUint8ArrayOptional authenticated non-encrypted bytes (max 8 MiB).
Returns
Uint8Array — Versioned NEO1 authenticated envelope bytes.
Exceptions
  • TypeError — on invalid argument types or arity.
  • Error — with code "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_RANDOM", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
import { decrypt, encrypt, generateEnvelopeKey } from "vael:crypto";
const key = generateEnvelopeKey();
const aad = new Uint8Array([1, 2]);
const sealed = encrypt(key, new Uint8Array([10, 20, 30]), aad);
console.log(decrypt(key, sealed, aad));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function decrypt(
  key: Uint8Array,
  envelope: Uint8Array,
  associatedData?: Uint8Array,
): Uint8Array;

Synchronously verify and decrypt a NEO1 AES-256-GCM envelope.

Returns no plaintext when tag verification fails.

Parameters
NameTypeDescription
keyUint8ArrayExactly 32 bytes.
envelopeUint8ArrayUp to 8 MiB + 34 bytes.
associatedData optionalUint8ArrayMust match the value passed to encrypt.
Returns
Uint8Array — Authenticated plaintext bytes.
Exceptions
  • TypeError — on invalid argument types or arity.
  • Error — with code "VAEL_CRYPTO_AUTH", "VAEL_CRYPTO_FORMAT", "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createEncryptor(
  algorithm: CipherAlgorithm,
  key: Uint8Array,
  iv: Uint8Array,
  options?: CipherOptions,
): Cipher;

Create a bounded stateful encryptor for "aes-256-cbc" or "aes-256-gcm".

Parameters
NameTypeDescription
algorithmCipherAlgorithm"aes-256-cbc" or "aes-256-gcm" (case-insensitive).
keyUint8ArrayExactly 32 bytes.
ivUint8Array16 bytes for CBC, 12 bytes for GCM.
options optionalCipherOptionsCBC only ({ padding?: boolean }).
Returns
Cipher — Stateful Cipher encryptor instance; call close() in finally.
Exceptions
  • TypeError — on invalid arguments or unsupported options.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_IV", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_TOO_MANY_OBJECTS".
import { createDecryptor, createEncryptor, randomBytes } from "vael:crypto";
const key = randomBytes(32);
const iv = randomBytes(12);
const enc = createEncryptor("aes-256-gcm", key, iv);
try {
enc.update("secret");
const ct = enc.final();
const tag = enc.getAuthTag();
const dec = createDecryptor("aes-256-gcm", key, iv);
try {
dec.setAuthTag(tag);
dec.update(ct);
console.log(dec.final().byteLength);
} finally {
dec.close();
}
} finally {
enc.close();
}
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createDecryptor(
  algorithm: CipherAlgorithm,
  key: Uint8Array,
  iv: Uint8Array,
  options?: CipherOptions,
): Cipher;

Create a bounded stateful decryptor for "aes-256-cbc" or "aes-256-gcm".

GCM decryptors must call setAuthTag(tag) before final().

Parameters
NameTypeDescription
algorithmCipherAlgorithm"aes-256-cbc" or "aes-256-gcm" (case-insensitive).
keyUint8ArrayExactly 32 bytes.
ivUint8Array16 bytes for CBC, 12 bytes for GCM.
options optionalCipherOptionsCBC only ({ padding?: boolean }).
Returns
Cipher — Stateful Cipher decryptor instance; call close() in finally.
Exceptions
  • TypeError — on invalid arguments or unsupported options.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_IV", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_TOO_MANY_OBJECTS".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createHash(algorithm: HashAlgorithm): Hash;

Create a synchronous incremental SHA-2 hash ("sha256", "sha384", or "sha512").

Parameters
NameTypeDescription
algorithmHashAlgorithmSHA-2 algorithm name ("sha256", "sha384", or "sha512").
Returns
Hash — Incremental Hash instance.
Exceptions
  • TypeError — on wrong argument type or arity.
  • Error — with code "VAEL_CRYPTO_ALGORITHM" or "VAEL_CRYPTO_TOO_MANY_OBJECTS".
import { createHash } from "vael:crypto";
const hex = createHash("sha256").update("vael").digest("hex");
console.log(hex);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createHmac(algorithm: HashAlgorithm, key: Uint8Array | string): Hash;

Create a synchronous incremental HMAC ("sha256", "sha384", or "sha512").

Parameters
NameTypeDescription
algorithmHashAlgorithmSHA-2 algorithm name ("sha256", "sha384", or "sha512").
keyUint8Array | stringKey bytes or UTF-8 string (max 1 MiB).
Returns
Hash — Incremental Hash HMAC instance.
Exceptions
  • TypeError — on wrong argument types or arity.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_TOO_MANY_OBJECTS", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
import { createHmac } from "vael:crypto";
const mac = createHmac("sha256", "secret-key").update("payload").digest("hex");
console.log(mac);
functionSince v0.0.0-alpha.0vjs only
export function hashAsync(algorithm: HashAlgorithm, data: Uint8Array | string): Promise<Uint8Array>;
export function hashAsync(
  algorithm: HashAlgorithm,
  data: Uint8Array | string,
  encoding: DigestEncoding,
): Promise<string>;

Compute a one-shot SHA-2 digest on the bounded worker pool (max 64 pending jobs / 64 MiB total across async crypto workers; input max 16 MiB).

Rejects with VAEL_CRYPTO_BUSY if the worker budget is exhausted.

Compute a one-shot encoded SHA-2 digest string on the bounded worker pool.

Parameters
NameTypeDescription
algorithmHashAlgorithmSHA-2 algorithm name ("sha256", "sha384", or "sha512").
dataUint8Array | stringInput bytes or UTF-8 string (max 16 MiB).
encoding optionalDigestEncodingOutput digest encoding ("hex", "base64", "base64url", "latin1", or "binary").
Returns
  • Promise<Uint8Array> — Promise resolving to raw digest bytes.
  • Promise<string> — Promise resolving to the encoded digest string.
Exceptions
  • TypeError — on invalid arguments or unsupported encoding.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
import { hashAsync } from "vael:crypto";
const hex = await hashAsync("sha256", "hello", "hex");
console.log(hex);
functionSince v0.0.0-alpha.0vjs only
export function hmacAsync(
  algorithm: HashAlgorithm,
  key: Uint8Array | string,
  data: Uint8Array | string,
): Promise<Uint8Array>;
export function hmacAsync(
  algorithm: HashAlgorithm,
  key: Uint8Array | string,
  data: Uint8Array | string,
  encoding: DigestEncoding,
): Promise<string>;

Compute a one-shot HMAC digest on the bounded worker pool (max 64 pending jobs / 64 MiB total).

Compute a one-shot encoded HMAC digest string on the bounded worker pool.

Parameters
NameTypeDescription
algorithmHashAlgorithmSHA-2 algorithm name ("sha256", "sha384", or "sha512").
keyUint8Array | stringKey bytes or UTF-8 string (max 1 MiB).
dataUint8Array | stringInput bytes or UTF-8 string (max 16 MiB).
encoding optionalDigestEncodingOutput digest encoding.
Returns
  • Promise<Uint8Array> — Promise resolving to raw HMAC digest bytes.
  • Promise<string> — Promise resolving to the encoded HMAC digest string.
Exceptions
  • TypeError — on invalid arguments or unsupported encoding.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function pbkdf2(
  algorithm: HashAlgorithm,
  password: Uint8Array | string,
  salt: Uint8Array | string,
  iterations: number,
  length: number,
): Uint8Array;

Synchronously derive key material using PBKDF2-HMAC-SHA2.

Parameters
NameTypeDescription
algorithmHashAlgorithm"sha256", "sha384", or "sha512" (case-insensitive).
passwordUint8Array | stringBytes or UTF-8 string (max 1 MiB).
saltUint8Array | stringBytes or UTF-8 string (max 1 MiB).
iterationsnumberInteger in 1..=1_000_000.
lengthnumberOutput byte length in 1..=1024.
Returns
Uint8Array — Derived key bytes of length length.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when iterations or length is outside its bounds.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
import { pbkdf2, randomBytes } from "vael:crypto";
const salt = randomBytes(16);
const derived = pbkdf2("sha256", "passphrase", salt, 100_000, 32);
console.log(derived.byteLength); // 32
functionSince v0.0.0-alpha.0vjs only
export function pbkdf2Async(
  algorithm: HashAlgorithm,
  password: Uint8Array | string,
  salt: Uint8Array | string,
  iterations: number,
  length: number,
): Promise<Uint8Array>;

Derive key material using PBKDF2-HMAC-SHA2 on the bounded worker pool.

Shares the 64-job / 64 MiB pending async crypto budget (VAEL_CRYPTO_BUSY).

Parameters
NameTypeDescription
algorithmHashAlgorithm"sha256", "sha384", or "sha512" (case-insensitive).
passwordUint8Array | stringBytes or UTF-8 string (max 1 MiB).
saltUint8Array | stringBytes or UTF-8 string (max 1 MiB).
iterationsnumberInteger in 1..=1_000_000.
lengthnumberOutput byte length in 1..=1024.
Returns
Promise<Uint8Array> — Promise resolving to derived key bytes of length length.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when iterations or length is outside its bounds.
  • Error — with code "VAEL_CRYPTO_ALGORITHM", "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function bcryptHash(password: Uint8Array | string, cost?: number): string;

Synchronously hash a password with bcrypt, returning a 60-character $2b$ modular-crypt string.

Parameters
NameTypeDescription
passwordUint8Array | stringBytes or UTF-8 string (max 72 bytes; never truncated).
cost optionalnumberLogarithmic work factor integer in 4..=16 (default: 10).
Returns
string — 60-character $2b$ bcrypt hash string.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when cost is not an integer in 4..=16.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_RANDOM", or "VAEL_CRYPTO_DETACHED".
import { bcryptHash, bcryptVerify } from "vael:crypto";
const hash = bcryptHash("correct horse battery staple", 4);
console.log(bcryptVerify("correct horse battery staple", hash)); // true
functionSince v0.0.0-alpha.0vjs only
export function bcryptHashAsync(password: Uint8Array | string, cost?: number): Promise<string>;

Hash a password with bcrypt on the bounded worker pool ($2b$ format, max 72 bytes, cost in 4..=16, default 10).

Parameters
NameTypeDescription
passwordUint8Array | stringBytes or UTF-8 string (max 72 bytes; never truncated).
cost optionalnumberLogarithmic work factor integer in 4..=16 (default: 10).
Returns
Promise<string> — Promise resolving to a 60-character $2b$ bcrypt hash string.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when cost is not an integer in 4..=16.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_RANDOM", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function bcryptVerify(password: Uint8Array | string, hash: string): boolean;

Synchronously verify password against a $2a$/$2b$/$2y$ bcrypt hash in constant time.

Passwords over 72 bytes and malformed or over-cost (> 16) hashes return false.

Parameters
NameTypeDescription
passwordUint8Array | stringCandidate password bytes or UTF-8 string.
hashstringModular-crypt bcrypt hash string ($2a$, $2b$, or $2y$).
Returns
boolean — true if password matches hash.
Exceptions
  • TypeError — on wrong argument types or arity.
  • Error — with code = "VAEL_CRYPTO_DETACHED" when a detached buffer is supplied.
functionSince v0.0.0-alpha.0vjs only
export function bcryptVerifyAsync(password: Uint8Array | string, hash: string): Promise<boolean>;

Verify password against a bcrypt hash on the bounded worker pool.

Parameters
NameTypeDescription
passwordUint8Array | stringCandidate password bytes or UTF-8 string.
hashstringModular-crypt bcrypt hash string.
Returns
Promise<boolean> — Promise resolving to true if password matches hash.
Exceptions
  • TypeError — on wrong argument types or arity.
  • Error — with code "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function shamirSplit(
  secret: Uint8Array,
  threshold: number,
  shareCount: number,
): ShamirShare[];

Synchronously split secret into shareCount GF(256) Shamir shares requiring threshold shares to reconstruct.

Shares are not authenticated. Limits: secret 1..=8 MiB, 2 <= threshold <= shareCount <= 255, secret.length * shareCount <= 64 MiB. Rejects SharedArrayBuffer-backed views with VAEL_CRYPTO_SHAMIR_SHARED.

Parameters
NameTypeDescription
secretUint8ArrayNon-empty secret byte slice (1..=8 MiB).
thresholdnumberMinimum shares needed for reconstruction (2..=shareCount).
shareCountnumberTotal number of shares to produce (threshold..=255).
Returns
ShamirShare[] — Array of shareCount frozen { index, value } share records.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when threshold or shareCount is out of range.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_RANDOM", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_SHAMIR_SHARED".
import { shamirCombine, shamirSplit } from "vael:crypto";
const shares = shamirSplit(new Uint8Array([1, 2, 3, 4]), 2, 3);
const recovered = shamirCombine([shares[0], shares[2]]);
console.log(recovered);
functionSince v0.0.0-alpha.0vjs only
export function shamirSplitAsync(
  secret: Uint8Array,
  threshold: number,
  shareCount: number,
): Promise<ShamirShare[]>;

Split secret into GF(256) Shamir shares on the bounded worker pool.

Parameters
NameTypeDescription
secretUint8ArrayNon-empty secret byte slice (1..=8 MiB).
thresholdnumberMinimum shares needed for reconstruction (2..=shareCount).
shareCountnumberTotal number of shares to produce (threshold..=255).
Returns
Promise<ShamirShare[]> — Promise resolving to shareCount frozen { index, value } share records.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when threshold or shareCount is out of range.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_RANDOM", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_SHAMIR_SHARED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function shamirCombine(shares: readonly ShamirShare[], threshold?: number): Uint8Array;

Synchronously reconstruct a secret from GF(256) Shamir shares.

Shares are not authenticated. Limits: 2..=255 shares with identical non-zero length (max 8 MiB per share, 64 MiB total).

Parameters
NameTypeDescription
sharesreadonly ShamirShare[]At least 2 valid ShamirShare records with identical non-zero byte lengths.
threshold optionalnumberOptional minimum share count (2..=255, <= shares.length).
Returns
Uint8Array — Reconstructed secret bytes.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when threshold or share index is out of range.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_SHAMIR_DUPLICATE", "VAEL_CRYPTO_SHAMIR_LENGTH", or "VAEL_CRYPTO_SHAMIR_SHARED".
functionSince v0.0.0-alpha.0vjs only
export function shamirCombineAsync(
  shares: readonly ShamirShare[],
  threshold?: number,
): Promise<Uint8Array>;

Reconstruct a secret from GF(256) Shamir shares on the bounded worker pool.

Parameters
NameTypeDescription
sharesreadonly ShamirShare[]At least 2 valid ShamirShare records with identical non-zero byte lengths.
threshold optionalnumberOptional minimum share count (2..=255, <= shares.length).
Returns
Promise<Uint8Array> — Promise resolving to reconstructed secret bytes.
Exceptions
  • TypeError — on wrong argument types or arity.
  • RangeError — when threshold or share index is out of range.
  • Error — with code "VAEL_CRYPTO_LIMIT", "VAEL_CRYPTO_DETACHED", "VAEL_CRYPTO_SHAMIR_DUPLICATE", "VAEL_CRYPTO_SHAMIR_LENGTH", "VAEL_CRYPTO_SHAMIR_SHARED", "VAEL_CRYPTO_BUSY", or "VAEL_CRYPTO_NO_EXECUTOR".
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function blake3(data: Uint8Array | string, options?: Blake3Options): Uint8Array;

Compute a synchronous BLAKE3 digest or extendable output (XOF) of data.

Parameters
NameTypeDescription
dataUint8Array | stringInput bytes or UTF-8 string.
options optionalBlake3OptionsOptional XOF output length (1..=65_536, default 32).
Returns
Uint8Array — Digest bytes of length options?.outputLength ?? 32.
Exceptions
  • TypeError — on invalid argument types or arity.
  • RangeError — when outputLength is outside 1..=65_536.
  • Error — with code "VAEL_CRYPTO_DETACHED" or "VAEL_CRYPTO_LIMIT".
import { blake3 } from "vael:crypto";
const digest = blake3("hello");
const xof = blake3("hello", { outputLength: 64 });
console.log(digest.byteLength, xof.byteLength);
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function blake3Keyed(
  key: Uint8Array,
  data: Uint8Array | string,
  options?: Blake3Options,
): Uint8Array;

Compute a synchronous keyed BLAKE3 MAC/PRF over data using a 32-byte key.

Parameters
NameTypeDescription
keyUint8ArrayExactly 32 key bytes.
dataUint8Array | stringInput bytes or UTF-8 string.
options optionalBlake3OptionsOptional XOF output length (1..=65_536, default 32).
Returns
Uint8Array — Keyed digest bytes of length options?.outputLength ?? 32.
Exceptions
  • TypeError — on invalid argument types or arity.
  • RangeError — when outputLength is outside 1..=65_536.
  • Error — with code "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_LIMIT".
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function blake3DeriveKey(
  context: string,
  keyMaterial: Uint8Array | string,
  options?: Blake3Options,
): Uint8Array;

Synchronously derive key material from keyMaterial using BLAKE3 key derivation with domain-separation context.

Parameters
NameTypeDescription
contextstringDomain-separation context string.
keyMaterialUint8Array | stringInput key material bytes or UTF-8 string.
options optionalBlake3OptionsOptional XOF output length (1..=65_536, default 32).
Returns
Uint8Array — Derived key bytes of length options?.outputLength ?? 32.
Exceptions
  • TypeError — on invalid argument types or arity.
  • RangeError — when outputLength is outside 1..=65_536.
  • Error — with code "VAEL_CRYPTO_DETACHED" or "VAEL_CRYPTO_LIMIT".
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function blake2b(data: Uint8Array | string, options?: Blake2Options): Uint8Array;

Compute a synchronous BLAKE2b digest or keyed MAC over data.

Parameters
NameTypeDescription
dataUint8Array | stringInput bytes or UTF-8 string.
options optionalBlake2OptionsOptional outputLength (1..=64, default 64) and key (0..=64 bytes).
Returns
Uint8Array — Digest bytes of length options?.outputLength ?? 64.
Exceptions
  • TypeError — on invalid argument types or arity.
  • RangeError — when outputLength is outside 1..=64.
  • Error — with code "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_LIMIT".
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function blake2s(data: Uint8Array | string, options?: Blake2Options): Uint8Array;

Compute a synchronous BLAKE2s digest or keyed MAC over data.

Parameters
NameTypeDescription
dataUint8Array | stringInput bytes or UTF-8 string.
options optionalBlake2OptionsOptional outputLength (1..=32, default 32) and key (0..=32 bytes).
Returns
Uint8Array — Digest bytes of length options?.outputLength ?? 32.
Exceptions
  • TypeError — on invalid argument types or arity.
  • RangeError — when outputLength is outside 1..=32.
  • Error — with code "VAEL_CRYPTO_KEY", "VAEL_CRYPTO_DETACHED", or "VAEL_CRYPTO_LIMIT".
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface CipherOptions {
  padding?: boolean;
}

Options accepted by createEncryptor and createDecryptor (aes-256-cbc only; aes-256-gcm accepts no options).

Properties
PropertyTypeDescriptionSince
padding optionalbooleanEnable or disable PKCS#7 padding for aes-256-cbc (default: true).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface Blake3Options {
  outputLength?: number;
}

Options accepted by blake3, blake3Keyed, and blake3DeriveKey.

Properties
PropertyTypeDescriptionSince
outputLength optionalnumberOutput XOF digest length in bytes (1..=65_536, default: 32).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface Blake2Options {
  outputLength?: number;
  key?: Uint8Array;
}

Options accepted by blake2b and blake2s.

Properties
PropertyTypeDescriptionSince
outputLength optionalnumberOutput digest length in bytes (1..=64 for blake2b, default 64; 1..=32 for blake2s, default 32).v0.0.0-alpha.2
key optionalUint8ArrayOptional MAC key bytes (0..=64 for blake2b, 0..=32 for blake2s; an empty Uint8Array behaves as unkeyed per RFC 7693 Section 2.5).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface ShamirShare {
  readonly index: number;
  readonly value: Uint8Array;
}

Frozen share record produced by shamirSplit / shamirSplitAsync and consumed by shamirCombine / shamirCombineAsync.

Properties
PropertyTypeDescriptionSince
index readonlynumberOne-based GF(256) share index (1..=255).v0.0.0-alpha.0
value readonlyUint8ArrayShare bytes (same length as the secret; record property is frozen, array elements remain mutable).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface Cipher {
  update(data: Uint8Array | string, encoding?: InputEncoding): Uint8Array;
  final(): Uint8Array;
  setAAD(aad: Uint8Array): this;
  setAuthTag(tag: Uint8Array): this;
  getAuthTag(): Uint8Array;
  setAutoPadding(enabled: boolean): this;
  close(): void;
  dispose(): void;
}

Bounded, stateful synchronous AES-256-CBC or AES-256-GCM cipher returned by createEncryptor or createDecryptor.

Call close() or dispose() in a finally block (even after final()) to stay within the per-runtime open-object (64) and buffered-byte (64 MiB) caps.

Methods
methodSince v0.0.0-alpha.0
update(data: Uint8Array | string, encoding?: InputEncoding): Uint8Array;

Accumulate bytes or a UTF-8 string into the cipher (at most 8 MiB per call). Always returns an empty Uint8Array; output is produced by final().

Parameters
NameTypeDescription
dataUint8Array | stringInput bytes or UTF-8 string (max 8 MiB per call).
encoding optionalInputEncodingOptional string input encoding ("utf8" / "utf-8" when data is a string).
Returns
Uint8Array — Always an empty Uint8Array; finalized bytes are returned by final().
Exceptions
  • TypeError — on wrong argument types or supplying an encoding with binary input.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
methodSince v0.0.0-alpha.0
final(): Uint8Array;

Perform the cipher operation over all buffered input and finalize the cipher. For GCM decryption, verifies the authentication tag before returning any plaintext.

Returns
Uint8Array — Finalized ciphertext or authenticated plaintext bytes.
Exceptions
  • TypeError — when any argument is supplied.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", "VAEL_CRYPTO_AUTH", "VAEL_CRYPTO_PADDING", "VAEL_CRYPTO_TAG", or "VAEL_CRYPTO_LIMIT".
methodSince v0.0.0-alpha.0
setAAD(aad: Uint8Array): this;

Set GCM associated data (aes-256-gcm only, at most once before update or final; max 8 MiB).

Parameters
NameTypeDescription
aadUint8ArrayAssociated authenticated data bytes (max 8 MiB).
Returns
this — this for chaining.
Exceptions
  • TypeError — when aad is not a Uint8Array or arity is wrong.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", "VAEL_CRYPTO_UNSUPPORTED", or "VAEL_CRYPTO_LIMIT".
methodSince v0.0.0-alpha.0
setAuthTag(tag: Uint8Array): this;

Set the expected 16-byte GCM authentication tag before final() (aes-256-gcm decryptors only).

Parameters
NameTypeDescription
tagUint8ArrayExpected 16-byte GCM authentication tag.
Returns
this — this for chaining.
Exceptions
  • TypeError — when tag is not a Uint8Array or arity is wrong.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", "VAEL_CRYPTO_UNSUPPORTED", or "VAEL_CRYPTO_TAG".
methodSince v0.0.0-alpha.0
getAuthTag(): Uint8Array;

Return the 16-byte GCM authentication tag produced by final() (aes-256-gcm encryptors only, after final()).

Returns
Uint8Array — The 16-byte GCM authentication tag.
Exceptions
  • TypeError — when any argument is supplied.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", or "VAEL_CRYPTO_UNSUPPORTED".
methodSince v0.0.0-alpha.0
setAutoPadding(enabled: boolean): this;

Enable or disable CBC PKCS#7 padding before final() (aes-256-cbc only).

Parameters
NameTypeDescription
enabledbooleanWhether PKCS#7 padding is enabled (true by default).
Returns
this — this for chaining.
Exceptions
  • TypeError — when enabled is not a boolean or arity is wrong.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_STATE", or "VAEL_CRYPTO_UNSUPPORTED".
methodSince v0.0.0-alpha.0
close(): void;

Release this cipher’s native slot and buffered bytes. Idempotent. Later calls throw VAEL_CRYPTO_CLOSED.

Returns
void
methodSince v0.0.0-alpha.0
dispose(): void;

Alias for close(). Idempotent.

Returns
void
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface Hash {
  update(data: Uint8Array | string, encoding?: InputEncoding): this;
  digest(): Uint8Array;
  digest(encoding: DigestEncoding): string;
  close(): void;
  dispose(): void;
}

Synchronous incremental SHA-2 hash or HMAC returned by createHash or createHmac.

Open digest objects are capped at 1024 per runtime; call close() or dispose() if not calling digest().

Methods
methodSince v0.0.0-alpha.0
update(data: Uint8Array | string, encoding?: InputEncoding): this;

Add bytes or a UTF-8 string (max 16 MiB per update).

Parameters
NameTypeDescription
dataUint8Array | stringInput bytes or UTF-8 string (max 16 MiB).
encoding optionalInputEncodingOptional string encoding ("utf8" / "utf-8" when data is a string).
Returns
this — this for chaining.
Exceptions
  • TypeError — on wrong argument types or supplying an encoding with binary input.
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_DIGESTED", "VAEL_CRYPTO_LIMIT", or "VAEL_CRYPTO_DETACHED".
methodSince v0.0.0-alpha.0
digest(): Uint8Array;
digest(encoding: DigestEncoding): string;

Consume the hash state and return raw digest bytes.

Consume the hash state and return the digest encoded as a string.

Parameters
NameTypeDescription
encoding optionalDigestEncodingOutput digest encoding ("hex", "base64", "base64url", "latin1", or "binary").
Returns
  • Uint8Array — Raw digest bytes.
  • string — Encoded digest string.
Exceptions
  • Error — with code "VAEL_CRYPTO_CLOSED", "VAEL_CRYPTO_DIGESTED", or "VAEL_CRYPTO_LIMIT".
  • TypeError — on unsupported encoding (checked before consuming state).
methodSince v0.0.0-alpha.0
close(): void;

Release native state without producing a digest. Idempotent.

Returns
void
methodSince v0.0.0-alpha.0
dispose(): void;

Alias for close(). Idempotent.

Returns
void
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type HashAlgorithm = "sha256" | "sha384" | "sha512" | (string & {});

Supported SHA-2 hash and HMAC algorithms (case-insensitive).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type CipherAlgorithm = "aes-256-cbc" | "aes-256-gcm" | (string & {});

Supported symmetric cipher algorithms (case-insensitive).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type InputEncoding = "utf8" | "utf-8" | (string & {});

Supported string input encoding (UTF-8 only, case-insensitive).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type DigestEncoding = "hex" | "base64" | "base64url" | "latin1" | "binary" | (string & {});

Supported digest output encodings (case-insensitive).

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type CryptoErrorCode =
  | "VAEL_CRYPTO_KEY"
  | "VAEL_CRYPTO_FORMAT"
  | "VAEL_CRYPTO_AUTH"
  | "VAEL_CRYPTO_RANDOM"
  | "VAEL_CRYPTO_LIMIT"
  | "VAEL_CRYPTO_DETACHED"
  | "VAEL_CRYPTO_ALGORITHM"
  | "VAEL_CRYPTO_IV"
  | "VAEL_CRYPTO_TAG"
  | "VAEL_CRYPTO_PADDING"
  | "VAEL_CRYPTO_STATE"
  | "VAEL_CRYPTO_CLOSED"
  | "VAEL_CRYPTO_UNSUPPORTED"
  | "VAEL_CRYPTO_TOO_MANY_OBJECTS"
  | "VAEL_CRYPTO_ENCODING"
  | "VAEL_CRYPTO_DIGESTED"
  | "VAEL_CRYPTO_BUSY"
  | "VAEL_CRYPTO_NO_EXECUTOR"
  | "VAEL_CRYPTO_INTERNAL"
  | "VAEL_CRYPTO_SHAMIR_DUPLICATE"
  | "VAEL_CRYPTO_SHAMIR_LENGTH"
  | "VAEL_CRYPTO_SHAMIR_SHARED";

Error codes reported on Error.code by vael:crypto operations.