Crypto Module in Node.js

Last Updated : 9 Mar, 2026

The Node.js crypto module provides cryptographic functionality used to secure data and protect information in applications and network communications.

  • The crypto module is used to perform cryptographic operations in Node.js.
  • Cryptography is an important part of network security.
  • Cryptography refers to the science of secret writing used to keep data confidential.
JavaScript
    // Importing crypto module
    const crypto = require('crypto');
    
    // Creating and initializing algorithm and password
    const algorithm = 'aes-192-cbc';
    const password = 'Password used to generate key';
    
    // Getting key for the cipher object
    const key = crypto.scryptSync(password, 'salt', 24);
    
    // Creating and initializing the static iv
    const iv = Buffer.alloc(16, 0);
    
    // Creating and initializing the cipher object
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    
    // Getting buffer value
    // by using final() method
    let value = cipher.final('hex');
    
    // Display the result
    console.log("buffer :- " + value);

Output:

buffer :- b9be42878310d599e4e49e040d1badb9

Methods of the Node.js Crypto Module

The Complete List of crypto module are listed below:

1. Cipher Methods

These methods are used to perform encryption operations using a cipher object.

  • cipher.final(): cipher.final() returns the buffer containing the final encrypted data of the cipher object.
  • cipher.update(): cipher.update() updates the cipher with data according to the given encoding format.

2. Decipher Methods

These methods are used to decrypt encrypted data using a decipher object.

  • decipher.update(): decipher.update() updates the decipher with encrypted data according to the encoding format.
  • decipher.final():  decipher.final() returns the buffer containing the final decrypted value of the decipher object.

3. Cipher and Decipher Creation

These methods are used to create objects required for performing encryption and decryption.

  • crypto.createCipheriv(): crypto.createCipheriv() creates a Cipher object using a specified algorithm, key, and initialization vector.
  • crypto.createDecipheriv(): crypto.createDecipheriv() creates a Decipher object using a specified algorithm, key, and initialization vector.

4. Encryption and Decryption

These methods are used to encrypt and decrypt data using public and private keys.

5. Hashing and Message Authentication

These methods are used to generate secure hashes and message authentication codes.

  • crypto.createHash(): crypto.createHash() creates a hash object used to generate hash digests using a specified algorithm.
  • crypto.createHmac(): crypto.createHmac() creates an HMAC object using a given algorithm and secret key.
  • crypto.getHashes(): crypto.getHashes() returns an array containing the names of all supported hash algorithms.

6. Random Data Generation

These methods generate cryptographically secure random data for security purposes.

7. Password-Based Key Derivation

These methods generate secure keys from passwords using key derivation algorithms.

  • crypto.pbkdf2(): crypto.pbkdf2() provides an asynchronous implementation of the PBKDF2 key derivation function.
  • crypto.pbkdf2Sync(): crypto.pbkdf2Sync() provides a synchronous implementation of PBKDF2.
  • crypto.scrypt(): crypto.scrypt() provides an asynchronous implementation of the scrypt key derivation algorithm.

8. Diffie-Hellman Key Exchange

These methods implement the Diffie-Hellman algorithm used for secure key exchange.

9. Elliptic Curve Cryptography

These methods support elliptic curve cryptography for secure communication.

  • crypto.createECDH(): crypto.createECDH() creates an Elliptic Curve Diffie-Hellman key exchange object.
  • crypto.getCurves(): crypto.getCurves() returns an array containing the names of supported elliptic curves.

10. Digital Signatures

These methods are used to create and verify digital signatures.

11. Key Generation

These methods generate asymmetric public and private key pairs.

12. Other Methods

These additional methods provide utility functions related to cryptography.

  • crypto.getCiphers(): crypto.getCiphers() returns an array containing the names of all supported cipher algorithms.
  • script.createCachedData(): script.createCachedData() creates cached data that can be used with the script constructor to improve performance.
Comment

Explore