If you store 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 go-to algorithm for this for over two decades, and despite newer options it's still a perfectly good 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. A general-purpose hash like SHA-256 is designed to be fast, which is exactly what you don't want for passwords. If an attacker steals your database, speed is their friend: a fast hash lets them try billions of guesses per second.

Bcrypt fights back in three ways:

  • A built-in salt. Every hash includes a random salt, so two users with the same password get different hashes. This kills rainbow-table attacks.
  • A tunable work factor (the "cost"). You decide how expensive each hash is. As hardware gets faster, you raise the cost.
  • A self-describing output. The salt and cost are stored inside the hash string itself, so you don't manage them separately.

When you'd use it (and when you wouldn't)

Use bcrypt whenever you're storing something a human picked and will type again: login passwords, PINs, that sort of thing. It's the right tool any time you need to verify a secret later without being able to recover it.

Don't reach for bcrypt to hash API keys, file contents, or anything where you need speed or need to compare large volumes of data. For those, SHA-256 or BLAKE3 are correct. And don't use bcrypt as a key-derivation function for encryption keys — that's what scrypt, Argon2, or PBKDF2 are for, though Argon2 overlaps nicely with password hashing too.

Bcrypt vs Argon2

Argon2 won the Password Hashing Competition and is memory-hard, which makes GPU and ASIC attacks harder. If you're starting fresh and your platform has a solid, well-maintained Argon2 library, it's a great choice. But bcrypt is battle-tested, available everywhere, and hard to misconfigure. Picking bcrypt in 2024 is not a mistake.

Reading a bcrypt hash

A bcrypt hash looks like this:

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

Broken down:

  • $2b$ — the algorithm version.
  • 12 — the cost factor. This is a power of two, so cost 12 means 212 = 4,096 iterations.
  • R9h/cIPz0gi.URNNX3kh2O — the 22-character salt.
  • The rest — the actual hash.

Everything the verify step needs lives in that one string. That's why you store it in a single column and never touch the salt yourself.

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 COST = 12;

// On sign-up: hash and store the result
async function register(password) {
  const hash = await bcrypt.hash(password, COST);
  // save `hash` to the users table
  return hash;
}

// On login: compare against the stored hash
async function verify(password, storedHash) {
  return bcrypt.compare(password, storedHash);
}

Note what you don't do: you never generate a salt manually, never store it in a second field, and never roll your own comparison. The library handles the salt, embeds it in the output, and compare extracts it to re-hash the input.

If you just want to see what a bcrypt hash looks like for a given input and cost — handy for debugging a seed script or sanity-checking a library's output — you can use our Bcrypt Hash Generator right in the browser.

Common gotchas

The 72-byte limit

This one surprises people. Bcrypt only hashes the first 72 bytes of input. Anything beyond that is silently ignored. So "aaaa..." (73 a's) and "aaaa..." (100 a's) can produce the same hash. For normal passwords this never matters, but if you pre-hash passwords with something like SHA-256 and pass the hex digest in, you can exceed 72 bytes or truncate entropy. If you need to support very long passphrases, pre-hash with SHA-256 and pass the base64 result (which stays under the limit).

Null bytes

Some older bcrypt implementations stop reading at the first null byte, which can effectively ignore most of a password. Modern libraries handle this, but it's a reason to stick with well-maintained ones.

Picking the cost

Higher cost is more secure but slower. The usual advice: tune it so a single hash takes roughly 250–500ms on your production hardware. On a modern server that's often cost 12 or 13. Benchmark it — don't guess. And remember a login endpoint doing expensive hashing is a DoS vector, so rate-limit it.

Upgrading cost over time

You can't re-hash a password you don't have in plaintext. The trick: check the cost of the stored hash at login time. If it's below your current target, you already have the plaintext in hand (the user just typed it), so re-hash and update the record.

async function verify(password, storedHash) {
  const ok = await bcrypt.compare(password, storedHash);
  if (ok) {
    const rounds = bcrypt.getRounds(storedHash);
    if (rounds < COST) {
      const newHash = await bcrypt.hash(password, COST);
      // update the user's stored hash
    }
  }
  return ok;
}

Timing and comparison

Always use the library's compare function, never === on raw hashes. The library does a constant-time comparison to avoid timing side channels. Rolling your own string comparison can leak information one byte at a time.

The short version

Bcrypt is slow on purpose, salts for you, and stores everything it needs in one self-describing string. Pick a cost that takes a few hundred milliseconds, mind the 72-byte limit, re-hash on login when you bump the cost, and lean on the library for comparison. Do that and you've handled password storage correctly — which is more than a surprising number of production apps can say.

Photo by Jake Walker on Unsplash