Node.js module
crypto
The 'node:crypto' module provides cryptographic functionality, including wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, verify, and key derivation functions.
It supports common algorithms such as SHA-256, AES, RSA, ECDH, and more. The module also offers secure random number generation, key management, and certificate handling, making it essential for implementing secure protocols and data encryption.
Works in Bun · notes
Most crypto functionality is implemented, but some specific methods related to engine configuration, FIPS mode, and secure heap usage are missing.
Functions 56
- crypto.argon2()
Provides an asynchronous Argon2 implementation.
- crypto.argon2Sync()
Provides a synchronous [Argon2][] implementation.
- crypto.checkPrime()
Checks the primality of the
candidate. - crypto.checkPrimeSync()
Checks the primality of the
candidate. - crypto.createCipheriv()
Creates and returns a
Cipherobject, with the givenalgorithm,keyand initialization vector (iv). - crypto.createDecipheriv()
Creates and returns a
Decipherivobject that uses the givenalgorithm,keyand initialization vector (iv). - crypto.createDiffieHellman()
Creates a
DiffieHellmankey exchange object using the suppliedprimeand an optional specificgenerator. - crypto.createDiffieHellmanGroup()
An alias for getDiffieHellman
- crypto.createECDH()
Creates an Elliptic Curve Diffie-Hellman (
ECDH) key exchange object using a predefined curve specified by thecurveNamestring. - crypto.createHash()
Creates and returns a
Hashobject that can be used to generate hash digests using the givenalgorithm. - crypto.createHmac()
Creates and returns an
Hmacobject that uses the givenalgorithmandkey. - crypto.createPrivateKey()
Creates and returns a new key object containing a private key.
- crypto.createPublicKey()
Creates and returns a new key object containing a public key.
- crypto.createSecretKey()
Creates and returns a new key object containing a secret key for symmetric encryption or
Hmac. - crypto.createSign()
Creates and returns a
Signobject that uses the givenalgorithm. - crypto.createVerify()
Creates and returns a
Verifyobject that uses the given algorithm. - crypto.decapsulate()
Key decapsulation using a KEM algorithm with a private key.
- crypto.diffieHellman()
Computes the Diffie-Hellman shared secret based on a
privateKeyand apublicKey. - crypto.encapsulate()
Key encapsulation using a KEM algorithm with a public key.
- crypto.generateKey()
Asynchronously generates a new random secret key of the given
length. - crypto.generateKeyPair()
Generates a new asymmetric key pair of the given
type. - crypto.generateKeyPairSync()
Generates a new asymmetric key pair of the given
type. - crypto.generateKeySync()
Synchronously generates a new random secret key of the given
length. - crypto.generatePrime()
Generates a pseudorandom prime of
sizebits. - crypto.generatePrimeSync()
Generates a pseudorandom prime of
sizebits. - crypto.getCipherInfo()
Returns information about a given cipher.
- crypto.getCiphers()
``js const { getCiphers, } = await import('node:crypto') - crypto.getCurves()
``js const { getCurves, } = await import('node:crypto') - crypto.getDiffieHellman()
Creates a predefined
DiffieHellmanGroupkey exchange object. - crypto.getFips()
- crypto.getHashes()
``js const { getHashes, } = await import('node:crypto') - crypto.getRandomValues()
A convenient alias for webcrypto.getRandomValues.
- crypto.hash()
A utility for creating one-shot hash digests of data.
- crypto.hkdf()
HKDF is a simple key derivation function defined in RFC 5869\.
- crypto.hkdfSync()
Provides a synchronous HKDF key derivation function as defined in RFC 5869\.
- crypto.pbkdf2()
Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) implementation.
- crypto.pbkdf2Sync()
Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2) implementation.
- crypto.privateDecrypt()
Decrypts
bufferwithprivateKey. - crypto.privateEncrypt()
Encrypts
bufferwithprivateKey. - crypto.pseudoRandomBytes()
- crypto.publicDecrypt()
Decrypts
bufferwithkey.bufferwas previously encrypted using the corresponding private key, for example using privateEncrypt. - crypto.publicEncrypt()
Encrypts the content of
bufferwithkeyand returns a newBufferwith encrypted content. - crypto.randomBytes()
Generates cryptographically strong pseudorandom data.
- crypto.randomFill()
This function is similar to randomBytes but requires the first argument to be a
Bufferthat will be filled. - crypto.randomFillSync()
Synchronous version of randomFill.
- crypto.randomInt()
Return a random integer
nsuch thatmin <= n < max. - crypto.randomUUID()
Generates a random RFC 4122 version 4 UUID.
- crypto.randomUUIDv7()
Generates a random RFC 9562 version 7 UUID.
- crypto.scrypt()
Provides an asynchronous scrypt implementation.
- crypto.scryptSync()
Provides a synchronous scrypt implementation.
- crypto.secureHeapUsed()
- crypto.setEngine()
Load and set the
enginefor some or all OpenSSL functions (selected by flags). - crypto.setFips()
Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
- crypto.sign()
Calculates and returns the signature for
datausing the given private key and algorithm. - crypto.timingSafeEqual()
This function compares the underlying bytes that represent the given
ArrayBuffer,TypedArray, orDataViewinstances using a constant-time algorithm. - crypto.verify()
Verifies the given signature for
datausing the given key and algorithm.
Classes 11
- class crypto.Cipheriv
Instances of the
Cipherivclass are used to encrypt data. - class crypto.Decipheriv
Instances of the
Decipherivclass are used to decrypt data. - class crypto.DiffieHellman
The
DiffieHellmanclass is a utility for creating Diffie-Hellman key exchanges. - class crypto.DiffieHellmanGroup
The
DiffieHellmanGroupclass takes a well-known modp group as its argument. - class crypto.ECDH
The
ECDHclass is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) key exchanges. - class crypto.Hash
The
Hashclass is a utility for creating hash digests of data. - class crypto.Hmac
The
Hmacclass is a utility for creating cryptographic HMAC digests. - class crypto.KeyObject
Node.js uses a
KeyObjectclass to represent a symmetric or asymmetric key, and each kind of key exposes different functions. - class crypto.Sign
The
Signclass is a utility for generating signatures. - class crypto.Verify
The
Verifyclass is a utility for verifying signatures. - class crypto.X509Certificate
Encapsulates an X509 certificate and provides read-only access to its information.
Interfaces 78
- interface Argon2Parameters
- interface AsymmetricKeyDetails
- interface CheckPrimeOptions
- interface CipherAEADMethods
- interface CipherCCM
Instances of the
Cipherivclass are used to encrypt data. - interface CipherCCMOptions
- interface CipherChaCha20Poly1305
Instances of the
Cipherivclass are used to encrypt data. - interface CipherChaCha20Poly1305Options
- interface CipherGCM
Instances of the
Cipherivclass are used to encrypt data. - interface CipherGCMOptions
- interface CipherInfo
- interface CipherInfoOptions
- interface CipherOCB
Instances of the
Cipherivclass are used to encrypt data. - interface CipherOCBOptions
- interface DecipherAEADMethods
- interface DecipherCCM
Instances of the
Decipherivclass are used to decrypt data. - interface DecipherChaCha20Poly1305
Instances of the
Decipherivclass are used to decrypt data. - interface DecipherGCM
Instances of the
Decipherivclass are used to decrypt data. - interface DecipherOCB
Instances of the
Decipherivclass are used to decrypt data. - interface DHKeyPairOptions
- interface DiffieHellmanOptions
- interface DSAKeyPairOptions
- interface ECKeyPairOptions
- interface ED25519KeyPairOptions
- interface ED448KeyPairOptions
- interface GeneratePrimeOptions
- interface GeneratePrimeOptionsArrayBuffer
- interface GeneratePrimeOptionsBigInt
- interface HashOptions
- interface HmacOptions
- interface JsonWebKeyInput
- interface JwkKeyExportOptions
- interface KeyPairExportOptions
- interface KeyPairExportResult
- interface MLDSAKeyPairOptions
- interface MLKEMKeyPairOptions
- interface OneShotDigestOptions
- interface OneShotDigestOptionsWithBufferEncoding
- interface OneShotDigestOptionsWithStringEncoding
- interface PrivateKeyExportOptions
- interface PrivateKeyInput
- interface PublicDecryptJsonWebKeyInput
- interface PublicDecryptKeyObjectInput
- interface PublicDecryptOptions
- interface PublicDecryptPrivateKeyInput
- interface PublicDecryptPublicKeyInput
- interface PublicEncryptJsonWebKeyInput
- interface PublicEncryptKeyObjectInput
- interface PublicEncryptOptions
- interface PublicEncryptPrivateKeyInput
- interface PublicEncryptPublicKeyInput
- interface PublicKeyExportOptions
- interface PublicKeyInput
- interface RandomUUIDOptions
- interface RandomUUIDV7Options
- interface RawPrivateKeyExportOptions
- interface RawPrivateKeyInput
- interface RawPublicKeyExportOptions
- interface RawPublicKeyInput
- interface RSAKeyPairOptions
- interface RSAPSSKeyPairOptions
- interface ScryptOptions
- interface SecretKeyOptions
- interface SecureHeapUsage
- interface SigningOptions
- interface SignJsonWebKeyInput
- interface SignKeyObjectInput
- interface SignPrivateKeyInput
- interface SignRawPrivateKeyInput
- interface SLHDSAKeyPairOptions
- interface SymmetricKeyExportOptions
- interface VerifyJsonWebKeyInput
- interface VerifyKeyObjectInput
- interface VerifyPublicKeyInput
- interface VerifyRawPublicKeyInput
- interface X25519KeyPairOptions
- interface X448KeyPairOptions
- interface X509CheckOptions
Types 23
- type Argon2Algorithm
- type AsymmetricKeyType
- type BinaryLike
- type CipherCCMTypes
- type CipherChaCha20Poly1305Types
- type CipherGCMTypes
- type CipherMode
- type CipherOCBTypes
- type DSAEncoding
- type ECDHKeyFormat
- type KeyExportOptions
- type KeyExportResult
- type KeyFormat
- type KeyLike
- type KeyObjectType
- type KeyPairExportCallback
- type LargeNumberLike
- type MLDSAKeyType
- type MLKEMKeyType
- type PrivateKeyExportType
- type PublicKeyExportType
- type SLHDSAKeyType
- type UUID
Variables 2
- const crypto.subtle
A convenient alias for
crypto.webcrypto.subtle. - const crypto.webcrypto
An implementation of the Web Crypto API standard.
Namespaces 3
- namespace crypto.constants
An object containing commonly used constants for crypto and security related operations.
- namespace crypto.generateKeyPair
- namespace crypto.webcrypto
An implementation of the Web Crypto API standard.