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