Storing passwords is one of those tasks where "good enough" isn't good enough. Get it wrong and a single database leak turns into mass account takeover. bcrypt has been the sensible default for password hashing for over two decades, and it's still a solid choice today. Here's what it actually does and how to use it without shooting yourself in the foot.
What bcrypt actually is
bcrypt is a password-hashing function built on the Blowfish cipher. Unlike general-purpose hashes like SHA-256, it's deliberately slow. That slowness is the point. An attacker who steals your database has to spend real compute time per guess, which makes brute-forcing millions of passwords far more expensive.
Two features make bcrypt good at its job:
- A built-in salt. Every hash includes a random salt, so two users with the same password get completely different hashes. This kills rainbow-table attacks.
- A tunable work factor (cost). You pick how many rounds of key expansion run. Each increment doubles the work. As hardware gets faster, you bump the cost and stay ahead.
A bcrypt hash is self-describing. Look at one and you can read off the parameters:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW
$2b$ → algorithm version
12 → cost factor (2^12 = 4096 iterations)
R9h… → 22-char salt followed by the 31-char hashBecause the salt and cost live inside the string, you only need to store that one column. No separate salt field, no juggling.
When to use it (and when not to)
Reach for bcrypt whenever you're storing user passwords in a database. That's the core use case and it handles it well.
Don't use it for things it isn't meant for:
- Not for hashing large data or files. bcrypt truncates input past 72 bytes — more on that below. Use SHA-256 for integrity checks.
- Not for API tokens or session IDs. Those should be long random values; you don't need slow hashing for high-entropy secrets.
- Not for encryption. Hashing is one-way. If you need to get the original value back, you want encryption, not bcrypt.
If you're starting a greenfield project and want the current state of the art, Argon2 is worth a look — it won the Password Hashing Competition and resists GPU attacks better. But bcrypt is battle-tested, available in every language, and perfectly acceptable for the vast majority of apps. Don't let perfect be the enemy of shipped.
Common gotchas
The 72-byte truncation
bcrypt only looks at the first 72 bytes of input. Anything beyond that is silently ignored. For normal passwords this never matters, but it causes two subtle bugs. First, extremely long passphrases may collide if they share a 72-byte prefix. Second, if you pre-hash passwords (a common pattern to get around the limit) with something like SHA-256 that outputs raw bytes containing null characters, some bcrypt implementations stop at the null byte and effectively weaken the hash. If you pre-hash, base64-encode the result first.
Picking the wrong cost
A cost that's too low is insecure; too high and your login endpoint becomes a self-inflicted denial of service. Aim for a hash taking roughly 250–500ms on your production hardware. Benchmark it. On modern servers, a cost of 12 is a reasonable starting point, but measure rather than guess, and revisit it every year or two.
Comparing hashes wrong
Never compare the stored hash to a freshly computed one with a plain string equality check, and never try to re-hash a plaintext with a new salt and expect it to match. Always use the library's verify/compare function — it extracts the salt and cost from the stored hash and does the comparison in constant time to avoid timing leaks.
Logging plaintext
It sounds obvious, but it happens constantly: request bodies get logged, including the password field. Scrub sensitive fields before anything hits your logs.
A worked example
Here's the full cycle in Node.js using the bcrypt package. The same shape applies in Python, Go, PHP, and everywhere else.
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
// At signup: hash the password before storing it
async function hashPassword(plain) {
return await bcrypt.hash(plain, SALT_ROUNDS);
}
// At login: compare the submitted password to the stored hash
async function verifyPassword(plain, storedHash) {
return await bcrypt.compare(plain, storedHash);
}
// Usage
const hash = await hashPassword('correct horse battery staple');
// store `hash` in the users table
const ok = await verifyPassword('correct horse battery staple', hash);
console.log(ok); // trueNotice there's no manual salt handling and no separate column. bcrypt.hash generates the salt, bcrypt.compare reads it back out. That simplicity is a feature — fewer moving parts means fewer ways to get it wrong.
If you just want to eyeball what a bcrypt hash looks like, test a login flow against known values, or generate a hash for a seed script without wiring up code, our Bcrypt Hash Generator lets you produce and verify hashes right in the browser. It's handy for quick checks during development.
Upgrading cost over time
Because the cost is baked into each stored hash, you can migrate gradually. When a user logs in successfully, check the cost of their stored hash. If it's below your current target, re-hash their password (which you have in plaintext at that moment) and update the record:
async function loginAndMaybeUpgrade(plain, storedHash) {
const ok = await bcrypt.compare(plain, storedHash);
if (!ok) return null;
const currentCost = bcrypt.getRounds(storedHash);
if (currentCost < SALT_ROUNDS) {
const newHash = await bcrypt.hash(plain, SALT_ROUNDS);
// persist newHash
}
return 'authenticated';
}Over a few weeks of normal logins, most active accounts quietly migrate to the stronger cost with zero disruption.
The short version
Use bcrypt for passwords. Let the library handle salting. Pick a cost that takes a few hundred milliseconds and bump it over time. Always verify with the library's compare function, mind the 72-byte limit, and keep plaintext out of your logs. Do those things and your password storage will be the least interesting part of your next security review — which is exactly where you want it to be.
Photo by Jake Walker on Unsplash