Every few months another breach leak hits the news, and someone pastes a list of plaintext passwords to the internet. The embarrassing part isn't usually the breach itself — databases get stolen. The embarrassing part is that the passwords weren't hashed properly. If you store user passwords, bcrypt is one of the simplest ways to not end up in that headline.
What bcrypt actually is
Bcrypt is a password-hashing function built on the Blowfish cipher. It was designed in 1999 specifically for hashing passwords, and that "specifically" is the whole point. Unlike general-purpose hashes like SHA-256 or MD5, bcrypt is deliberately slow and its cost is tunable.
That sounds backwards. We usually want things fast. But for password storage, speed is the enemy. If an attacker gets your database, they'll try to brute-force the hashes offline on GPUs that can churn through billions of SHA-256 guesses per second. Bcrypt's design makes each guess expensive, so a cracking run that would take minutes against SHA-256 takes years against bcrypt.
Two features make bcrypt good at this:
- A built-in salt. Every hash includes a random salt, so identical passwords produce different hashes. Rainbow tables are useless.
- A work factor (cost). You choose how many rounds of key expansion to run. Each increment doubles the work. As hardware gets faster, you bump the cost and stay ahead.
Reading a bcrypt hash
A bcrypt hash looks like gibberish, but it's structured. Here's one:
$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWBreaking that apart:
$2b$— the algorithm version.12— the cost factor (2^12 = 4096 iterations).- The next 22 characters are the salt.
- The rest is the actual hash.
Because the salt and cost are stored inside the string, you don't need a separate column for them. When you verify a password, the library reads the cost and salt back out of the stored hash and recomputes. That's why there's no "compare these two salts" step in your code.
When to use it (and when not to)
Use bcrypt for passwords. That's its job. If you're storing user login credentials and you don't have a strong reason to use something else, bcrypt is a safe, boring, well-understood default.
Don't use it for:
- Hashing files or large blobs. Bcrypt silently truncates input past 72 bytes. Use SHA-256 for integrity checks.
- API keys or session tokens. These are already high-entropy random values. A fast hash is fine, and you often need the speed.
- General data integrity. That's not what it's for.
The modern alternatives worth knowing are Argon2 (the Password Hashing Competition winner, now the recommendation if you're starting fresh) and scrypt. Both are memory-hard, which resists GPU attacks even better. But bcrypt is still perfectly acceptable, available in every language's standard ecosystem, and extremely hard to misuse. If your framework ships with bcrypt, using it is not a mistake.
A worked example
Here's the full round-trip in Node.js using the bcrypt package:
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
async function hashPassword(plain) {
// bcrypt generates the salt internally
return await bcrypt.hash(plain, SALT_ROUNDS);
}
async function checkPassword(plain, storedHash) {
return await bcrypt.compare(plain, storedHash);
}
// usage
const hash = await hashPassword('correct horse battery staple');
console.log(hash);
// $2b$12$...
const ok = await checkPassword('correct horse battery staple', hash);
console.log(ok); // trueNotice you never handle the salt yourself and never compare strings with ===. The compare function does a constant-time comparison and extracts everything it needs from the stored hash. That's the whole API, and keeping it that small is intentional.
If you just want to eyeball what a hash looks like, or generate one quickly to test a login form, our Bcrypt Hash Generator does it in the browser without you having to spin up a script.
Common gotchas
The 72-byte truncation
Bcrypt only reads the first 72 bytes of input. Anything beyond that is ignored. For normal passwords this never matters, but if you "pre-hash" long inputs or let users paste huge passphrases, two different passwords could collide. A common fix is to SHA-256 the password first and base64-encode the result before passing it to bcrypt — but only do this deliberately, and be consistent, because mixing hashed and unhashed inputs will lock people out.
Picking a cost factor
Cost 10 was the old default; 12 is a reasonable floor today. The right number depends on your hardware — you want a hash to take somewhere around 100–250ms. Too low and you're not slowing attackers; too high and your login endpoint becomes a self-inflicted denial of service. Benchmark on your production-equivalent server and pick accordingly.
Upgrading cost over time
Because the cost is baked into each stored hash, you can migrate gradually. On successful login, check whether the stored hash's cost is below your current target and, if so, re-hash the plaintext (which you have in hand at that moment) and update the record. Users get upgraded transparently as they log in.
Don't log the plaintext
Obvious, but it happens. A debug console.log or an error tracker capturing request bodies can leak passwords into logs that aren't nearly as protected as your database. Scrub password fields before anything gets logged.
Timing and comparison
Always use the library's compare/verify function rather than hashing the input and comparing strings yourself. The built-in comparison is constant-time, which avoids leaking information through how long the check takes.
The short version
Bcrypt is slow on purpose, salts automatically, and stores its own parameters so verification is trivial. Pick a cost around 12, call hash on signup and compare on login, never roll your own salting, and watch out for the 72-byte limit. Do that and you've handled password storage correctly — which puts you ahead of a surprising number of production systems out there.
Photo by Sasun Bughdaryan on Unsplash