Storing passwords is one of those tasks that looks trivial until you get it wrong and end up in a breach disclosure email. The rule is simple: never store a password you can read back. You store a hash, and when someone logs in you hash their input and compare. Bcrypt has been doing this job well since 1999, and it's still a sensible default.
What bcrypt actually is
Bcrypt is a password hashing function built on the Blowfish cipher. The important part isn't the cipher, though — it's that bcrypt is deliberately slow and includes a tunable cost factor. Fast hashes like MD5 or SHA-256 are great for checksums and terrible for passwords, because an attacker with a leaked database can try billions of guesses per second on a GPU. Bcrypt is designed to resist exactly that.
Three features matter for day-to-day use:
- Built-in salt. Bcrypt generates a random salt per password and stores it inside the hash string. You don't manage salts yourself.
- Adjustable work factor. The cost (or "rounds") parameter controls how expensive hashing is. As hardware gets faster, you bump the number.
- Self-describing output. The resulting string contains the algorithm version, cost, salt, and hash — everything you need to verify later.
Reading a bcrypt hash
A bcrypt hash looks like this:
$2b$12$Dwt1BZj6pcyc3Dy1FWZ-lu...Break it down:
$2b$— the algorithm variant.12— the cost factor (212 = 4096 iterations).- The next 22 characters are the salt.
- The rest is the actual hash.
Because the salt and cost travel with the hash, verification needs only the stored string and the plaintext guess. No separate columns, no extra bookkeeping.
When to use it (and when not to)
Reach for bcrypt whenever you're storing user passwords in a typical web or API application. It's widely supported, battle-tested, and available in every mainstream language.
There are cases where you might pick something else. Argon2 (specifically Argon2id) won the Password Hashing Competition and is memory-hard, which gives it a stronger edge against GPU and ASIC attacks. If you're starting fresh and have a solid Argon2 library available, it's a great choice. Scrypt is another memory-hard option. But bcrypt remains perfectly acceptable, and "use the well-reviewed library you already have" beats "roll something clever."
Do not use bcrypt for general-purpose hashing — file integrity, cache keys, or anything that needs to be fast. Use SHA-256 or similar there. Bcrypt's slowness is a feature only for passwords.
A worked example
Here's the pattern in Node.js using the bcrypt package. The shape is nearly identical in other languages.
const bcrypt = require('bcrypt');
const SALT_ROUNDS = 12;
// When a user registers or changes their password
async function hashPassword(plain) {
return bcrypt.hash(plain, SALT_ROUNDS);
}
// When a user logs in
async function verifyPassword(plain, storedHash) {
return bcrypt.compare(plain, storedHash);
}
// Usage
const hash = await hashPassword('correct horse battery staple');
const ok = await verifyPassword('correct horse battery staple', hash);
console.log(ok); // trueNotice there's no manual salt handling and no custom comparison. bcrypt.compare extracts the salt and cost from the stored hash, re-hashes the input, and does a constant-time comparison. That last point matters — never compare hashes with === or a plain string equality check, because timing differences can leak information.
If you want to see what a bcrypt hash looks like for a given input, or quickly generate one for testing, you can use our Bcrypt Hash Generator in the browser instead of spinning up a script.
Common gotchas
The 72-byte limit
Bcrypt only looks at the first 72 bytes of input. Anything beyond that is silently ignored. For most passwords this never matters, but it becomes a real bug if you do something like pre-hashing with a long token, or if you support very long passphrases. A common pattern to avoid the truncation surprise is to SHA-256 the password first (producing a fixed-length digest), then bcrypt that — but be aware of null-byte handling if you feed raw binary in. Keep it simple unless you have a specific reason.
Picking the cost factor
A cost of 10 was fine years ago; 12 is a reasonable modern baseline. The right number depends on your hardware. Aim for hashing to take roughly 200–300 milliseconds on your production server. Too low and you're not slowing attackers enough. Too high and your login endpoint becomes a self-inflicted denial of service under load. Benchmark it:
console.time('hash');
await bcrypt.hash('test', 12);
console.timeEnd('hash');Upgrading cost over time
Because the cost is baked into each hash, you can migrate gradually. On successful login, check whether the stored hash uses your current cost. If it's lower, re-hash the plaintext (which you have at that moment) and update the record. Over time your whole user base rolls forward without forcing a password reset.
Blocking the event loop
Bcrypt is CPU-intensive by design. In single-threaded runtimes like Node, use the async API so you don't stall everything else. Many libraries run the work on a thread pool for you — the synchronous versions exist mostly for scripts and tests.
Don't log the plaintext
Obvious, but it happens. Make sure your request logging, error reporting, and debugging middleware never capture the raw password field. Scrub it early.
A quick checklist
- Use a maintained bcrypt library — never implement it yourself.
- Let the library generate and store the salt.
- Set cost to about 12 and benchmark it on real hardware.
- Verify with the library's constant-time compare function.
- Watch the 72-byte limit if you handle long inputs.
- Re-hash on login when you raise the cost.
Password hashing is one of the few security areas where the correct answer is boring and stable. Pick a solid algorithm, use the library properly, and move on to the problems that actually need your creativity.
Photo by Jake Walker on Unsplash