If you're storing passwords, you need to hash them. Not encrypt — hash. And bcrypt is still one of the most sensible default choices for that job, despite being over two decades old. It's boring, well-tested, and available in every language you're likely to use. Boring is good when it comes to security.
Here's what you actually need to know to use it correctly.
What bcrypt actually is
Bcrypt is a password-hashing function built on the Blowfish cipher. Two things make it suitable for passwords specifically:
- It's deliberately slow. Fast hashes like SHA-256 are great for checksums and terrible for passwords, because an attacker with a stolen database can try billions of guesses per second. Bcrypt is tuned to be expensive on purpose.
- It has a built-in cost factor. You can crank up the work factor as hardware gets faster, so the same code stays resilient over time.
It also salts automatically. You don't manage salts yourself — bcrypt generates a random one per password and stores it inside the output string. That single detail removes a whole category of mistakes.
Reading a bcrypt hash
A bcrypt hash looks like this:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWBroken down:
$2b$— the algorithm version.12— the cost factor (2^12 = 4096 rounds).- The next 22 characters are the salt.
- The rest is the actual hash.
Because the salt and cost are embedded, verification needs nothing but the stored string and the plaintext attempt. No separate salt column, no config lookup.
When to use it (and when not to)
Use bcrypt for user passwords and any secret you need to verify but never recover. API tokens you generate yourself are usually better handled with a fast hash plus high entropy — bcrypt's slowness is wasted there since the input isn't guessable by humans.
Don't use bcrypt to encrypt data you need to read back. Hashing is one-way by design. If you ever find yourself wanting to "decrypt a bcrypt hash," you've misunderstood the tool.
Argon2 is the modern alternative and generally the recommended first pick for new systems, since it resists GPU and memory-based attacks more aggressively. But bcrypt remains a perfectly solid choice, and its ubiquity means fewer dependency headaches. If your framework ships bcrypt by default, you don't need to go hunting for anything else.
A worked example
Here's the full cycle in Node.js using the bcrypt package. The pattern is nearly identical in Python, PHP, Go, and Ruby.
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
// Registering a user
async function hashPassword(plain) {
return bcrypt.hash(plain, SALT_ROUNDS);
}
// Logging in
async function verifyPassword(plain, storedHash) {
return bcrypt.compare(plain, storedHash);
}
// Usage
const stored = await hashPassword('correct horse battery staple');
const ok = await verifyPassword('correct horse battery staple', stored);
console.log(ok); // trueNotice you never touch the salt and you never implement the comparison yourself. bcrypt.compare handles pulling the salt and cost out of the stored hash, re-hashing the attempt, and comparing in constant time.
If you just want to eyeball a hash or sanity-check a value during development, our Bcrypt Hash Generator lets you produce and verify hashes in the browser without writing throwaway scripts.
The gotchas that actually bite people
1. The 72-byte limit
Bcrypt silently truncates input after 72 bytes. If two passwords share the same first 72 bytes, they hash to the same value. This rarely matters for normal passwords, but it becomes a real vulnerability if you "pre-hash" with something that produces long, low-entropy output, or if you let users submit very long inputs expecting full coverage. A common safe pattern is to SHA-256 the password first (producing a fixed-length digest), then bcrypt that — but be aware this introduces its own subtleties around encoding. For most apps, just enforce a sane maximum length and move on.
2. Picking the wrong cost factor
Too low and it's cheap to crack. Too high and your login endpoint becomes a denial-of-service vector against yourself. Aim for roughly 250ms of work on your production hardware. Benchmark it; don't guess. A cost of 10–12 is a reasonable starting range in 2024, but measure on your actual servers.
3. Comparing hashes with ===
Always use the library's compare/verify function, never a plain string equality check. Those functions run in constant time to avoid timing attacks. Rolling your own comparison can leak information about how many characters matched.
4. Null byte handling
Some older bcrypt implementations truncate at the first null byte. Modern libraries generally handle this, but it's worth knowing your implementation's behavior if you accept binary or unusual input.
5. Forgetting to rehash on login
When you raise your cost factor, old hashes keep their old cost. A nice pattern: on successful login, check whether the stored hash used the current cost, and if not, rehash the plaintext (which you have in hand at that moment) and update the record. Over time your whole user base migrates transparently.
async function loginAndUpgrade(plain, user) {
const ok = await bcrypt.compare(plain, user.hash);
if (!ok) return false;
// Upgrade if the stored cost is below our current setting
const currentCost = bcrypt.getRounds(user.hash);
if (currentCost < SALT_ROUNDS) {
user.hash = await bcrypt.hash(plain, SALT_ROUNDS);
await user.save();
}
return true;
}The short version
Use bcrypt (or Argon2) for passwords. Let the library handle salting and comparison. Pick a cost factor by measuring, not by copying a number off the internet. Watch the 72-byte limit. Rehash on login when you bump the cost. Do those five things and you've avoided nearly every password-storage mistake that shows up in breach post-mortems.