🔒 Bcrypt

生成和验证 Bcrypt 密码哈希

🔐 生成哈希
计算轮数 (Cost Factor)
✅ 验证密码

What is Bcrypt?

Bcrypt is a password hashing algorithm designed specifically for password storage, created by Niels Provos and David Mazières and presented at the 1999 USENIX conference. Based on the Blowfish cipher, it incorporates salt and a configurable cost factor, making brute-force and rainbow table attacks extremely difficult.

Unlike general-purpose hash algorithms like MD5 or SHA-256, Bcrypt's core design philosophy is deliberately slow. A few hundred milliseconds of delay is imperceptible for user login, but catastrophic for attackers attempting large-scale brute-force attacks — they can only try a handful of password combinations per second.

Core Features of Bcrypt

  • Built-in Salt: A random salt is automatically included with every hash, requiring no manual management. Even two users with the same password will produce completely different hashes.
  • Configurable Cost Factor: The number in $2a$10$... (e.g., 10) controls computational intensity. Each increment doubles the work. This allows Bcrypt to "scale up" security as hardware improves over time.
  • GPU/ASIC Resistance: Bcrypt requires significant memory access, dramatically reducing the effectiveness of GPU and specialized hardware acceleration.
  • Fixed Output Format: Always produces a 60-character hash string in the format $2a$rounds$22-char-salt31-char-hash.
  • One-Way Irreversibility: Hashes can only be generated from plaintext, never reversed. Verification requires re-hashing the candidate text and comparing.

Understanding Bcrypt Hash Format

A typical Bcrypt hash looks like this:

$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy

  • $2a$: Algorithm version identifier (variants like $2b$, $2y$ fix edge cases).
  • 10: Cost factor, meaning 2^10 = 1024 rounds of computation.
  • N9qo8uLOickgx2ZMRZoMye: 22-character salt (128 bits, Base64 encoded).
  • IjZAgcfl7p92ldGxad68LJZdL17lhWy: 31-character hash result (184 bits, Base64 encoded).

How to Choose a Cost Factor?

The cost factor determines Bcrypt's computational complexity. Choosing the right value requires balancing security and user experience:

Cost FactorRoundsTypical TimeUse Case
416~1 msTesting only, not for production
664~4 msLow security requirements
8256~16 msGeneral applications
101024~65 msRecommended default
124096~260 msHigh security requirements
1416384~1 secondVery high security

Recommendation: For most web applications, a cost factor of 10 is an excellent starting point. Increase it gradually as server performance improves. A practical rule of thumb: tune the cost factor so a single hash takes 250-500ms — secure enough while remaining imperceptible to users.

Bcrypt vs Other Password Hashing Algorithms

AlgorithmCharacteristicsRecommendation
BcryptBattle-tested (1999), memory-intensive, GPU-resistant, mature ecosystem⭐⭐⭐⭐⭐
Argon2Winner of 2015 Password Hashing Competition, tunable memory/time/parallelism⭐⭐⭐⭐⭐
PBKDF2NIST recommended, HMAC-based, tunable iterations⭐⭐⭐⭐
ScryptMemory-intensive, but complex parameter tuning⭐⭐⭐⭐
SHA-256 (direct)Too fast, extremely vulnerable to brute-force — never use for passwords
MD5 (direct)Completely broken — absolutely never use for passwords❌❌

Common Bcrypt Use Cases

  • Web App User Authentication: Node.js (Express), Python (Django/Flask), Ruby on Rails, PHP (Laravel) and many frameworks use Bcrypt as the default password hashing scheme.
  • API Key Storage: Store the hash of API keys in the database, recompute and compare on verification.
  • Database Password Columns: Store only Bcrypt hashes in the database. Even if the database is breached, attackers cannot directly obtain plaintext passwords.
  • Configuration File Protection: Avoid writing plaintext passwords in configuration files.

Bcrypt Best Practices

  • Never implement hashing yourself: Use well-audited, mature libraries such as Node.js bcrypt or bcryptjs, Python bcrypt, or Go golang.org/x/crypto/bcrypt.
  • Choose an appropriate cost factor: Test different cost factors on your server before deployment and select the highest value that meets your security needs.
  • Use constant-time comparison: Always use the library's compare function for password verification, never use === directly, to prevent timing attacks.
  • Use a new random salt per hash: Bcrypt handles this automatically — no extra work needed.
  • Limit password length: Bcrypt processes a maximum of 72 bytes of input (71 characters with null terminator). For longer passwords, pre-hash with SHA-256 first.
  • Upgrade the cost factor periodically: Increase the cost factor over time as hardware evolves to maintain security strength.

Why Use an Online Bcrypt Tool?

In daily development, developers often need to quickly generate Bcrypt hashes for test data, verify password migration schemes, or debug authentication flows. Our online Bcrypt tool offers one-click hash generation and password verification with adjustable cost factors from 4 to 14, running entirely in the browser (using the bcryptjs library). No data is ever uploaded to any server — it's safe and reliable. No installation required — just open your browser. An essential tool in every backend developer's toolkit.