If you store user passwords, you need to hash them. Not encrypt — hash. And for most applications, bcrypt is still the boringly reliable choice. It's been around since 1999, it's battle-tested, and it's available in every major language. Let's talk about how it actually works and the mistakes I keep seeing in code reviews.

What bcrypt actually is

bcrypt is a password-hashing function built on the Blowfish cipher. Unlike a plain hash like SHA-256, bcrypt is deliberately slow. That's the whole point. A fast hash is great for checksums but terrible for passwords, because an attacker who steals your database can try billions of guesses per second on modern GPUs. bcrypt makes each guess expensive.

Two features matter most:

  • A built-in salt. Every hash includes a random salt, so identical passwords produce different hashes. This kills rainbow-table attacks and means you don't manage salts yourself.
  • A tunable cost factor. You control how much work each hash takes. As hardware gets faster, you bump the cost up.

A bcrypt hash is self-describing. Here's what one looks like:

$2b$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW
 |  |  |                     |
 |  |  |                     +-- hash (31 chars)
 |  |  +------------------------ salt (22 chars)
 |  +--------------------------- cost factor (12)
 +------------------------------ algorithm version (2b)

Everything needed to verify a password is baked into that one string. You store it as-is in a single column.

When to use it (and when not to)

Use bcrypt when you're storing passwords that users will later type in to log in. That's its job and it does it well.

Don't use it for:

  • API keys or tokens you generate yourself. Those are high-entropy random strings, so a fast hash (HMAC-SHA256) is fine and faster.
  • Hashing large data — bcrypt truncates input, more on that below.
  • Two-way needs. Hashing is one-way. If you need to get the value back, you want encryption, not a hash.

bcrypt's main modern rivals are Argon2 and scrypt. Argon2 (specifically Argon2id) is the current competition winner and resists GPU attacks better. If you're starting fresh and have a solid Argon2 library, it's a great pick. But bcrypt is still perfectly acceptable, more widely supported, and harder to misconfigure. Don't rip out working bcrypt just to chase fashion.

The gotchas that bite people

1. The 72-byte truncation

bcrypt only looks at the first 72 bytes of input. Anything beyond that is ignored. Most of the time this doesn't matter — nobody uses a 73-character password. But it becomes a real bug if you pre-hash passwords (say, with SHA-256) and feed the hex digest to bcrypt. A SHA-256 hex string is 64 characters, so you're fine, but base64 of a longer value can exceed 72 bytes and silently lose entropy. Know your input length.

2. Null bytes

Some older bcrypt implementations stop reading at the first null byte (\0). If you pre-hash with a binary digest that contains a null, you can accidentally truncate the password to almost nothing. Always feed bcrypt text, or use a Base64/hex encoding step if you must pre-hash.

3. Choosing the cost factor

The cost is logarithmic: cost 12 does twice the work of cost 11. A good target is roughly 250–500ms per hash on your production hardware. Benchmark it. The default of 10 was reasonable a decade ago; 12 is a sensible modern baseline for most servers. Too low and you're weak; too high and your login endpoint becomes a self-inflicted denial of service.

4. Comparing hashes

Never compare a stored hash against a freshly computed one with ==. Use the library's compare/verify function. It re-extracts the salt and cost from the stored hash and does a constant-time comparison, which protects against timing attacks.

A short worked example

Here's the typical flow in Node.js using the bcrypt package. The pattern is nearly identical in Python (bcrypt), PHP (password_hash), or Go (golang.org/x/crypto/bcrypt).

const bcrypt = require('bcrypt');

const COST = 12;

// On signup: hash and store the result
async function hashPassword(plain) {
  return bcrypt.hash(plain, COST);
}

// On login: verify the typed password against the stored hash
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 the users table

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

Note what's not here: no manual salt generation, no concatenation, no separate salt column. The library handles all of it. Resist the urge to be clever.

Upgrading cost over time

Because the cost is stored in the hash, you can upgrade users transparently. On a successful login, check whether the stored hash's cost is below your current target, and if so, re-hash the plaintext (which you have at that moment) and update the database:

async function verifyAndUpgrade(plain, storedHash) {
  const ok = await bcrypt.compare(plain, storedHash);
  if (!ok) return false;

  const currentCost = parseInt(storedHash.split('$')[2], 10);
  if (currentCost < COST) {
    const newHash = await bcrypt.hash(plain, COST);
    // persist newHash for this user
  }
  return true;
}

Over a few weeks of logins, your whole user base migrates to the stronger cost without anyone resetting a password.

Testing and experimenting

When you're debugging a login issue or just want to see what a bcrypt hash looks like for a given input and cost, it helps to generate one quickly without writing throwaway code. Our Bcrypt Hash Generator lets you produce and inspect hashes in the browser — handy for seeding a test database or sanity-checking that your verification logic agrees with a reference implementation. Just don't paste real production passwords into any online tool.

The short version

  • Hash passwords with bcrypt; never store plaintext or reversible encryption.
  • Let the library handle the salt — it's in the hash string.
  • Pick a cost around 12 and benchmark for ~250–500ms.
  • Watch the 72-byte and null-byte limits if you pre-hash.
  • Always verify with the library's compare function, not ==.
  • Upgrade the cost on login as hardware improves.

Get those right and password storage stops being something you need to worry about. bcrypt isn't exciting, and that's exactly why it's good.

Photo by Jake Walker on Unsplash