Skip to content

node:crypto

How Elide supports Node's crypto module: hashing, ciphers, keys, signatures, ML-DSA and ML-KEM, its error strings, and the APIs that are not available.

On this page

node:crypto provides Node’s crypto API for hashing, ciphers, keys, signatures, key agreement, key derivation, and random values, including ML-DSA and ML-KEM keys. webcrypto is the same object as globalThis.crypto.

Example

js
// crypto.mjs
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "node:crypto";

console.log(createHash("sha256").update("elide").digest("hex"));
console.log(createHmac("sha256", "secret").update("elide").digest("base64url"));

const key = randomBytes(32);
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", key, iv);
const sealed = Buffer.concat([cipher.update("attack at dawn"), cipher.final()]);
const decipher = createDecipheriv("aes-256-gcm", key, iv).setAuthTag(cipher.getAuthTag());
console.log(Buffer.concat([decipher.update(sealed), decipher.final()]).toString());
utf-8 · js · Lines: 12

elide run crypto.mjs prints:

text
bdfdf1cc04d072e4128275829659a2296e17d52f848a06528e36bd2a5a7d142a
XtA6v3HkpDq8cpjaFeOfRO2VxSoKx5bR11f7dK9U25g
attack at dawn
utf-8 · text · Lines: 3

Differences from Node

  • process.versions.openssl is 1.1.1 and process.features.openssl_is_boringssl is true.
  • Errors use BoringSSL-style strings (error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT) and ERR_OSSL_* codes.
  • X509Certificate and Certificate are not exported.
  • Ed448 and X448 throw ERR_CRYPTO_UNSUPPORTED_OPERATION; OCB ciphers are absent.
  • argon2 and argon2Sync throw ERR_CRYPTO_ARGON2_NOT_SUPPORTED.
  • setFips throws ERR_CRYPTO_FIPS_UNAVAILABLE; getFips() returns 0.
  • setEngine throws ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED.

Reference

Status: Partial · 95 of 112 suite tests pass (84.8%) · since 1.1.0

Exports

ExportKindStatusNotes
HashclassFullupdate(), digest(), copy()
HmacclassFullupdate(), digest()
createHash()functionFullCreates and returns a Hash object for the given algorithm
createHmac()functionFullCreates and returns an Hmac object for the given algorithm and key
randomBytes()functionFullGenerates cryptographically strong pseudorandom bytes
randomFill(), randomFillSync()functionFullFills a buffer with random bytes
randomInt()functionFullReturns a random integer n such that min <= n < max
randomUUID()functionFullGenerates a random RFC 4122 version 4 UUID string
pbkdf2(), pbkdf2Sync()functionFullDerives a key using PBKDF2
scrypt(), scryptSync()functionFullDerives a key using scrypt
timingSafeEqual()functionFullCompares two buffers in constant time to prevent timing attacks
getHashes()functionFullReturns the list of supported hash algorithm names
getCiphers()functionFullReturns the list of supported cipher algorithm names
constantspropertyFullCrypto constants such as RSA_PKCS1_PADDING
webcryptopropertyFullThe Web Crypto API (SubtleCrypto) instance

Not implemented

Certificate, X509Certificate, decapsulate, encapsulate.

Tests: node-api node:crypto: 95 pass, 17 fail, 0 error, 69 skipped. Failing: test-crypto-certificate.js, test-crypto-dh-leak.js, test-crypto-encap-decap.js, test-crypto-key-objects-messageport.js, test-crypto-keyobject-hidden-slots.js, test-crypto-pqc-encrypted-pkcs8.js, test-crypto-pqc-key-objects-ml-dsa.js, test-crypto-pqc-key-objects-ml-kem.js, and 9 more.

Was this page helpful?