Overview
Thenode:crypto module provides cryptographic functionality that includes a set of wrappers for OpenSSL’s hash, HMAC, cipher, decipher, sign, and verify functions.
Core Features
Hashing
Create cryptographic hash digests of data using various algorithms:- SHA-256, SHA-512 - Secure Hash Algorithms
- SHA-3 - SHA-3 family algorithms
- MD5 - Message Digest (legacy, not recommended)
Encryption and Decryption
Encrypt and decrypt data using symmetric and asymmetric algorithms:- AES (CBC, CTR, GCM, OCB modes)
- ChaCha20-Poly1305
- RSA with OAEP padding
Digital Signatures
Sign and verify data using asymmetric key pairs:- RSA (PKCS1, PSS)
- ECDSA (Elliptic Curve Digital Signature Algorithm)
- Ed25519, Ed448 (Edwards-curve signatures)
- ML-DSA (Post-quantum signatures)
Key Classes
Certificate
Work with SPKAC (Signed Public Key and Challenge) data:Cipheriv
Encrypt data using specified algorithm, key, and initialization vector:cipher.update(data[, inputEncoding][, outputEncoding])- Update cipher with datacipher.final([outputEncoding])- Return remaining encrypted datacipher.setAAD(buffer[, options])- Set additional authenticated datacipher.getAuthTag()- Get authentication tag for authenticated encryption
Decipheriv
Decrypt data encrypted with Cipheriv:decipher.update(data[, inputEncoding][, outputEncoding])- Update decipher with encrypted datadecipher.final([outputEncoding])- Return remaining decrypted datadecipher.setAuthTag(buffer[, encoding])- Set authentication tagdecipher.setAAD(buffer[, options])- Set additional authenticated data
Hash
Create hash digests:hash.update(data[, inputEncoding])- Update hash with datahash.digest([encoding])- Calculate and return digesthash.copy([options])- Create deep copy of hash state
Hmac
Create HMAC (Hash-based Message Authentication Code):KeyObject
Represent cryptographic keys:keyObject.asymmetricKeyType- Type of asymmetric key (rsa, ec, ed25519, etc.)keyObject.symmetricKeySize- Size of symmetric key in byteskeyObject.type- Key type: ‘secret’, ‘public’, or ‘private’keyObject.export([options])- Export key in various formats
Key Generation
generateKey()
Generate symmetric keys:generateKeyPair()
Generate asymmetric key pairs:- RSA - RSA key pairs
- RSA-PSS - RSA with PSS padding
- DSA - Digital Signature Algorithm
- EC - Elliptic Curve (secp256k1, P-256, P-384, P-521)
- Ed25519, Ed448 - Edwards-curve keys
- X25519, X448 - Curve25519/448 for key agreement
- DH - Diffie-Hellman
- ML-DSA, ML-KEM - Post-quantum algorithms
Random Data Generation
randomBytes()
Generate cryptographically strong random data:randomUUID()
Generate RFC 4122 version 4 UUID:randomInt()
Generate random integer:Key Derivation
pbkdf2()
Password-Based Key Derivation Function 2:scrypt()
Scrypt key derivation function:hkdf()
HMAC-based Extract-and-Expand Key Derivation Function:Diffie-Hellman Key Exchange
DiffieHellman
Create shared secrets using Diffie-Hellman:ECDH
Elliptic Curve Diffie-Hellman:Asymmetric Key Types
| Key Type | Description | OID |
|---|---|---|
'rsa' | RSA | 1.2.840.113549.1.1.1 |
'rsa-pss' | RSA PSS | 1.2.840.113549.1.1.10 |
'dsa' | DSA | 1.2.840.10040.4.1 |
'ec' | Elliptic curve | 1.2.840.10045.2.1 |
'ed25519' | Ed25519 | 1.3.101.112 |
'ed448' | Ed448 | 1.3.101.113 |
'x25519' | X25519 | 1.3.101.110 |
'x448' | X448 | 1.3.101.111 |
'dh' | Diffie-Hellman | 1.2.840.113549.1.3.1 |
'ml-dsa-44' | ML-DSA-44 (post-quantum) | 2.16.840.1.101.3.4.3.17 |
'ml-dsa-65' | ML-DSA-65 (post-quantum) | 2.16.840.1.101.3.4.3.18 |
'ml-dsa-87' | ML-DSA-87 (post-quantum) | 2.16.840.1.101.3.4.3.19 |
'ml-kem-512' | ML-KEM-512 (post-quantum) | 2.16.840.1.101.3.4.4.1 |
'ml-kem-768' | ML-KEM-768 (post-quantum) | 2.16.840.1.101.3.4.4.2 |
'ml-kem-1024' | ML-KEM-1024 (post-quantum) | 2.16.840.1.101.3.4.4.3 |
Utility Functions
getCiphers()
Get list of supported cipher algorithms:getHashes()
Get list of supported hash algorithms:getCurves()
Get list of supported elliptic curves:Security Considerations
For password hashing, use
scrypt() or pbkdf2() with high iteration counts. Never store passwords in plain text.Related APIs
- Web Crypto API - Browser-compatible cryptography
- TLS/SSL - Secure network communications
- Permissions - Control access to cryptographic operations