If you're storing passwords, you need a password hashing function — not a general-purpose hash like SHA-256, and definitely not plaintext. Bcrypt has been around since 1999 and it's still a perfectly sensible default in 2024. It's not the newest option, but it's well understood, widely supported, and hard to misuse in dangerous ways.
Here's what you actually need to know to use it correctly.
What bcrypt actually is
Bcrypt is a password hashing function built on the Blowfish cipher. The important word is slow. Fast hashes like MD5 or SHA-256 are designed to churn through gigabytes of data quickly — which is exactly what you don't want when an attacker gets your database and starts brute-forcing. Bcrypt is deliberately expensive to compute, and you can tune how expensive with a cost factor.
Two more things bcrypt does for you automatically:
- Salting. Every hash includes a random salt, so two identical passwords produce different hashes. You don't manage the salt separately — it's baked into the output string.
- Self-describing output. A bcrypt hash encodes the algorithm version, cost factor, salt, and digest all in one string. That means you can verify a password with nothing but the stored hash and the plaintext.
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. Everything verification needs is right there.
When to use it
Use bcrypt whenever you're storing user passwords and need to verify them later. That's the main use case. It's not for hashing files, generating API tokens, or anything where you need speed or fixed-length arbitrary-input digests — use SHA-256 or a HMAC for those.
If you're starting a greenfield project and want the most modern option, Argon2id is the current recommendation from most security bodies. But bcrypt is not broken, it's available in every language, and migrating later is straightforward. For most teams, "use bcrypt with a sensible cost" is a decision you won't regret.
The cost factor
The cost (also called work factor or rounds) is a logarithmic value: each increment doubles the work. A cost of 12 does twice the work of 11, four times the work of 10, and so on.
The goal is to make each hash take a noticeable but tolerable amount of time — roughly 100–300ms on your production hardware. A cost of 10 was the old default; 12 is a reasonable modern starting point. The right answer depends on your servers, so benchmark it:
// Node.js, roughly timing a cost factor
const bcrypt = require('bcrypt');
for (const cost of [10, 11, 12, 13]) {
const start = Date.now();
bcrypt.hashSync('benchmark-password', cost);
console.log(`cost ${cost}: ${Date.now() - start}ms`);
}Pick the highest cost that keeps login responses comfortable under load. And bump it over time — hardware gets faster, so a cost that felt heavy five years ago is cheap today.
A worked example
Here's the full hash-and-verify cycle in Node. Note that you never compare hashes with === — you use the library's compare function, which handles the salt extraction and timing-safe comparison for you.
const bcrypt = require('bcrypt');
async function register(plainPassword) {
const cost = 12;
const hash = await bcrypt.hash(plainPassword, cost);
// store `hash` in your users table
return hash;
}
async function login(plainPassword, storedHash) {
const ok = await bcrypt.compare(plainPassword, storedHash);
return ok; // true if the password matches
}
// usage
const stored = await register('correct horse battery staple');
console.log(await login('correct horse battery staple', stored)); // true
console.log(await login('wrong password', stored)); // falseIf you just want to eyeball what a bcrypt hash looks like, or generate one quickly to drop into a test fixture or seed script, our Bcrypt Hash Generator does it in the browser without you wiring up a project. Handy for debugging why a stored hash isn't verifying.
Common gotchas
The 72-byte limit
Bcrypt only looks at the first 72 bytes of input. Anything beyond that is silently ignored. For normal passwords this never matters, but if you're pre-hashing or concatenating a pepper, you can hit it. It also means characters beyond byte 72 contribute nothing — a common surprise when someone pastes a very long passphrase.
Null byte truncation
Some older bcrypt implementations truncate at the first null byte. If your input could contain a \0 — for example, if you base64-decode something first — the password effectively becomes empty. If you need to handle arbitrary binary input, pre-hash with SHA-256 and base64-encode before passing it to bcrypt.
Don't double-hash by accident
A classic bug: hashing the password on the client and again on the server, then comparing inconsistently. Pick one place to hash — the server — and keep the plaintext off disk and out of logs.
Handle cost upgrades gracefully
When you raise the cost factor, existing hashes still use the old cost. Re-hash on successful login: verify with the stored hash, and if its cost is below your current target, compute a fresh hash and save it. Users get migrated transparently as they sign in.
const ok = await bcrypt.compare(plain, storedHash);
if (ok) {
const currentCost = bcrypt.getRounds(storedHash);
if (currentCost < 12) {
const upgraded = await bcrypt.hash(plain, 12);
// save `upgraded` to the user record
}
}Never roll your own salt
Bcrypt generates a cryptographically secure salt for you. Don't pass your own, don't reuse one across users, and don't store the salt in a separate column. It's already in the hash string.
The short version
Bcrypt is a safe, boring choice — which is exactly what you want in security. Store the full hash string, use the library's compare function, set a cost factor that takes a couple hundred milliseconds on your hardware, and re-hash on login when you raise it. Watch the 72-byte limit and null-byte truncation if you're doing anything clever with the input. Do those things and your password storage will hold up far better than the countless breaches built on plain SHA-256.