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 51
- 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
Decipherobject 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.diffieHellman()
Computes the Diffie-Hellman secret based on a
privateKeyand apublicKey. - 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.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 9
- class crypto.Cipher
Instances of the
Cipherclass are used to encrypt data. - class crypto.Decipher
Instances of the
Decipherclass are used to decrypt data. - class crypto.DiffieHellman
The
DiffieHellmanclass is a utility for creating Diffie-Hellman key exchanges. - 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.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 59
- interface AsymmetricKeyDetails
- interface BasePrivateKeyEncodingOptions
- interface CheckPrimeOptions
- interface CipherCCM
Instances of the
Cipherclass are used to encrypt data. - interface CipherCCMOptions
- interface CipherChaCha20Poly1305
Instances of the
Cipherclass are used to encrypt data. - interface CipherChaCha20Poly1305Options
- interface CipherGCM
Instances of the
Cipherclass are used to encrypt data. - interface CipherGCMOptions
- interface CipherInfo
- interface CipherInfoOptions
- interface CipherOCB
Instances of the
Cipherclass are used to encrypt data. - interface CipherOCBOptions
- interface DecipherCCM
Instances of the
Decipherclass are used to decrypt data. - interface DecipherChaCha20Poly1305
Instances of the
Decipherclass are used to decrypt data. - interface DecipherGCM
Instances of the
Decipherclass are used to decrypt data. - interface DecipherOCB
Instances of the
Decipherclass are used to decrypt data. - interface DiffieHellmanGroupConstructor
- interface DSAKeyPairKeyObjectOptions
- interface DSAKeyPairOptions
- interface ECKeyPairKeyObjectOptions
- interface ECKeyPairOptions
- interface ED25519KeyPairKeyObjectOptions
- interface ED25519KeyPairOptions
- interface ED448KeyPairKeyObjectOptions
- interface ED448KeyPairOptions
- interface GeneratePrimeOptions
- interface GeneratePrimeOptionsArrayBuffer
- interface GeneratePrimeOptionsBigInt
- interface HashOptions
- interface JsonWebKey
- interface JsonWebKeyInput
- interface JwkKeyExportOptions
- interface KeyExportOptions
- interface KeyPairKeyObjectResult
- interface KeyPairSyncResult
- interface PrivateKeyInput
- interface PublicKeyInput
- interface RandomUUIDOptions
- interface RSAKeyPairKeyObjectOptions
- interface RSAKeyPairOptions
- interface RsaPrivateKey
- interface RSAPSSKeyPairKeyObjectOptions
- interface RSAPSSKeyPairOptions
- interface RsaPublicKey
- interface ScryptOptions
- interface SecureHeapUsage
- interface SigningOptions
- interface SignJsonWebKeyInput
- interface SignKeyObjectInput
- interface SignPrivateKeyInput
- interface VerifyJsonWebKeyInput
- interface VerifyKeyObjectInput
- interface VerifyPublicKeyInput
- interface X25519KeyPairKeyObjectOptions
- interface X25519KeyPairOptions
- interface X448KeyPairKeyObjectOptions
- interface X448KeyPairOptions
- interface X509CheckOptions
Types 20
- type BinaryLike
- type BinaryToTextEncoding
- type CharacterEncoding
- type CipherCCMTypes
- type CipherChaCha20Poly1305Types
- type CipherGCMTypes
- type CipherKey
- type CipherMode
- type CipherOCBTypes
- type DiffieHellmanGroup
- type DSAEncoding
- type ECDHKeyFormat
- type Encoding
- type KeyFormat
- type KeyLike
- type KeyObjectType
- type KeyType
- type LargeNumberLike
- type LegacyCharacterEncoding
- type UUID
Variables 3
- const crypto.DiffieHellmanGroup
The
DiffieHellmanGroupclass takes a well-known modp group as its argument. - 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
- namespace crypto.generateKeyPair
- namespace crypto.webcrypto
An implementation of the Web Crypto API standard.