Storing passwords is one of those tasks that looks trivial and ruins your week if you get it wrong. You never store the plain text. You never use plain SHA-256 either. For most applications, bcrypt remains a sensible, boring, well-understood default — and boring is exactly what you want in security code.
What bcrypt actually is
Bcrypt is a password hashing function built on the Blowfish cipher, designed specifically to be slow. That slowness is the point. A fast hash like MD5 or SHA-256 can be computed billions of times per second on a GPU, which makes brute-forcing leaked password databases cheap. Bcrypt deliberately burns CPU time per hash, so an attacker who steals your database can only guess a few thousand candidates per second instead of billions.
It also handles two things for you automatically:
- Salting. A random salt is generated for every password and embedded in the output. Identical passwords produce different hashes, so precomputed rainbow tables are useless.
- Cost factor. A configurable work factor lets you make hashing more expensive as hardware improves, without changing your code.
A bcrypt hash looks like this:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWEverything an attacker needs to verify a guess is in that string — and that's fine, because the whole design assumes the hash is public. The pieces are: the algorithm version ($2b$), the cost factor (12), then a combined salt and hash. You don't store the salt separately; it's baked in.
When to use it (and when not to)
Reach for bcrypt when you're storing user passwords and need to verify them later. That's the main use case.
Do not use bcrypt for:
- General-purpose hashing like deduplicating files or generating cache keys — use SHA-256, it's fast and that's what you want there.
- Hashing large inputs. Bcrypt only looks at the first 72 bytes of input. More on that below.
- API tokens or session IDs that you generate yourself. Those should be long random values; you can store them with a plain SHA-256 since they already have full entropy.
If you're starting a greenfield project and your platform supports it well, Argon2 is the more modern choice and won the Password Hashing Competition. But bcrypt is still perfectly acceptable, has mature libraries everywhere, and has no realistic known weaknesses for this purpose. Don't rewrite working bcrypt code just to chase fashion.
A short worked example
Here's the pattern in Node.js using the bcrypt package. The shape is nearly identical in every language.
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
// On signup
async function hashPassword(plain) {
return bcrypt.hash(plain, SALT_ROUNDS);
}
// On login
async function verifyPassword(plain, storedHash) {
return bcrypt.compare(plain, storedHash);
}
Notice you never regenerate the salt on login. bcrypt.compare reads the salt and cost straight out of the stored hash, re-hashes the submitted password with those parameters, and does a constant-time comparison. You just hand it the plain text and the stored string.
Want to see what a hash looks like before wiring up a library, or generate one quickly for a test fixture? Our Bcrypt Hash Generator lets you produce and inspect hashes in the browser so you can experiment with cost factors without writing a script.
Gotchas that bite people
The 72-byte limit
Bcrypt silently ignores everything past the first 72 bytes of input. For normal passwords this never matters. It becomes a real problem if you do something clever like pre-hashing or concatenating a long pepper onto the password — two different long passwords sharing a 72-byte prefix will hash identically. If you need to support arbitrarily long passwords, SHA-256 the input first (and base64-encode it to avoid null-byte truncation issues), then bcrypt the result.
Picking a cost factor
The cost factor is exponential: each increment doubles the work. A value of 12 is a reasonable 2024 default for server-side hashing, taking roughly 200–300ms on typical hardware. Benchmark on your production machines. Aim for something in the 250ms range — slow enough to frustrate attackers, fast enough that login doesn't feel broken. If you log in thousands of times a second you'll need to balance it against CPU budget.
Upgrading cost over time
Because the cost is stored in the hash, you can raise it later. On successful login, check whether the stored hash's cost is below your current target and, if so, re-hash the password (which you have in plain text at that moment) and update the record. This quietly migrates your users to stronger hashes without forcing resets.
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
}Don't invent your own scheme
No double-hashing, no custom salt-mixing, no XOR tricks. These usually weaken security rather than improve it. Use the library's hash and compare functions as intended.
Timing attacks
Always compare with the library's compare function, never with == on the hash strings. The library does a constant-time comparison; a naive equality check can leak information through response timing.
Storage and column sizing
A bcrypt hash is always 60 characters. A common database bug is defining the column as VARCHAR(32) or similar and silently truncating the hash, which breaks every login afterwards. Use at least CHAR(60) or VARCHAR(72) to leave headroom. Store the full string exactly as returned — version, cost, salt, and all.
The short version
- Use bcrypt for passwords, SHA-256 for everything else.
- Let the library handle salting; never roll your own.
- Default to a cost of 12 and benchmark it.
- Watch the 72-byte limit and the 60-character storage requirement.
- Use
compare, not string equality.
Get those five right and your password storage is in better shape than a large chunk of the software running in production today.
Photo by Jake Walker on Unsplash