KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
Node.jsNode.js Tutorial
Lesson

Node.js Crypto

10 min reading
Free Course

Node.js Crypto: Hashing, Tokens, HMAC, and Authenticated Encryption

The built-in node:crypto module provides battle-tested cryptographic primitives powered by OpenSSL, with zero third-party dependencies required. You can generate unpredictable session tokens, hash data with SHA-256, verify webhook HMAC signatures, and encrypt secrets with AES-256-GCM.

Random Tokens, SHA-256 Hashing, and HMAC Verification

Never use Math.random() for tokens, API keys, or reset links because its output is predictable. Use crypto.randomBytes() or crypto.randomUUID() instead. When verifying HMAC webhook signatures, compare digests with crypto.timingSafeEqual() so subtle timing differences never leak signature bytes.

import crypto from 'node:crypto';

// 1. Cryptographically secure token & UUID
const resetToken = crypto.randomBytes(32).toString('hex');
const requestId = crypto.randomUUID();

// 2. SHA-256 digest and HMAC-SHA256 signature verification
const payload = JSON.stringify({ orderId: 912, paid: true });
const webhookSecret = 'demo_webhook_signing_secret';

const signature = crypto.createHmac('sha256', webhookSecret).update(payload).digest('hex');
const expected = crypto.createHmac('sha256', webhookSecret).update(payload).digest('hex');

const isValid = crypto.timingSafeEqual(
  Buffer.from(signature, 'hex'),
  Buffer.from(expected, 'hex')
);

console.log({ requestId, tokenLength: resetToken.length, isValid });

Password Hashing vs Fast SHA-256

Do not use plain sha256 for user passwords—GPUs compute billions of fast SHA-256 hashes per second. Use slow, salted password-hashing functions like crypto.scrypt() (built into node:crypto) or argon2 so brute-force attacks remain infeasible.

Common Mistakes to Avoid

  • Reusing an IV (initialization vector) in AES encryption: Generate a fresh random 12-byte IV (crypto.randomBytes(12)) on every aes-256-gcm encryption and store it alongside the ciphertext and auth tag.
  • Using == or === to check API secrets or HMACs: Plain string comparison exits early on the first mismatched byte. Always use crypto.timingSafeEqual() with equal-length Buffers.
  • Using MD5 or SHA-1 for security checks: Both algorithms have known collision attacks. Standardize on SHA-256 or SHA-512 for integrity and HMACs.

Frequently Asked Questions

Does crypto.timingSafeEqual() throw if buffer lengths differ?

Yes. Always verify bufA.length === bufB.length first before calling timingSafeEqual(), or hash both values to fixed-length 32-byte SHA-256 digests first.

Should I store passwords with crypto.createHash('sha256')?

No. Always use crypto.scrypt() with a random 16-byte per-user salt (or Argon2id) so attackers cannot crack leaked hashes quickly.

When should I use AES-256-GCM instead of AES-256-CBC?

Prefer AES-256-GCM because it provides authenticated encryption, verifying both secrecy and that ciphertext was not tampered with.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js Streams
Continue JourneyNext: Node.js OS Module

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum