If you're storing user passwords, you don't store the password. You store a hash of it. That part most people get. The part that goes wrong is which hashing approach they reach for. SHA-256 is fast, which is exactly what you don't want. bcrypt is slow on purpose, and that's why it's still a solid default years after it first appeared.

What bcrypt actually is

bcrypt is a password-hashing function built on the Blowfish cipher. Two properties make it useful for passwords:

  • It's deliberately slow. A general-purpose hash like SHA-256 can be computed billions of times per second on a GPU. bcrypt has a tunable work factor (the "cost") that makes each hash take a meaningful fraction of a millisecond to hundreds of milliseconds. That throttles attackers trying to brute-force stolen hashes.
  • It handles salting for you. Every bcrypt hash includes a random salt baked into the output. You don't manage a separate salt column, and you don't get two identical hashes for two users with the same password.

A bcrypt hash looks like this:

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

Reading it left to right: $2b$ is the algorithm version, 12 is the cost factor, and the rest is the 22-character salt followed by the hash. Everything you need to verify a password later is in that one string. That's genuinely convenient — no extra fields, no reconstructing anything.

When to use it

Use bcrypt for storing user passwords in an application where you control the backend. That's the main use case and it does it well. If you want to experiment with the format or generate a sample hash to test your verification code, our Bcrypt Hash Generator lets you produce one in the browser without wiring up a script first.

bcrypt is not a general-purpose hash. Don't use it for file integrity, deduplication, or hashing API keys where speed matters — use SHA-256 for those. And for new systems where you have the choice, Argon2id is the modern recommendation and worth evaluating. That said, bcrypt remains well-understood, widely supported, and perfectly acceptable. Nobody's going to fault you for shipping it.

A short worked example

Here's the typical flow in Node.js using the bcrypt package. The pattern is nearly identical in every language.

const bcrypt = require('bcrypt');

const SALT_ROUNDS = 12;

// When a user registers
async function hashPassword(plain) {
  return await bcrypt.hash(plain, SALT_ROUNDS);
}

// When a user logs in
async function verifyPassword(plain, storedHash) {
  return await 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); // true

Notice you never pass the salt around. bcrypt.compare reads the salt and cost out of the stored hash and recomputes the comparison. That's the whole API you need for 99% of cases.

Choosing a cost factor

The cost factor is exponential — each increment doubles the work. A cost of 12 is a reasonable starting point on modern hardware. The honest way to tune it: benchmark on your actual production server and pick the highest value that keeps login under roughly 250ms. Anything slower and your login endpoint starts to feel sluggish; much faster and you're giving attackers an easier time.

// Quick benchmark
const start = Date.now();
await bcrypt.hash('benchmark', 12);
console.log(`Cost 12 took ${Date.now() - start}ms`);

The gotchas

The 72-byte limit

bcrypt only looks at the first 72 bytes of input. Anything past that is silently ignored. For normal passwords this never matters. But if you let users paste 200-character passphrases, or if you pre-hash passwords with something that produces long output, you can get a nasty surprise where different passwords collide. A common safe pattern is to SHA-256 the password first, base64-encode it, then bcrypt that — keeping the input well under 72 bytes while allowing arbitrarily long passwords.

Null bytes

Some bcrypt implementations truncate at the first null byte. If a password can contain one, you may end up hashing an empty-ish string. Most libraries handle this now, but it's worth knowing your library's behaviour.

Don't roll your own comparison

Always use the library's compare function, never a plain string equality check against a re-hashed value — you can't re-hash to the same output without the original salt anyway. The provided compare is also written to resist timing attacks.

Rehashing on login

Over time you'll want to bump your cost factor. You can't rehash stored passwords in bulk because you don't have the plaintext. Instead, rehash opportunistically: on a successful login, check whether the stored hash uses your current cost, and if not, hash the freshly-provided plaintext and update the record.

async function loginAndMaybeRehash(plain, user) {
  const ok = await bcrypt.compare(plain, user.hash);
  if (!ok) return false;

  const currentCost = parseInt(user.hash.split('$')[2], 10);
  if (currentCost < SALT_ROUNDS) {
    user.hash = await bcrypt.hash(plain, SALT_ROUNDS);
    await saveUser(user);
  }
  return true;
}

Using it for rate limiting by mistake

Because bcrypt is slow, a flood of login attempts can tie up your CPU and effectively DoS your own service. Pair it with proper rate limiting on your auth endpoints. The slowness is a feature against offline attacks, but it works against you under a live request flood.

The short version

  • Use bcrypt (or Argon2id) for passwords, never fast hashes like SHA-256 alone.
  • Let the library manage the salt — it's inside the hash string.
  • Tune the cost factor to your hardware, aiming for a couple hundred milliseconds per hash.
  • Watch the 72-byte limit if you allow long passwords.
  • Rehash opportunistically on login to keep up with rising cost factors.

None of this is complicated once you've done it once. Get the pattern right in a small helper, reuse it everywhere, and your password storage stops being something you have to think about.

Photo by FlyD on Unsplash