What does the node:crypto module provide, and what's the difference between hashing and encryption?
Short answer
crypto provides hashing (createHash), symmetric/asymmetric encryption, HMAC signing, and secure random generation.
Advertisement
AModel answer
crypto provides hashing (createHash), symmetric/asymmetric encryption, HMAC signing, and secure random generation. Hashing is one-way, producing a fixed-size fingerprint that can't be reversed; encryption is reversible, transforming data into ciphertext decryptable back to plaintext given the correct key.
const hash = crypto.createHash('sha256').update('some data').digest('hex');
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);Real-world example
A file-integrity checker computes a SHA-256 hash to verify a download wasn't corrupted, while a separate feature encrypts stored API credentials with AES-256-GCM so they can be decrypted later.
Likely follow-ups
- Why is crypto.randomBytes() preferred over Math.random() for security-sensitive values?
- What does 'GCM' in AES-256-GCM provide beyond basic encryption?
Related topics