DevNexus LogoDevNexus
ToolsBlogAbout
K
Browse Tools
HomeBlogHow To Hash Passwords Nodejs Bcrypt Vs Crypto
DevNexus LogoDevNexus

A free, open-source toolkit of developer utilities. Built by developers, for developers.

Tools

  • All Tools
  • Text Utilities
  • Encoders
  • Formatters

Resources

  • Blog
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Β© 2026 MyDevToolHub

Built with Next.js 16 + MongoDB Β· Crafted for developers

nodejs password hashingbcrypt vs cryptosecure authentication nodejspassword securitydeveloper guide hashing

How to Hash Passwords in Node.js: bcrypt vs crypto (Complete Developer Guide)

Learn how to securely hash passwords in Node.js using bcrypt and crypto. Compare both approaches with code examples and best practices.

DT
MyDevToolHub Team
Mar 18, 20265 min read

Related tools

Browse all tools
Hash GeneratorOpen hash-generator tool

Introduction

If you're building any application with user authentication, password security should be your top priority. One of the most critical steps is hashing passwords correctly.

In Node.js, developers commonly use two approaches:

  • bcrypt (recommended)
  • Node.js built-in crypto module

But which one should you use? And how do you implement them securely?

In this guide, you’ll learn:

  • How password hashing works in Node.js
  • bcrypt vs crypto comparison
  • Secure implementation with code examples
  • Common mistakes to avoid

You can also experiment with hashing using this tool:

πŸ‘‰ https://www.mydevtoolhub.com/tools/hash-generator


Why Hash Passwords?

Before jumping into code, let’s understand why hashing is necessary.

Without Hashing:

Code
{
  "email": "user@example.com",
  "password": "mypassword123"
}

If your database is leaked β†’ all passwords are exposed.

With Hashing:

Code
{
  "email": "user@example.com",
  "password": "$2b$10$Xyz..."
}

Now attackers cannot directly read passwords.


Option 1: Using bcrypt (Recommended)

What is bcrypt?

bcrypt is a library specifically designed for hashing passwords securely.

Why bcrypt is Preferred:

  • Built-in salting
  • Adjustable cost factor
  • Slow hashing (prevents brute force)

Install bcrypt

Code
npm install bcrypt

Example: Hashing Password with bcrypt

Code
const bcrypt = require('bcrypt');

async function hashPassword(password) {
  const saltRounds = 10;
  const hash = await bcrypt.hash(password, saltRounds);
  return hash;
}

hashPassword('myPassword123').then(console.log);

Example: Verifying Password

Code
const bcrypt = require('bcrypt');

async function verifyPassword(password, hash) {
  const isMatch = await bcrypt.compare(password, hash);
  return isMatch;
}

Option 2: Using Node.js crypto

What is crypto?

crypto is a built-in Node.js module for cryptographic operations.

It supports hashing algorithms like:

  • SHA-256
  • SHA-512

Example: Hashing with crypto

Code
const crypto = require('crypto');

function hashPassword(password) {
  return crypto
    .createHash('sha256')
    .update(password)
    .digest('hex');
}

console.log(hashPassword('myPassword123'));

Problem with crypto (Important)

While crypto is powerful, using it directly for passwords is risky.

Issues:

  • No built-in salting
  • Fast hashing β†’ vulnerable to brute force
  • Requires manual security handling

Improving crypto with Salt

Code
const crypto = require('crypto');

function hashPassword(password, salt) {
  return crypto
    .createHmac('sha256', salt)
    .update(password)
    .digest('hex');
}

Even with this, it's still not as secure as bcrypt.


bcrypt vs crypto (Detailed Comparison)

Featurebcryptcrypto (SHA-256)
Designed for Passwordsβœ… Yes❌ No
Saltingβœ… Built-in❌ Manual
SpeedSlow (secure)Fast (risky)
Brute Force ProtectionStrongWeak
Ease of UseEasyMedium

When Should You Use bcrypt?

Use bcrypt when:

  • Storing user passwords
  • Building authentication systems
  • Handling sensitive credentials

When Should You Use crypto?

Use crypto when:

  • Hashing files
  • Generating tokens
  • Creating signatures

