If you're storing user passwords, you should not be storing them. You should be storing a one-way hash that an attacker can't reverse even if they dump your whole database. Bcrypt is one of the oldest and most trusted tools for that job, and despite being over 20 years old, it's still a perfectly reasonable default.
What bcrypt actually is
Bcrypt is a password-hashing function built on the Blowfish cipher. The key thing that makes it good for passwords isn't that it's clever cryptography — it's that it's deliberately slow. Fast hash functions like MD5 or SHA-256 are great for checksums, but terrible for passwords because an attacker with a GPU can try billions of guesses per second.
Bcrypt fixes this with a tunable work factor (also called the cost). Each increment of the cost doubles the time it takes to compute a hash. That means you can keep hashing expensive enough to frustrate attackers while remaining fast enough for a login request.
Salts come built in
One of the nicest things about bcrypt is that it generates a random salt automatically and stores it inside the output. You don't have to manage a separate salt column. A bcrypt hash looks like this:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWReading left to right: $2b$ is the algorithm version, 12 is the cost factor, the next 22 characters are the salt, and the rest is the actual hash. Because the salt lives in the string, verifying a password later is trivial — bcrypt reads the salt and cost straight out of the stored value.
When to use it (and when not to)
Reach for bcrypt whenever you need to store passwords or other low-entropy secrets that humans choose. It's well-supported in just about every language and framework, which matters more than people admit — boring and battle-tested beats shiny.
There are cases where you might pick something else:
- Argon2 is the modern winner of the Password Hashing Competition and is memory-hard, which gives better resistance to GPU and ASIC attacks. If you're starting fresh and your platform has a solid Argon2 library, it's a strong choice.
- scrypt is another memory-hard option you'll see in the wild.
- For hashing high-entropy secrets like API tokens or random session keys, you don't need a slow hash at all — a plain SHA-256 is fine, because there's nothing to brute-force.
But if you want something that's universally available, easy to get right, and has survived decades of scrutiny, bcrypt remains a solid default.
Common gotchas
The 72-byte limit
Bcrypt only looks at the first 72 bytes of the input. Anything beyond that is silently ignored. This usually doesn't matter, but it bites you if you try to pre-hash or concatenate things. If you want to support arbitrarily long passwords, a common pattern is to run the password through SHA-256 first and base64-encode it before passing it to bcrypt — but only do this if you understand why, and be consistent about it.
Picking the cost factor
A cost of 10 was the classic default, but hardware has moved on. Something in the range of 12 is a sensible modern baseline for interactive logins. Benchmark it on your production hardware: you want a single hash to take somewhere around 100–250ms. Too low and you're not protecting anyone; too high and login becomes sluggish and you open yourself to denial-of-service.
Never roll your own comparison
Always use the library's verify/compare function. It handles reading the salt and cost from the stored hash, and it does a constant-time comparison to avoid timing attacks. Don't hash the input and compare strings yourself.
Don't store the cost separately
You don't need to. The hash string already contains everything needed to verify it. This also makes upgrading easy — you can bump the cost for new users while old hashes keep working.
A worked example
Here's what it looks like in Node.js with the bcrypt package. The same shape applies in almost any language.
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 hash = await hashPassword('correct horse battery staple');
console.log(hash);
// $2b$12$...
const ok = await verifyPassword('correct horse battery staple', hash);
console.log(ok); // trueIn Python with passlib it's just as clean:
from passlib.hash import bcrypt
hashed = bcrypt.using(rounds=12).hash("s3cret")
bcrypt.verify("s3cret", hashed) # TrueNotice there's no salt handling anywhere. That's the point — the library does it for you, and trying to be clever is how bugs creep in.
Trying it quickly
If you just want to see what a bcrypt hash looks like, experiment with different cost factors, or verify a value against a stored hash without wiring up code, our Bcrypt Hash Generator does exactly that in the browser. It's handy for debugging a login flow or sanity-checking that your backend is producing the format you expect.
Upgrading hashes over time
Because the cost is baked into every hash, you can migrate gradually. A good pattern: on a successful login, check whether the stored hash used an outdated cost. If it did, re-hash the password (which you have in plaintext at that exact moment) with the new cost and save it. Over time your database upgrades itself without ever forcing a password reset.
if (await bcrypt.compare(plain, storedHash)) {
if (bcrypt.getRounds(storedHash) < SALT_ROUNDS) {
const newHash = await bcrypt.hash(plain, SALT_ROUNDS);
await saveHash(user.id, newHash);
}
// proceed with login
}The short version
Use a dedicated password-hashing function — bcrypt is a fine one. Let the library handle salting and comparison. Set a cost factor that's slow enough to hurt attackers but fast enough for users, around 12 on modern hardware. Watch the 72-byte limit. And plan for upgrading the cost down the line. Get those few things right and you've avoided the overwhelming majority of password-storage mistakes.
Photo by Jake Walker on Unsplash