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
// 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());
elide run crypto.mjs prints:
bdfdf1cc04d072e4128275829659a2296e17d52f848a06528e36bd2a5a7d142a
XtA6v3HkpDq8cpjaFeOfRO2VxSoKx5bR11f7dK9U25g
attack at dawn
Differences from Node
process.versions.opensslis1.1.1andprocess.features.openssl_is_boringsslistrue.- Errors use BoringSSL-style strings (
error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT) andERR_OSSL_*codes. X509CertificateandCertificateare not exported.- Ed448 and X448 throw
ERR_CRYPTO_UNSUPPORTED_OPERATION; OCB ciphers are absent. argon2andargon2SyncthrowERR_CRYPTO_ARGON2_NOT_SUPPORTED.setFipsthrowsERR_CRYPTO_FIPS_UNAVAILABLE;getFips()returns0.setEnginethrowsERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED.
Reference
Status: Partial · 95 of 112 suite tests pass (84.8%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
Hash | class | Full | update(), digest(), copy() |
Hmac | class | Full | update(), digest() |
createHash() | function | Full | Creates and returns a Hash object for the given algorithm |
createHmac() | function | Full | Creates and returns an Hmac object for the given algorithm and key |
randomBytes() | function | Full | Generates cryptographically strong pseudorandom bytes |
randomFill(), randomFillSync() | function | Full | Fills a buffer with random bytes |
randomInt() | function | Full | Returns a random integer n such that min <= n < max |
randomUUID() | function | Full | Generates a random RFC 4122 version 4 UUID string |
pbkdf2(), pbkdf2Sync() | function | Full | Derives a key using PBKDF2 |
scrypt(), scryptSync() | function | Full | Derives a key using scrypt |
timingSafeEqual() | function | Full | Compares two buffers in constant time to prevent timing attacks |
getHashes() | function | Full | Returns the list of supported hash algorithm names |
getCiphers() | function | Full | Returns the list of supported cipher algorithm names |
constants | property | Full | Crypto constants such as RSA_PKCS1_PADDING |
webcrypto | property | Full | The 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.