Real-World Scenario

Scenario: Login System

❌ Using crypto (bad example):

  • Fast hashing β†’ attacker can brute force quickly

βœ… Using bcrypt:

  • Slow hashing β†’ attacker cannot easily crack passwords

Best Practices for Password Hashing

1. Always Use bcrypt or Argon2

Never use MD5 or plain SHA-256.


2. Use Proper Salt Rounds

Typical value: 10–12


3. Never Store Plain Passwords


4. Use HTTPS

Protect passwords during transmission.


5. Implement Rate Limiting

Prevent brute force attacks.


Performance Considerations

bcrypt is intentionally slow.

Why?

To make brute-force attacks expensive.

Example:

  • crypto hash β†’ milliseconds
  • bcrypt hash β†’ 100+ ms

Testing Hashes Quickly

Use this tool to understand hashing behavior:

πŸ‘‰ https://www.mydevtoolhub.com/tools/hash-generator


Common Developer Mistakes

  • Using SHA-256 directly for passwords
  • Not using salt
  • Storing passwords in logs
  • Using low cost factor

Advanced Tip: Async vs Sync

Always use async bcrypt functions in production.

Code
await bcrypt.hash(password, 10);

FAQs

Is bcrypt better than crypto?

Yes, for password hashing.

Can I use SHA-256 for passwords?

Not recommended alone.

What is salt in bcrypt?

Random data added automatically.

How many salt rounds should I use?

10–12 is standard.

Is bcrypt slow?

Yes, intentionally for security.

Should I use crypto at all?

Yes, but not for password hashing.


Final Summary

  • bcrypt = best for passwords
  • crypto = general-purpose hashing

If you're building authentication systems, always choose bcrypt.


Final Thoughts

Password security is not optionalβ€”it’s essential.

Using the wrong hashing method can expose millions of users.

Start experimenting with hashing using:

πŸ‘‰ https://www.mydevtoolhub.com/tools/hash-generator

Master bcrypt and secure coding practices to build safe and scalable applications.

On This Page

  • Introduction
  • Why Hash Passwords?
  • Without Hashing:
  • With Hashing:
  • Option 1: Using bcrypt (Recommended)
  • What is bcrypt?
  • Why bcrypt is Preferred:
  • Install bcrypt
  • Example: Hashing Password with bcrypt
  • Example: Verifying Password
  • Option 2: Using Node.js crypto
  • What is crypto?
  • Example: Hashing with crypto
  • Problem with crypto (Important)
  • Issues:
  • Improving crypto with Salt
  • bcrypt vs crypto (Detailed Comparison)
  • When Should You Use bcrypt?
  • When Should You Use crypto?
  • Real-World Scenario
  • Scenario: Login System
  • Best Practices for Password Hashing
  • 1. Always Use bcrypt or Argon2
  • 2. Use Proper Salt Rounds
  • 3. Never Store Plain Passwords
  • 4. Use HTTPS
  • 5. Implement Rate Limiting
  • Performance Considerations
  • Why?
  • Testing Hashes Quickly
  • Common Developer Mistakes
  • Advanced Tip: Async vs Sync
  • FAQs
  • Is bcrypt better than crypto?
  • Can I use SHA-256 for passwords?
  • What is salt in bcrypt?
  • How many salt rounds should I use?
  • Is bcrypt slow?
  • Should I use crypto at all?
  • Final Summary
  • Final Thoughts

You Might Also Like

All posts

Handling Special Characters, Unicode, and Spaces in URL Encoding (Advanced Guide for Developers)

Learn how to handle special characters, Unicode, emojis, and spaces in URL encoding with real examples and edge-case fixes.

Mar 18, 20267 min read

Debugging URL Encoding Issues in Production Applications (Advanced Developer Guide)

Learn how to debug URL encoding issues in production using logs, network tools, and advanced developer techniques.

Mar 18, 20267 min read

Real-World URL Encoding Examples Every Developer Should Know (Practical Guide)

Master URL encoding with real-world examples including forms, search queries, APIs, and redirects. A practical guide for developers.

Mar 18, 20267 min read