Chapter 31: Node.js Crypto Module

1. What is the crypto module? (the honest explanation)

node:crypto is Node.js’s built-in cryptographic toolkit.

It gives you access to:

  • Hashing (SHA-256, SHA-512, MD5, etc.)
  • HMAC (keyed hashing)
  • Encryption / decryption (AES, ChaCha20, etc.)
  • Key generation (random bytes, UUIDs, prime numbers)
  • Digital signatures (RSA, ECDSA, Ed25519)
  • Diffie-Hellman key exchange
  • PBKDF2, scrypt (password hashing)
  • Web Crypto API compatibility (crypto.webcrypto)

Very important mindset in 2025–2026:

Never write your own cryptography algorithms Never use MD5 / SHA-1 for security purposes Never roll your own password hashing Never trust user input when dealing with crypto

The crypto module is very well audited, very fast (mostly C++ under the hood), and actively maintained.

2. Modern import style (2025–2026)

JavaScript

Always use the node: prefix — it makes it crystal clear this is core Node, not an npm package.

3. Most important & frequently used features

Let’s go through the ones you will use 90% of the time in real projects.

3.1 Hashing – createHash (most common)

JavaScript

Real-world pattern – file integrity check

JavaScript

3.2 HMAC – keyed hashing (very important for JWT, webhooks)

JavaScript

Real webhook verification pattern

JavaScript

Never use === for comparison — use timingSafeEqual to prevent timing attacks.

3.3 Password hashing – PBKDF2 or scrypt (must know)

Never store plain passwords or use md5/sha256 directly.

Modern recommendation 2026: use scrypt or argon2 (via npm)

But pbkdf2 is still very widely used and is built-in.

JavaScript

Promise version (modern)

JavaScript

Very common storage format (used by many frameworks)

text

3.4 Symmetric encryption – AES (very common)

JavaScript

Full encrypt + decrypt round-trip

JavaScript

5. Very common real-world patterns (2026)

Pattern 1 – Secure token / session ID generation

JavaScript

Pattern 2 – JWT signing (without jsonwebtoken library)

JavaScript

Pattern 3 – File integrity + authenticity (HMAC)

JavaScript

6. Security rules you must follow (2025–2026)

  • Never use MD5 or SHA-1 for security purposes
  • Never use crypto.randomBytes for keys if you can use crypto.generateKeyPair or crypto.subtle.generateKey
  • Always use authenticated encryption modes (GCM, ChaCha20-Poly1305)
  • Always use timingSafeEqual for comparing secrets / signatures
  • Never log keys, IVs, auth tags, or full ciphertexts
  • Never reuse IVs / nonces in AES-GCM
  • Use scrypt or Argon2 (via npm) for password hashing if possible — PBKDF2 is acceptable but slower

Summary – Quick cheat sheet

Task Recommended function / class Typical real-world example
Hash file / string createHash(‘sha256’) File integrity, caching keys
Keyed hash (webhooks, JWT) createHmac(‘sha256’, secret) Verify webhook signatures, JWT signing
Secure random bytes / tokens randomBytes(size) Session IDs, reset tokens, salts
Password hashing pbkdf2 / scrypt (or Argon2 via npm) User passwords
Symmetric encryption createCipheriv(‘aes-256-gcm’, …) Encrypt sensitive data at rest / in transit
Timing-safe comparison timingSafeEqual(a, b) Compare signatures, tokens, passwords
Generate UUID crypto.randomUUID() Database IDs, correlation IDs

Would you like to go much deeper into any specific area?

  • Full AES-GCM encrypt/decrypt round-trip with storage format
  • JWT signing & verification using only core crypto
  • Password hashing storage format & verification
  • File signing + integrity check system
  • Web Crypto API (crypto.subtle) vs legacy crypto
  • Common crypto attacks & how Node.js protects you

Just tell me which direction you want — I’ll continue with detailed, production-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *