Skip to content
VaelJS Blog

node:crypto

Node-compatible cryptographic hashes, HMACs, CSPRNG, PBKDF2, and constant-time comparison.

Available in both vjs and vjs-lite runtimes. Enabled by default unless --no-node-compat is passed.

Supported digest algorithms are "sha256", "sha384", and "sha512" (case-insensitive). Legacy algorithms such as MD5 and SHA-1 are intentionally rejected with ERR_CRYPTO_HASH_UNKNOWN / ERR_CRYPTO_INVALID_DIGEST.

import ... from "node:crypto"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
Callback-async scrypt, hkdf, pbkdf2, and randomFill are V8-only (vjs). Synchronous exports and webcrypto run on both vjs and vjs-lite.
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
const digest = createHash("sha256").update("hello").digest("hex");
const mac = createHmac("sha256", "secret").update("hello").digest();
const token = randomBytes(16);
console.log(digest, mac.byteLength, token.byteLength);
console.log(timingSafeEqual(mac, mac));
API Symbols
Functions (18)
randomBytesv0.0.0-alpha.0
Generate size cryptographically strong pseudo-random bytes.
randomUUIDv0.0.0-alpha.0
Generate a random RFC 4122/9562 version 4 UUID string.
createHashv0.0.0-alpha.0
Create an incremental Hash instance for algorithm.
createHmacv0.0.0-alpha.0
Create an incremental Hmac instance for algorithm and key.
pbkdf2Syncv0.0.0-alpha.0
Synchronously derive a key using PBKDF2-HMAC.
timingSafeEqualv0.0.0-alpha.0
Constant-time equality comparison for equal-length byte buffers.
createCipherivv0.0.0-alpha.2
Create a symmetric Cipher object using algorithm, key, and iv.
createDecipherivv0.0.0-alpha.2
Create a symmetric Decipher object using algorithm, key, and iv.
scryptSyncv0.0.0-alpha.2
Synchronously derive a key using scrypt.
scryptvjs onlyv0.0.0-alpha.2
Asynchronously derive a key using scrypt and invoke callback.
hkdfSyncv0.0.0-alpha.2
Synchronously derive a key using HKDF (RFC 5869).
hkdfvjs onlyv0.0.0-alpha.2
Asynchronously derive a key using HKDF (RFC 5869) and invoke callback.
pbkdf2vjs onlyv0.0.0-alpha.2
Asynchronously derive a key using PBKDF2-HMAC and invoke callback.
hashv0.0.0-alpha.2
One-shot cryptographic hash returning an encoded string (default "hex").
randomIntv0.0.0-alpha.2
Return a cryptographically random integer n in 0 <= n < max.
randomFillSyncv0.0.0-alpha.2
Synchronously fill buffer (or sub-range [offset, offset + size)) with random bytes.
randomFillvjs onlyv0.0.0-alpha.2
Asynchronously fill buffer with cryptographically strong random bytes.
getHashesv0.0.0-alpha.2
Return an array of supported hash algorithm names.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function randomBytes(size: number): Uint8Array;

Generate size cryptographically strong pseudo-random bytes.

Backed by the operating system CSPRNG and bounded to 8 MiB per call.

Parameters
NameTypeDescription
sizenumberNumber of random bytes to generate (0 to 8_388_608).
Returns
Uint8Array — Newly allocated Buffer (Uint8Array subclass) filled with random bytes.
Exceptions
  • RangeError — when size is negative, not an integer, or exceeds 8 MiB (VAEL_CRYPTO_LIMIT).
  • Error — if the operating system CSPRNG fails.
