生成和验证 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.
$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.$2a$rounds$22-char-salt31-char-hash.A typical Bcrypt hash looks like this:
$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
The cost factor determines Bcrypt's computational complexity. Choosing the right value requires balancing security and user experience:
| Cost Factor | Rounds | Typical Time | Use Case |
|---|---|---|---|
| 4 | 16 | ~1 ms | Testing only, not for production |
| 6 | 64 | ~4 ms | Low security requirements |
| 8 | 256 | ~16 ms | General applications |
| 10 | 1024 | ~65 ms | Recommended default |
| 12 | 4096 | ~260 ms | High security requirements |
| 14 | 16384 | ~1 second | Very 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.
| Algorithm | Characteristics | Recommendation |
|---|---|---|
| Bcrypt | Battle-tested (1999), memory-intensive, GPU-resistant, mature ecosystem | ⭐⭐⭐⭐⭐ |
| Argon2 | Winner of 2015 Password Hashing Competition, tunable memory/time/parallelism | ⭐⭐⭐⭐⭐ |
| PBKDF2 | NIST recommended, HMAC-based, tunable iterations | ⭐⭐⭐⭐ |
| Scrypt | Memory-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 | ❌❌ |
bcrypt or bcryptjs, Python bcrypt, or Go golang.org/x/crypto/bcrypt.compare function for password verification, never use === directly, to prevent timing attacks.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.