If you're storing user passwords, you don't store passwords. You store a one-way hash, and when someone logs in you hash what they typed and compare. Bcrypt has been the pragmatic default for this since the late 90s, and despite its age it's still a perfectly reasonable choice for most applications.

What bcrypt actually is

Bcrypt is a password-hashing function built on the Blowfish cipher. The key thing that makes it suitable for passwords — and plain SHA-256 or MD5 completely unsuitable — is that it's deliberately slow and the cost is tunable.

A fast hash is great for checksums and terrible for passwords. If an attacker steals your database, they'll try to crack hashes offline by guessing passwords and hashing each guess. With MD5 a modern GPU does billions of guesses per second. With bcrypt tuned to take ~250ms per hash, that same attacker is reduced to a few guesses per second. That asymmetry is the whole point.

Bcrypt also handles two things automatically that people often get wrong when they roll their own:

  • Salting — a random salt is generated per hash and stored inside the output, so identical passwords produce different hashes.
  • Cost encoding — the work factor is baked into the hash string, so you can verify old hashes even after you raise the cost for new ones.

Reading a bcrypt hash

A bcrypt hash looks like this:

$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW

Breaking it down: $2b$ is the algorithm version, 12 is the cost factor, and the rest is the 22-character salt followed by the hash itself. Everything you need to verify a password later is in that one string. No separate salt column required.

When to use it

Reach for bcrypt whenever you're authenticating users with passwords and you don't have a strong reason to use something else. It's well supported in every major language, battle-tested, and hard to misuse.

The main alternatives worth knowing: Argon2 (the winner of the Password Hashing Competition, more memory-hard and the modern recommendation if you're starting fresh) and scrypt. All three are fine. Bcrypt's advantage is maturity and ubiquity — you'll find a solid, audited library everywhere. Do not use plain SHA/MD5, and don't invent your own scheme.

Common gotchas

The 72-byte limit

Bcrypt only looks at the first 72 bytes of the input. Anything past that is silently ignored. This matters in two ways. First, extremely long passwords don't get more secure past 72 bytes. Second — and this is the dangerous one — if you pre-hash passwords (for example to get around the limit) by running them through SHA-256 first, encode the digest as base64 or hex, not raw bytes, so you don't accidentally include null bytes. Raw binary digests can contain a 0x00 byte, and some older bcrypt implementations truncate at the first null, drastically weakening the hash.

Pick a sensible cost

The cost factor is exponential — each increment doubles the work. A cost of 12 is a good baseline in 2024; 10 is the old default and is getting light. Benchmark on your actual production hardware and aim for roughly 200–300ms per hash. Fast enough that login feels instant, slow enough to hurt attackers.

// Rough rule of thumb for login latency:
// cost 10 ≈ 60ms, cost 12 ≈ 250ms, cost 14 ≈ 1s
// (varies wildly by CPU — always measure)

Use constant-time comparison

Never compare hashes with ==. Use the library's verify function, which does a constant-time comparison to avoid timing side channels. If you're comparing the stored hash to a freshly computed one yourself, you're probably doing it wrong — pass the plaintext and the stored hash to the library instead.

Upgrading cost over time

Because the cost is stored in the hash, you can bump it transparently. On successful login, check the cost of the stored hash. If it's below your current target, re-hash the password (which you have in plaintext at that moment) and update the record. Over time, active accounts migrate to the stronger cost without forcing a password reset.

A short worked example

Here's the full cycle in Node using the bcrypt package. The same shape applies in Python's passlib/bcrypt, PHP's password_hash, Go's golang.org/x/crypto/bcrypt, and so on.

const bcrypt = require('bcrypt');

const COST = 12;

// On signup
async function hashPassword(plain) {
  return bcrypt.hash(plain, COST);
}

// On login
async function verifyPassword(plain, storedHash) {
  return bcrypt.compare(plain, storedHash);
}

// Usage
const hash = await hashPassword('correct horse battery staple');
// -> $2b$12$...  store this string in your users table

const ok = await verifyPassword('correct horse battery staple', hash);
// -> true

Notice there's no manual salt handling. bcrypt.hash generates the salt, and bcrypt.compare reads it back out of the stored string. Keep it that simple — the surface area for bugs is smallest when you let the library do everything.

If you just want to eyeball what a hash looks like, test a cost factor, or verify that a plaintext matches a hash you already have, our Bcrypt Hash Generator does it in the browser without you wiring up a project. Handy for debugging a migration or sanity-checking a value from your database.

A few parting rules

  • Store the full bcrypt string as-is; don't strip the prefix or split out the salt.
  • Pick a cost around 12 and measure it on real hardware.
  • Treat passwords as opaque — don't trim, lowercase, or re-encode them before hashing.
  • Re-hash on login when you raise the cost.
  • Never log the plaintext, and get it out of memory as soon as you're done.

Bcrypt isn't flashy, but that's exactly why it's still a good pick. It does one job, encodes everything it needs into a single string, and is nearly impossible to misuse if you stick to a well-maintained library. For a greenfield project you might reach for Argon2 instead — but if bcrypt is what your stack gives you, you're in good shape.

Photo by FlyD on Unsplash