If you're storing user passwords, you don't store passwords. You store the output of a slow, salted hash function. bcrypt has been the sensible default for that job since 1999, and despite its age it's still a solid pick in 2024. Let me walk through what it does, why it works, and the mistakes I keep seeing in code reviews.
What bcrypt actually is
bcrypt is a password hashing function built on the Blowfish cipher. The important word there is slow. Unlike SHA-256 or MD5, which are designed to be fast, bcrypt is deliberately expensive to compute. That's the whole point. If an attacker steals your database, you want each guess to cost them real time.
Two features matter most:
- Built-in salting. bcrypt generates a random salt for every password and stores it inside the hash output. You don't manage salts separately.
- A tunable cost factor. You pick a "work factor" (also called rounds). Higher means slower. As hardware gets faster, you bump the number.
A bcrypt hash looks like this:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWReading it left to right: $2b$ is the algorithm version, 12 is the cost factor, and the rest is the 22-character salt followed by the hash. Everything you need to verify a password later lives in that one string. No separate salt column required.
When to use it
Reach for bcrypt whenever you're authenticating humans with passwords. Login forms, admin panels, API keys that users type — anything where the secret is low-entropy and chosen by a person.
Don't use it for:
- Hashing large files or verifying data integrity. Use SHA-256 for that. bcrypt caps input at 72 bytes.
- High-entropy tokens. A randomly generated 256-bit API token doesn't need a slow hash; a fast HMAC or SHA-256 is fine because brute force isn't feasible anyway.
Argon2 is the newer, arguably stronger option and won the Password Hashing Competition. If your language has a well-maintained Argon2 library, it's a great choice. But bcrypt remains battle-tested, available everywhere, and hard to misconfigure catastrophically. For most teams it's the pragmatic default.
A worked example
Here's the typical flow in Node.js using the bcrypt package. The pattern is nearly identical in every language.
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
// On signup: hash and store
async function hashPassword(plain) {
return await bcrypt.hash(plain, SALT_ROUNDS);
}
// On login: compare
async function checkPassword(plain, storedHash) {
return await bcrypt.compare(plain, storedHash);
}
// usage
const hash = await hashPassword('correct horse battery staple');
const ok = await checkPassword('correct horse battery staple', hash);
console.log(ok); // trueNotice there's no salt handling anywhere. bcrypt.hash generates one and embeds it. bcrypt.compare reads the salt and cost back out of the stored hash, re-runs the computation, and does a constant-time comparison. You just pass the plaintext and the stored string.
If you want to see what a hash looks like for a given input without wiring up a project, our Bcrypt Hash Generator lets you tweak the cost factor and inspect the output directly in the browser — handy for debugging or seeding test data.
Common gotchas
The 72-byte limit
bcrypt silently truncates input beyond 72 bytes. If your users have very long passphrases, everything after byte 72 is ignored. Two different 80-character passwords sharing the first 72 bytes will verify against each other. The usual fix is to pre-hash with SHA-256 and base64-encode the result before passing it to bcrypt, giving a fixed-length input well under the limit. Do this consistently on both signup and login.
Picking a cost factor
Cost is exponential: each increment roughly doubles the work. A good target is for a single hash to take around 250ms on your production hardware. Benchmark it:
console.time('bcrypt');
await bcrypt.hash('test', 12);
console.timeEnd('bcrypt');If 12 is too fast, go to 13. If your login endpoint feels sluggish under load, you may be too high. There's a genuine trade-off: higher cost protects users better but also makes your own servers do more work on every login, which can be a denial-of-service vector if you're not careful.
Upgrading hashes over time
Hardware improves, so a cost of 10 from five years ago is weak today. You can't re-hash a password you don't have in plaintext. The standard approach is opportunistic rehashing: when a user logs in successfully, check the cost factor stored in their hash. If it's below your current target, re-hash the plaintext they just gave you and update the database.
if (ok) {
const rounds = bcrypt.getRounds(storedHash);
if (rounds < SALT_ROUNDS) {
const newHash = await bcrypt.hash(plain, SALT_ROUNDS);
await saveHash(user.id, newHash);
}
}Don't roll your own comparison
Always use the library's compare function. Comparing hashes with === is both wrong (you'd need to re-derive the salt) and vulnerable to timing attacks. The built-in comparison is constant-time for a reason.
Use async in request handlers
The whole point of bcrypt is that it's slow. In a single-threaded runtime like Node, the synchronous hashSync will block your event loop for a quarter of a second — awful under concurrent logins. Use the async versions so the work happens off the main thread.
Quick checklist
- Use bcrypt (or Argon2) for user passwords, never MD5 or plain SHA.
- Let the library handle salting — don't invent your own scheme.
- Pick a cost factor targeting ~250ms, and benchmark on real hardware.
- Handle the 72-byte limit if long passwords are possible.
- Rehash on login when the stored cost is below your current standard.
- Always use the constant-time compare, always async.
Password storage is one of those areas where boring, well-understood tools win. bcrypt isn't flashy, but it's forgiving and it works. Get the cost factor right, mind the input limit, and you've handled the part of auth that most often shows up in breach headlines.