import { Buffer } from "node:buffer";
import { randomBytes } from "node:crypto";
const nonce = randomBytes(12);
console.log(Buffer.from(nonce).toString("hex"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function randomUUID(): string;

Generate a random RFC 4122/9562 version 4 UUID string.

Returns
string — Lowercase hyphenated UUID v4 string (36 characters).
Exceptions
  • Error — with code = "ERR_UNAVAILABLE" if the operating system CSPRNG fails.
import { randomUUID } from "node:crypto";
const id = randomUUID();
console.log(id);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createHash(algorithm: string): Hash;

Create an incremental Hash instance for algorithm.

Supports "sha256", "sha384", and "sha512" (case-insensitive). Up to 1024 digest handles may be open simultaneously.

Parameters
NameTypeDescription
algorithmstringHash algorithm name ("sha256", "sha384", or "sha512").
Returns
Hash — Incremental Hash instance.
Exceptions
  • TypeError — when algorithm is not a string.
  • Error — with code = "ERR_CRYPTO_HASH_UNKNOWN" when algorithm is unsupported.
  • RangeError — with code = "ERR_CRYPTO_TOO_MANY_OBJECTS" if more than 1024 digest handles are open.
import { createHash } from "node:crypto";
const hex = createHash("sha256").update("vaeljs", "utf8").digest("hex");
console.log(hex);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createHmac(algorithm: string, key: BinaryLike): Hmac;

Create an incremental Hmac instance for algorithm and key.

Supports "sha256", "sha384", and "sha512" (case-insensitive), with keys up to 1 MiB. Up to 1024 digest handles may be open simultaneously.

Parameters
NameTypeDescription
algorithmstringHash algorithm name ("sha256", "sha384", or "sha512").
keyBinaryLikeSecret HMAC key (UTF-8 string or byte view, max 1 MiB).
Returns
Hmac — Incremental Hmac instance.
Exceptions
  • TypeError — when algorithm is not a string or key has an invalid type.
  • Error — with code = "ERR_CRYPTO_HASH_UNKNOWN" when algorithm is unsupported.
  • RangeError — with code = "VAEL_CRYPTO_LIMIT" if key exceeds 1 MiB, or "ERR_CRYPTO_TOO_MANY_OBJECTS" if too many handles are open.
import { createHmac } from "node:crypto";
const tag = createHmac("sha256", "signing-key").update("payload").digest("base64url");
console.log(tag);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function pbkdf2Sync(
  password: BinaryLike,
  salt: BinaryLike,
  iterations: number,
  keylen: number,
  digest: string,
): Uint8Array;

Synchronously derive a key using PBKDF2-HMAC.

Supports "sha256", "sha384", and "sha512" digests. Bounded to 1..=1_000_000 iterations, keylen <= 1024, and 1 MiB max password/salt.

Parameters
NameTypeDescription
passwordBinaryLikeInput password bytes or UTF-8 string (max 1 MiB).
saltBinaryLikeCryptographic salt bytes or UTF-8 string (max 1 MiB).
iterationsnumberIteration count (1 to 1_000_000).
keylennumberDerived key length in bytes (0 to 1024).
digeststringHash algorithm name ("sha256", "sha384", or "sha512").
Returns
Uint8Array — Derived key bytes as a Buffer (Uint8Array subclass).
Exceptions
  • TypeError — with code = "ERR_CRYPTO_INVALID_DIGEST" when digest is unsupported, or on invalid argument types.
  • RangeError — when iterations, keylen, password, or salt is out of bounds.
import { Buffer } from "node:buffer";
import { pbkdf2Sync } from "node:crypto";
const key = pbkdf2Sync("passphrase", "salt-value", 10000, 32, "sha256");
console.log(Buffer.from(key).toString("hex"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function timingSafeEqual(a: ArrayBufferView, b: ArrayBufferView): boolean;

Constant-time equality comparison for equal-length byte buffers.

Parameters
NameTypeDescription
aArrayBufferViewFirst TypedArray or DataView to compare.
bArrayBufferViewSecond TypedArray or DataView to compare (must have the same byteLength as a).
Returns
boolean — true if a and b contain identical bytes, otherwise false.
Exceptions
  • TypeError — when a or b is not an ArrayBufferView.
  • RangeError — with code = "ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH" when a.byteLength !== b.byteLength.
import { Buffer } from "node:buffer";
import { timingSafeEqual } from "node:crypto";
const left = Buffer.from("secret", "utf8");
const right = Buffer.from("secret", "utf8");
console.log(timingSafeEqual(left, right)); // true
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function createCipheriv(
  algorithm: string,
  key: BinaryLike,
  iv: BinaryLike | null,
  options?: CipherOptions,
): Cipher;

Create a symmetric Cipher object using algorithm, key, and iv.

Parameters
NameTypeDescription
algorithmstringCipher algorithm name (for example "aes-256-gcm" or "aes-256-cbc").
keyBinaryLikeRaw key bytes or UTF-8 string.
ivBinaryLike | nullInitialization vector (or null for ECB modes).
options optionalCipherOptionsOptional cipher configuration (authTagLength).
Returns
Cipher — Symmetric Cipher instance.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function createDecipheriv(
  algorithm: string,
  key: BinaryLike,
  iv: BinaryLike | null,
  options?: CipherOptions,
): Decipher;

Create a symmetric Decipher object using algorithm, key, and iv.

Parameters
NameTypeDescription
algorithmstringCipher algorithm name (for example "aes-256-gcm" or "aes-256-cbc").
keyBinaryLikeRaw key bytes or UTF-8 string.
ivBinaryLike | nullInitialization vector (or null for ECB modes).
options optionalCipherOptionsOptional cipher configuration (authTagLength).
Returns
Decipher — Symmetric Decipher instance.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function scryptSync(
  password: BinaryLike,
  salt: BinaryLike,
  keylen: number,
  options?: ScryptOptions,
): Uint8Array;

Synchronously derive a key using scrypt.

Parameters
NameTypeDescription
passwordBinaryLikeInput password bytes or UTF-8 string.
saltBinaryLikeSalt bytes or UTF-8 string.
keylennumberDerived key length in bytes.
options optionalScryptOptionsOptional cost parameters (N, r, p, maxmem).
Returns
Uint8Array — Derived key bytes as a Buffer (Uint8Array subclass).
functionSince v0.0.0-alpha.2vjs only
export function scrypt(
  password: BinaryLike,
  salt: BinaryLike,
  keylen: number,
  callback: (err: Error | null, derivedKey: Uint8Array) => void,
): void;
export function scrypt(
  password: BinaryLike,
  salt: BinaryLike,
  keylen: number,
  options: ScryptOptions,
  callback: (err: Error | null, derivedKey: Uint8Array) => void,
): void;

Asynchronously derive a key using scrypt and invoke callback.

Available in vjs only (vjs-lite provides synchronous scryptSync only).

Overload 1 — scrypt(password, salt, keylen, callback)
NameTypeDescription
passwordBinaryLikeInput password bytes or UTF-8 string.
saltBinaryLikeSalt bytes or UTF-8 string.
keylennumberDerived key length in bytes.
callback(err: Error | null, derivedKey: Uint8Array) => voidCallback receiving (err, derivedKey).
void
Overload 2 — scrypt(password, salt, keylen, options, callback)

Asynchronously derive a key using scrypt with options and invoke callback. Available in vjs only (vjs-lite provides synchronous scryptSync only).

NameTypeDescription
passwordBinaryLikeInput password bytes or UTF-8 string.
saltBinaryLikeSalt bytes or UTF-8 string.
keylennumberDerived key length in bytes.
optionsScryptOptionsCost parameters (N, r, p, maxmem).
callback(err: Error | null, derivedKey: Uint8Array) => voidCallback receiving (err, derivedKey).
void
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function hkdfSync(
  digest: string,
  ikm: BinaryLike,
  salt: BinaryLike,
  info: BinaryLike,
  keylen: number,
): ArrayBuffer;

Synchronously derive a key using HKDF (RFC 5869).

Parameters
NameTypeDescription
digeststringHash algorithm name ("sha256", "sha384", or "sha512").
ikmBinaryLikeInput keying material.
saltBinaryLikeSalt bytes or UTF-8 string.
infoBinaryLikeContext/application-specific info bytes or UTF-8 string.
keylennumberDerived key length in bytes.
Returns
ArrayBuffer — ArrayBuffer containing the derived key.
functionSince v0.0.0-alpha.2vjs only
export function hkdf(
  digest: string,
  ikm: BinaryLike,
  salt: BinaryLike,
  info: BinaryLike,
  keylen: number,
  callback: (err: Error | null, derivedKey: ArrayBuffer) => void,
): void;

Asynchronously derive a key using HKDF (RFC 5869) and invoke callback.

Available in vjs only (vjs-lite provides synchronous hkdfSync only).

Parameters
NameTypeDescription
digeststringHash algorithm name ("sha256", "sha384", or "sha512").
ikmBinaryLikeInput keying material.
saltBinaryLikeSalt bytes or UTF-8 string.
infoBinaryLikeContext/application-specific info bytes or UTF-8 string.
keylennumberDerived key length in bytes.
callback(err: Error | null, derivedKey: ArrayBuffer) => voidCallback receiving (err, derivedKey).
Returns
void
functionSince v0.0.0-alpha.2vjs only
export function pbkdf2(
  password: BinaryLike,
  salt: BinaryLike,
  iterations: number,
  keylen: number,
  digest: string,
  callback: (err: Error | null, derivedKey: Uint8Array) => void,
): void;

Asynchronously derive a key using PBKDF2-HMAC and invoke callback.

Available in vjs only (vjs-lite provides synchronous pbkdf2Sync only).

Parameters
NameTypeDescription
passwordBinaryLikeInput password bytes or UTF-8 string.
saltBinaryLikeCryptographic salt bytes or UTF-8 string.
iterationsnumberIteration count.
keylennumberDerived key length in bytes.
digeststringHash algorithm name ("sha256", "sha384", or "sha512").
callback(err: Error | null, derivedKey: Uint8Array) => voidCallback receiving (err, derivedKey).
Returns
void
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function hash(
  algorithm: string,
  data: BinaryLike,
  outputEncoding?: DigestEncoding,
): string;
export function hash(
  algorithm: string,
  data: BinaryLike,
  outputEncoding: "buffer",
): Uint8Array;

One-shot cryptographic hash returning an encoded string (default "hex").

One-shot cryptographic hash returning raw digest bytes (outputEncoding: "buffer").

Parameters
NameTypeDescription
algorithmstringHash algorithm name ("sha256", "sha384", or "sha512").
dataBinaryLikeInput bytes or UTF-8 string.
outputEncoding optionalDigestEncoding | "buffer"Output encoding (default "hex"); "buffer".
Returns
  • string — Encoded digest string.
  • Uint8Array — Raw digest bytes as a Buffer (Uint8Array subclass).
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function randomInt(max: number): number;
export function randomInt(min: number, max: number): number;

Return a cryptographically random integer n in 0 <= n < max.

Overload 1 — randomInt(max)
NameTypeDescription
maxnumberExclusive upper bound.
number — Uniformly distributed random integer.
Overload 2 — randomInt(min, max)

Return a cryptographically random integer n in min <= n < max.

NameTypeDescription
minnumberInclusive lower bound.
maxnumberExclusive upper bound.
number — Uniformly distributed random integer.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function randomFillSync<T extends ArrayBufferView>(
  buffer: T,
  offset?: number,
  size?: number,
): T;

Synchronously fill buffer (or sub-range [offset, offset + size)) with random bytes.

Parameters
NameTypeDescription
bufferTDestination ArrayBufferView.
offset optionalnumberElement/byte offset to start filling (default 0).
size optionalnumberNumber of elements/bytes to fill.
Returns
T — The mutated buffer.
functionSince v0.0.0-alpha.2vjs only
export function randomFill<T extends ArrayBufferView>(
  buffer: T,
  callback: (err: Error | null, buf: T) => void,
): void;
export function randomFill<T extends ArrayBufferView>(
  buffer: T,
  offset: number,
  callback: (err: Error | null, buf: T) => void,
): void;
export function randomFill<T extends ArrayBufferView>(
  buffer: T,
  offset: number,
  size: number,
  callback: (err: Error | null, buf: T) => void,
): void;

Asynchronously fill buffer with cryptographically strong random bytes.

Available in vjs only (vjs-lite provides synchronous randomFillSync only).

Overload 1 — randomFill(buffer, callback)
NameTypeDescription
bufferTDestination ArrayBufferView.
callback(err: Error | null, buf: T) => voidCallback receiving (err, buf).
void
Overload 2 — randomFill(buffer, offset, callback)

Asynchronously fill buffer starting at offset with random bytes. Available in vjs only (vjs-lite provides synchronous randomFillSync only).

NameTypeDescription
bufferTDestination ArrayBufferView.
offsetnumberByte/element offset.
callback(err: Error | null, buf: T) => voidCallback receiving (err, buf).
void
Overload 3 — randomFill(buffer, offset, size, callback)

Asynchronously fill size units of buffer starting at offset with random bytes. Available in vjs only (vjs-lite provides synchronous randomFillSync only).

NameTypeDescription
bufferTDestination ArrayBufferView.
offsetnumberByte/element offset.
sizenumberNumber of elements/bytes to fill.
callback(err: Error | null, buf: T) => voidCallback receiving (err, buf).
void
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function getHashes(): string[];

Return an array of supported hash algorithm names.

Returns
string[] — Array of hash algorithm identifiers (such as ["sha256", "sha384", "sha512"]).
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface Hash {
  update(data: BinaryLike, inputEncoding?: string): this;
  digest(): Uint8Array;
  digest(encoding: DigestEncoding): string;
}

Incremental cryptographic hash object returned by createHash.

Methods
methodSince v0.0.0-alpha.0
update(data: BinaryLike, inputEncoding?: string): this;

Update hash content with data.

May be called repeatedly before digest() up to 16 MiB total input per digest object. Fails after digest() has been called.

Parameters
NameTypeDescription
dataBinaryLikeInput bytes or string to feed into the hash state.
inputEncoding optionalstringEncoding used when data is a string (default "utf8").
Returns
this — this for method chaining.
Exceptions
  • Error — if digest() was already called, or with code = "VAEL_CRYPTO_LIMIT" if cumulative input exceeds 16 MiB.
  • TypeError — when data has an invalid type or inputEncoding is unknown (ERR_UNKNOWN_ENCODING).
methodSince v0.0.0-alpha.0
digest(): Uint8Array;
digest(encoding: DigestEncoding): string;

Compute raw digest bytes.

Finalizes the hash state; subsequent update() or digest() calls throw.

Compute encoded digest string.

Finalizes the hash state; subsequent update() or digest() calls throw.

Parameters
NameTypeDescription
encoding optionalDigestEncodingOutput string encoding ("hex", "base64", "base64url", "latin1", or "binary").
Returns
  • Uint8Array — Raw digest bytes as a Buffer (Uint8Array subclass).
  • string — Encoded digest string.
Exceptions
  • Error — if digest() was already called.
  • TypeError — with code = "ERR_UNKNOWN_ENCODING" when encoding is unsupported.
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface Hmac {
  update(data: BinaryLike, inputEncoding?: string): this;
  digest(): Uint8Array;
  digest(encoding: DigestEncoding): string;
}

Incremental cryptographic HMAC object returned by createHmac.

Methods
methodSince v0.0.0-alpha.0
update(data: BinaryLike, inputEncoding?: string): this;

Update HMAC content with data.

May be called repeatedly before digest() up to 16 MiB total input per HMAC object. Fails after digest() has been called.

Parameters
NameTypeDescription
dataBinaryLikeInput bytes or string to authenticate.
inputEncoding optionalstringEncoding used when data is a string (default "utf8").
Returns
this — this for method chaining.
Exceptions
  • Error — if digest() was already called, or with code = "VAEL_CRYPTO_LIMIT" if cumulative input exceeds 16 MiB.
  • TypeError — when data has an invalid type or inputEncoding is unknown (ERR_UNKNOWN_ENCODING).
methodSince v0.0.0-alpha.0
digest(): Uint8Array;
digest(encoding: DigestEncoding): string;

Compute raw digest bytes.

Finalizes the HMAC state; subsequent update() or digest() calls throw.

Compute encoded digest string.

Finalizes the HMAC state; subsequent update() or digest() calls throw.

Parameters
NameTypeDescription
encoding optionalDigestEncodingOutput string encoding ("hex", "base64", "base64url", "latin1", or "binary").
Returns
  • Uint8Array — Raw HMAC tag bytes as a Buffer (Uint8Array subclass).
  • string — Encoded HMAC tag string.
Exceptions
  • Error — if digest() was already called.
  • TypeError — with code = "ERR_UNKNOWN_ENCODING" when encoding is unsupported.
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface CipherOptions {
  authTagLength?: number;
}

Options accepted by createCipheriv and createDecipheriv.

Properties
PropertyTypeDescriptionSince
authTagLength optionalnumberAuthentication tag byte length for AEAD ciphers (for example 16 for GCM).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface Cipher {
  update(data: BinaryLike, inputEncoding?: string): Uint8Array;
  update(
      data: BinaryLike,
      inputEncoding: string | undefined,
      outputEncoding: DigestEncoding,
    ): string;
  final(): Uint8Array;
  final(outputEncoding: DigestEncoding): string;
  setAutoPadding(autoPadding?: boolean): this;
  setAAD?(buffer: ArrayBufferView, options?: { plaintextLength?: number }): this;
  getAuthTag?(): Uint8Array;
}

Symmetric encryption cipher instance returned by createCipheriv.

Methods
methodSince v0.0.0-alpha.2
update(data: BinaryLike, inputEncoding?: string): Uint8Array;
update(
  data: BinaryLike,
  inputEncoding: string | undefined,
  outputEncoding: DigestEncoding,
): string;

Encrypt data as raw bytes.

Encrypt data and encode ciphertext as a string.

Parameters
NameTypeDescription
dataBinaryLike—
inputEncoding optionalstring | string | undefined—
outputEncoding optionalDigestEncoding—
Returns
methodSince v0.0.0-alpha.2
final(): Uint8Array;
final(outputEncoding: DigestEncoding): string;

Finalize encryption and return remaining ciphertext bytes.

Finalize encryption and encode remaining ciphertext as a string.

Parameters
NameTypeDescription
outputEncoding optionalDigestEncoding—
Returns
methodSince v0.0.0-alpha.2
setAutoPadding(autoPadding?: boolean): this;

Enable or disable PKCS#7 automatic padding for block ciphers.

Parameters
NameTypeDescription
autoPadding optionalboolean—
Returns
this
methodSince v0.0.0-alpha.2
setAAD?(buffer: ArrayBufferView, options?: { plaintextLength?: number }): this;

Supply additional authenticated data (AAD) for AEAD ciphers (GCM).

Parameters
NameTypeDescription
bufferArrayBufferView—
options optional{ plaintextLength?: number }—
Returns
this
methodSince v0.0.0-alpha.2
getAuthTag?(): Uint8Array;

Return the AEAD authentication tag after final().

Returns
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface Decipher {
  update(data: BinaryLike, inputEncoding?: string): Uint8Array;
  update(
      data: BinaryLike,
      inputEncoding: string | undefined,
      outputEncoding: string,
    ): string;
  final(): Uint8Array;
  final(outputEncoding: string): string;
  setAutoPadding(autoPadding?: boolean): this;
  setAAD?(buffer: ArrayBufferView, options?: { plaintextLength?: number }): this;
  setAuthTag?(buffer: ArrayBufferView): this;
}

Symmetric decryption decipher instance returned by createDecipheriv.

Methods
methodSince v0.0.0-alpha.2
update(data: BinaryLike, inputEncoding?: string): Uint8Array;
update(
  data: BinaryLike,
  inputEncoding: string | undefined,
  outputEncoding: string,
): string;

Decrypt data as raw bytes.

Decrypt data and decode plaintext as a string.

Parameters
NameTypeDescription
dataBinaryLike—
inputEncoding optionalstring | string | undefined—
outputEncoding optionalstring—
Returns
methodSince v0.0.0-alpha.2
final(): Uint8Array;
final(outputEncoding: string): string;

Finalize decryption and return remaining plaintext bytes.

Finalize decryption and decode remaining plaintext as a string.

Parameters
NameTypeDescription
outputEncoding optionalstring—
Returns
methodSince v0.0.0-alpha.2
setAutoPadding(autoPadding?: boolean): this;

Enable or disable PKCS#7 automatic padding for block ciphers.

Parameters
NameTypeDescription
autoPadding optionalboolean—
Returns
this
methodSince v0.0.0-alpha.2
setAAD?(buffer: ArrayBufferView, options?: { plaintextLength?: number }): this;

Supply additional authenticated data (AAD) for AEAD ciphers (GCM).

Parameters
NameTypeDescription
bufferArrayBufferView—
options optional{ plaintextLength?: number }—
Returns
this
methodSince v0.0.0-alpha.2
setAuthTag?(buffer: ArrayBufferView): this;

Set the expected AEAD authentication tag before calling final().

Parameters
NameTypeDescription
bufferArrayBufferView—
Returns
this
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface ScryptOptions {
  N?: number;
  cost?: number;
  r?: number;
  blockSize?: number;
  p?: number;
  parallelization?: number;
  maxmem?: number;
}

Cost parameters accepted by scrypt() and scryptSync().

Properties
PropertyTypeDescriptionSince
N optionalnumberCPU/memory cost parameter (power of two, default 16384).v0.0.0-alpha.2
cost optionalnumberAlias for N.v0.0.0-alpha.2
r optionalnumberBlock size parameter (default 8).v0.0.0-alpha.2
blockSize optionalnumberAlias for r.v0.0.0-alpha.2
p optionalnumberParallelization parameter (default 1).v0.0.0-alpha.2
parallelization optionalnumberAlias for p.v0.0.0-alpha.2
maxmem optionalnumberUpper memory limit in bytes (default 32 * 1024 * 1024).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface WebCryptoKey {
  readonly type: "secret" | "private" | "public";
  readonly extractable: boolean;
  readonly algorithm: Record<string, unknown>;
  readonly usages: readonly string[];
}

Cryptographic key descriptor used by webcrypto.subtle.

Properties
PropertyTypeDescriptionSince
type readonly"secret" | "private" | "public"Key type ("secret", "private", or "public").v0.0.0-alpha.2
extractable readonlybooleanWhether raw key material can be exported.v0.0.0-alpha.2
algorithm readonlyRecord<string, unknown>Algorithm metadata associated with this key.v0.0.0-alpha.2
usages readonlyreadonly string[]Permitted key usages ("encrypt", "decrypt", "sign", "verify", etc.).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface WebCryptoKeyPair {
  readonly publicKey: WebCryptoKey;
  readonly privateKey: WebCryptoKey;
}

Key pair returned by webcrypto.subtle.generateKey for asymmetric algorithms.

Properties
PropertyTypeDescriptionSince
publicKey readonlyWebCryptoKeyPublic key handle.v0.0.0-alpha.2
privateKey readonlyWebCryptoKeyPrivate key handle.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface WebSubtleCrypto {
  digest(algorithm: string | { name: string }, data: ArrayBufferView | ArrayBuffer): Promise<ArrayBuffer>;
  encrypt(
      algorithm: string | Record<string, unknown>,
      key: WebCryptoKey,
      data: ArrayBufferView | ArrayBuffer,
    ): Promise<ArrayBuffer>;
  decrypt(
      algorithm: string | Record<string, unknown>,
      key: WebCryptoKey,
      data: ArrayBufferView | ArrayBuffer,
    ): Promise<ArrayBuffer>;
  sign(
      algorithm: string | Record<string, unknown>,
      key: WebCryptoKey,
      data: ArrayBufferView | ArrayBuffer,
    ): Promise<ArrayBuffer>;
  verify(
      algorithm: string | Record<string, unknown>,
      key: WebCryptoKey,
      signature: ArrayBufferView | ArrayBuffer,
      data: ArrayBufferView | ArrayBuffer,
    ): Promise<boolean>;
  deriveBits(
      algorithm: string | Record<string, unknown>,
      baseKey: WebCryptoKey,
      length: number,
    ): Promise<ArrayBuffer>;
  deriveKey(
      algorithm: string | Record<string, unknown>,
      baseKey: WebCryptoKey,
      derivedKeyType: string | Record<string, unknown>,
      extractable: boolean,
      keyUsages: readonly string[],
    ): Promise<WebCryptoKey>;
  generateKey(
      algorithm: string | Record<string, unknown>,
      extractable: boolean,
      keyUsages: readonly string[],
    ): Promise<WebCryptoKey | WebCryptoKeyPair>;
  importKey(
      format: "raw" | "pkcs8" | "spki" | "jwk",
      keyData: ArrayBufferView | ArrayBuffer | Record<string, unknown>,
      algorithm: string | Record<string, unknown>,
      extractable: boolean,
      keyUsages: readonly string[],
    ): Promise<WebCryptoKey>;
  exportKey(format: "raw" | "pkcs8" | "spki", key: WebCryptoKey): Promise<ArrayBuffer>;
  exportKey(format: "jwk", key: WebCryptoKey): Promise<Record<string, unknown>>;
}

WebCrypto SubtleCrypto interface exposed on webcrypto.subtle.

Methods
methodSince v0.0.0-alpha.2
digest(algorithm: string | { name: string }, data: ArrayBufferView | ArrayBuffer): Promise<ArrayBuffer>;

Compute a cryptographic digest.

Parameters
NameTypeDescription
algorithmstring | { name: string }—
dataArrayBufferView | ArrayBuffer—
methodSince v0.0.0-alpha.2
encrypt(
  algorithm: string | Record<string, unknown>,
  key: WebCryptoKey,
  data: ArrayBufferView | ArrayBuffer,
): Promise<ArrayBuffer>;

Encrypt data using key and algorithm.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
keyWebCryptoKey—
dataArrayBufferView | ArrayBuffer—
methodSince v0.0.0-alpha.2
decrypt(
  algorithm: string | Record<string, unknown>,
  key: WebCryptoKey,
  data: ArrayBufferView | ArrayBuffer,
): Promise<ArrayBuffer>;

Decrypt data using key and algorithm.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
keyWebCryptoKey—
dataArrayBufferView | ArrayBuffer—
methodSince v0.0.0-alpha.2
sign(
  algorithm: string | Record<string, unknown>,
  key: WebCryptoKey,
  data: ArrayBufferView | ArrayBuffer,
): Promise<ArrayBuffer>;

Generate a digital signature.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
keyWebCryptoKey—
dataArrayBufferView | ArrayBuffer—
methodSince v0.0.0-alpha.2
verify(
  algorithm: string | Record<string, unknown>,
  key: WebCryptoKey,
  signature: ArrayBufferView | ArrayBuffer,
  data: ArrayBufferView | ArrayBuffer,
): Promise<boolean>;

Verify a digital signature.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
keyWebCryptoKey—
signatureArrayBufferView | ArrayBuffer—
dataArrayBufferView | ArrayBuffer—
Returns
Promise<boolean>
methodSince v0.0.0-alpha.2
deriveBits(
  algorithm: string | Record<string, unknown>,
  baseKey: WebCryptoKey,
  length: number,
): Promise<ArrayBuffer>;

Derive raw bits from a base key.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
baseKeyWebCryptoKey—
lengthnumber—
methodSince v0.0.0-alpha.2
deriveKey(
  algorithm: string | Record<string, unknown>,
  baseKey: WebCryptoKey,
  derivedKeyType: string | Record<string, unknown>,
  extractable: boolean,
  keyUsages: readonly string[],
): Promise<WebCryptoKey>;

Derive a WebCryptoKey from a base key.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
baseKeyWebCryptoKey—
derivedKeyTypestring | Record<string, unknown>—
extractableboolean—
keyUsagesreadonly string[]—
methodSince v0.0.0-alpha.2
generateKey(
  algorithm: string | Record<string, unknown>,
  extractable: boolean,
  keyUsages: readonly string[],
): Promise<WebCryptoKey | WebCryptoKeyPair>;

Generate a symmetric key or asymmetric key pair.

Parameters
NameTypeDescription
algorithmstring | Record<string, unknown>—
extractableboolean—
keyUsagesreadonly string[]—
methodSince v0.0.0-alpha.2
importKey(
  format: "raw" | "pkcs8" | "spki" | "jwk",
  keyData: ArrayBufferView | ArrayBuffer | Record<string, unknown>,
  algorithm: string | Record<string, unknown>,
  extractable: boolean,
  keyUsages: readonly string[],
): Promise<WebCryptoKey>;

Import raw, PKCS#8, SPKI, or JWK key material.

Parameters
NameTypeDescription
format"raw" | "pkcs8" | "spki" | "jwk"—
keyDataArrayBufferView | ArrayBuffer | Record<string, unknown>—
algorithmstring | Record<string, unknown>—
extractableboolean—
keyUsagesreadonly string[]—
methodSince v0.0.0-alpha.2
exportKey(format: "raw" | "pkcs8" | "spki", key: WebCryptoKey): Promise<ArrayBuffer>;
exportKey(format: "jwk", key: WebCryptoKey): Promise<Record<string, unknown>>;

Export key material in raw, PKCS#8, SPKI, or JWK format.

Export key material in JWK format.

Parameters
NameTypeDescription
format"raw" | "pkcs8" | "spki" | "jwk"—
keyWebCryptoKey—
Returns
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface WebCrypto {
  readonly subtle: WebSubtleCrypto;
  getRandomValues<T extends ArrayBufferView>(typedArray: T): T;
  randomUUID(): string;
}

Web Cryptography API object exposed as crypto.webcrypto.

Properties
PropertyTypeDescriptionSince
subtle readonlyWebSubtleCryptoSubtle cryptographic primitives interface.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
getRandomValues<T extends ArrayBufferView>(typedArray: T): T;

Fill typedArray with cryptographically strong random values.

Parameters
NameTypeDescription
typedArrayT—
Returns
T
methodSince v0.0.0-alpha.2
randomUUID(): string;

Generate a random RFC 4122/9562 v4 UUID string.

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

Supported digest output encodings.

typeSince v0.0.0-alpha.0vjs & vjs-lite
export type BinaryLike = string | Uint8Array | ArrayBufferView;

Binary or string input accepted by Hash, Hmac, and pbkdf2Sync.

typeSince v0.0.0-alpha.2vjs & vjs-lite
export type CryptoConstants = Readonly<Record<string, number | string>>;

Cryptographic constants exposed on crypto.constants.

constSince v0.0.0-alpha.2vjs & vjs-lite
export const webcrypto: WebCrypto;

Web Cryptography API interface (subtle, getRandomValues, randomUUID).

constSince v0.0.0-alpha.2vjs & vjs-lite
export const constants: CryptoConstants;

Cryptographic constants map.

defaultSince v0.0.0-alpha.0vjs & vjs-lite
const _default: {
  /** Generate cryptographically strong random bytes. @since 0.0.0-alpha.0 */
  randomBytes: typeof randomBytes;
  /** Generate an RFC 4122/9562 v4 UUID string. @since 0.0.0-alpha.0 */
  randomUUID: typeof randomUUID;
  /** Create an incremental hash object. @since 0.0.0-alpha.0 */
  createHash: typeof createHash;
  /** Create an incremental HMAC object. @since 0.0.0-alpha.0 */
  createHmac: typeof createHmac;
  /** Derive a key using PBKDF2-HMAC. @since 0.0.0-alpha.0 */
  pbkdf2Sync: typeof pbkdf2Sync;
  /** Compare two equal-length buffers in constant time. @since 0.0.0-alpha.0 */
  timingSafeEqual: typeof timingSafeEqual;
  /** Create a symmetric encryption cipher. @since 0.0.0-alpha.2 */
  createCipheriv: typeof createCipheriv;
  /** Create a symmetric decryption decipher. @since 0.0.0-alpha.2 */
  createDecipheriv: typeof createDecipheriv;
  /** Synchronously derive a key using scrypt. @since 0.0.0-alpha.2 */
  scryptSync: typeof scryptSync;
  /** Asynchronously derive a key using scrypt. @since 0.0.0-alpha.2 */
  scrypt: typeof scrypt;
  /** Synchronously derive a key using HKDF. @since 0.0.0-alpha.2 */
  hkdfSync: typeof hkdfSync;
  /** Asynchronously derive a key using HKDF. @since 0.0.0-alpha.2 */
  hkdf: typeof hkdf;
  /** Asynchronously derive a key using PBKDF2-HMAC. @since 0.0.0-alpha.2 */
  pbkdf2: typeof pbkdf2;
  /** Compute a one-shot cryptographic hash. @since 0.0.0-alpha.2 */
  hash: typeof hash;
  /** Generate a uniformly distributed random integer. @since 0.0.0-alpha.2 */
  randomInt: typeof randomInt;
  /** Synchronously fill a buffer with random bytes. @since 0.0.0-alpha.2 */
  randomFillSync: typeof randomFillSync;
  /** Asynchronously fill a buffer with random bytes. @since 0.0.0-alpha.2 */
  randomFill: typeof randomFill;
  /** List supported hash algorithm names. @since 0.0.0-alpha.2 */
  getHashes: typeof getHashes;
  /** Web Cryptography API interface. @since 0.0.0-alpha.2 */
  readonly webcrypto: WebCrypto;
  /** Cryptographic constants. @since 0.0.0-alpha.2 */
  readonly constants: CryptoConstants;
};
export default _default;
vjs-lite parity: scrypt, hkdf, pbkdf2, randomFill are available in vjs (V8) only and omitted from the default export in vjs-lite.

Default export object providing all node:crypto functions.