What Is Bcrypt
Bcrypt is a password hashing function based on the Blowfish cipher, designed by Niels Provos and David Mazières in 1999. It's widely regarded as the gold standard for password hashing and is used across the industry for password storage.
Unlike regular hashes (e.g., SHA-256), Bcrypt is purpose-built for password storage with built-in salt generation and adjustable cost factors.
Why You Need Bcrypt
- Resists brute force: Adjustable computation cost makes brute-forcing impractical
- Built-in salt: Automatically generates a random salt per hash, preventing rainbow table attacks
- Tunable cost factor: Increase computation cost as hardware improves
- Time-cost tradeoff: Legitimate logins take milliseconds, but attackers trying billions of passwords would take centuries
Bcrypt Hash Format
The Bcrypt hash format is:
$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
│ │ │ └─────────────────────────────────── 22-char salt
│ │ └────────────────────────────────────── 31-char hash
│ └───────────────────────────────────────── Cost factor (cost=10)
└──────────────────────────────────────────── Algorithm version (2a)
| Part | Description |
|------|-------------|
| $2a$ | Algorithm version (2a / 2b / 2y) |
| $10$ | Cost factor (2^10 = 1024 iterations) |
| N9qo8uLOickgx2ZMRZoMye | 22-character salt |
| IjZAgcfl7p92ldGxad68LJZdL17lhWy | 31-character hash |
Cost Factor
The cost factor determines Bcrypt's computation cost as a power of 2:
| Cost Factor | Iterations | Approximate Time (2024 hardware) | |-------------|-----------|--------------------------------| | 8 | 256 | ~30ms | | 10 | 1,024 | ~100ms | | 12 | 4,096 | ~300ms | | 14 | 16,384 | ~1.2s |
Recommendation: Choose a cost factor that makes user login take ~100-300ms. Current best practice: 10-12.
How to Use an Online Bcrypt Hash Tool
Using DevToolkit Pro's Bcrypt Hash Tool:
Generate Hash
- Enter the plaintext password
- Select a cost factor (default 10)
- Click Hash
- Copy the resulting Bcrypt hash
Verify Password
- Enter the Bcrypt hash
- Enter the password to verify
- Click Verify
- The tool indicates whether the password matches
Code Examples
Node.js
const bcrypt = require('bcrypt');
// Generate hash
const hash = await bcrypt.hash('myPassword123', 10);
// '$2a$10$N9qo8uLOickgx2ZMRZoMye...'
// Verify password
const isMatch = await bcrypt.compare('myPassword123', hash);
// true
Python
import bcrypt
# Generate hash
password = b'myPassword123'
hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=10))
# Verify password
is_match = bcrypt.checkpw(password, hashed)
# True
Go
import "golang.org/x/crypto/bcrypt"
// Generate hash
hash, err := bcrypt.GenerateFromPassword([]byte("myPassword123"), 10)
// Verify password
err = bcrypt.CompareHashAndPassword(hash, []byte("myPassword123"))
// nil if match
Bcrypt vs Other Hash Algorithms
| Algorithm | Speed | Built-in Salt | Cost Factor | Recommendation | |-----------|-------|---------------|-------------|----------------| | MD5 | Very fast | ❌ Manual | ❌ | ❌ Not recommended | | SHA-256 | Fast | ❌ Manual | ❌ | ❌ Not recommended | | Argon2 | Tunable | ✅ Built-in | ✅ | ✅ First choice for new projects | | Bcrypt | Tunable | ✅ Built-in | ✅ | ✅ Battle-tested, reliable |
Security Best Practices
- Never store plaintext passwords: Use Bcrypt or Argon2
- Don't limit password length: Bcrypt truncates at 72 bytes, but allow longer input
- Increase cost factor periodically: As hardware improves, raise the cost
- Re-hash on password change: Generate fresh hashes when users update passwords
- Use constant-time comparison: Use
timingSafeEqualto prevent timing attacks
FAQ
Which Is Better, Bcrypt or Argon2?
Argon2 won the 2015 Password Hashing Competition and is theoretically more secure (resists GPU/ASIC attacks). However, Bcrypt has 25+ years of battle-testing. Use Argon2 for new projects; no need to migrate existing Bcrypt implementations.
Why Does Bcrypt Produce Different Hashes Each Time?
Bcrypt automatically generates a random salt per hash. Even with the same input password, the output hash differs. Verification works by re-computing with the stored salt.
What Cost Factor Should I Choose?
Pick a value that makes user login take 100-300ms. In 2024, 10-12 is recommended. As hardware improves, this may need to increase.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
SHA-256 vs MD5: When to Use Which Hash Algorithm (2026)
SHA-256 vs MD5 compared: security, speed, and use cases. Learn when MD5 is still acceptable and where you must use SHA-256. Practical decision guide.
Online RSA Encryption/Decryption: Asymmetric Crypto and Digital Signatures
Learn RSA asymmetric encryption algorithm principles and applications. Understand public/private key encryption, digital signatures, key pair generation, and use in HTTPS and API authentication.
Complete Guide to MD5 Hash: How It Works, Uses, and Security
Deep dive into the MD5 algorithm, its common uses, and security vulnerabilities. Learn the differences between MD5, SHA-1, and SHA-256, and when MD5 is still okay to use.