Crypto Module in Node.js

From the Node.js Core Modules cheat sheet ยท Crypto & Buffer ยท verified Jul 2026

Crypto Module

Cryptographic functionality for hashing, encryption, and security

javascript
const crypto = require('crypto');

// Hash
const hash = crypto.createHash('sha256')
  .update('password')
  .digest('hex');

// Random
const token = crypto.randomBytes(32).toString('hex');

// Encrypt/Decrypt (createCipher removed in Node 22 - use createCipheriv)
const key = crypto.scryptSync('password', 'salt', 24);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-192-cbc', key, iv);
let encrypted = cipher.update('text', 'utf8', 'hex');
encrypted += cipher.final('hex');
๐ŸŸข Essential - Security and data integrity
๐Ÿ’ก Use crypto.randomBytes() for secure random values
โš ๏ธ Never use Math.random() for security
๐Ÿ“Œ bcrypt is better for password hashing
โšก Use streaming API for large files
๐Ÿ”— Related: jsonwebtoken for JWT tokens

More Node.js tasks

Back to the full Node.js Core Modules cheat sheet