Crypto Module

Last Updated : Oct 2026


crypto module is used for hashing, encryption, decryption, generating random values, passwords, digital signatures, and more.

So let's understand what is meant by cryptography.

Imagine you have a message:

Hello Sadhu

You want to send this message to your friend, but you don't want other people to understand it if they see it.

Cryptography is basically a collection of techniques that help us:

  • 🔒 Hide information → encryption
  • 🔑 Prove/verify information → signatures
  • 🧾 Create a fingerprint of information → hashing
  • 🎲 Generate secure random values → random numbers

The Node.js crypto module gives you tools to do these things.

hashing?

A hash function takes that input and produces a seemingly random fixed-length value.It is a one-way function.You can't decrypt it.

hello
 ↓
HASH FUNCTION
 ↓
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

That long value is called a hash.If you change the input even slightly, you get a completely different hash.

hello
↓
2cf24dba...

hello!
↓
ce06092f...

Note: hello → hash but you aren't supposed to be able to do: hash → hello

encryption?

Encryption is a way to encrypt data,but is a two-way process.You can decrypt the data with the right key.

DATA
↓
ENCRYPTION + KEY
↓
ENCRYPTED DATA

And with the correct key:

ENCRYPTED DATA
↓
KEY
↓
ORIGINAL DATA

Implementation

Importing the crypto module using CommonJS:

const crypto = require("node:crypto");

Importing the crypto module using ESM:

import crypto from "node:crypto";

Some Common Methods are:

  • randomUUID : Generates a random UUID (Universally Unique Identifier) version 4.
const uuid = crypto.randomUUID();
console.log(uuid);
  • randomUUIDv7 : Generates a random UUID version 7.
const uuidv7 = crypto.randomUUIDv7();
console.log(uuidv7);
  • createHash : Creates a hash object with the specified algorithm like SHA-256, SHA-512, etc.
const hash = crypto.createHash("sha256");
  • update : Updates the hash with the given data.Hash Data is always same for the same data.
hash.update("Sadhu");
  • digest : Returns the hash as a Buffer,You can specify the output format (e.g., "hex", "base64", etc.). Default is "hex".
hash.digest("hex");
console.log(hash);
  • randomBytes : Generates a random byte array of the specified length.
const randomBytes = crypto.randomBytes(16);
console.log(randomBytes);
console.log(randomBytes.toString("hex"));
  • scrypt : Performs key derivation using the scrypt algorithm.Generally used for password hashing.We Can Pass salt and key length.
    • salt : A salt is random data added to the provided data before hashing.It make hashing more secure,because if the same data is hashed twice, the resulting hashes will be different.
const password = "myPassword123";
const salt = crypto.randomBytes(16);
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
  if (err) throw err;

  console.log("Hash Password:", derivedKey.toString("hex"));
});
  • createCipheriv and createDecipheriv : These methods are used for symmetric encryption and decryption.It uses AES algorithm for symmetric encryption.
const key = crypto.randomBytes(32); // secret key
const iv = crypto.randomBytes(16); //initialization vector

const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);

let encrypted = cipher.update("Hello Node.js", "utf8", "hex");
encrypted += cipher.final("hex");

console.log(encrypted);

const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);

let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");

console.log(decrypted);

Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR