If you store user passwords, you need to hash them with a function built for the job. Bcrypt has been that function for most of us for well over two decades, and despite newer options like Argon2 and scrypt, it remains a perfectly solid choice. It's battle-tested, available in every major language, and hard to misuse in dangerous ways.

Let's go through what bcrypt is, why it's different from a plain hash, and the mistakes I still see in code reviews.

What bcrypt actually is

Bcrypt is a password-hashing function based on the Blowfish cipher. The key thing is that it's deliberately slow and adaptively tunable. That sounds like a bug if you're used to hashing being fast, but for passwords it's exactly the point.

A general-purpose hash like SHA-256 is designed to be fast. That's great for checksums and terrible for passwords, because an attacker who steals your database can try billions of guesses per second on a GPU. Bcrypt slows each guess down to a controlled amount of work, so even a leaked database buys the attacker far less.

Two features make bcrypt good for this:

  • A built-in cost factor. You pick a number (the "work factor" or "rounds"), and the time to compute a hash roughly doubles for each increment. As hardware gets faster, you bump the number.
  • A built-in salt. Bcrypt generates a random salt for every password and stores it inside the output. You don't manage salts yourself, and identical passwords produce different hashes.

Reading a bcrypt hash

A bcrypt hash looks like this:

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

It's self-describing, which is genuinely useful:

  • $2b$ — the algorithm version.
  • 12 — the cost factor (2^12 = 4096 iterations here).
  • The next 22 characters are the salt.
  • The rest is the actual hash.

Because the cost and salt travel with the hash, verifying a password later is self-contained: the library reads those parameters back out and recomputes. You don't store them in separate columns.

When to use it

Use bcrypt whenever you're storing credentials that a human types: account passwords, API keys you let users set, that kind of thing. It's the right tool for "I need to check this secret later but should never be able to recover the original."

Do not use bcrypt for:

  • Hashing large data or files — it's slow and caps input length (more on that below).
  • Anything you need to decrypt. Hashing is one-way; if you need the value back, you want encryption, not hashing.
  • HMACs or signatures — use the appropriate MAC/signature primitive.

A worked example

Here's the standard flow in Node.js with the bcrypt package. The pattern is nearly identical in Python, PHP, Go, and Ruby.

const bcrypt = require('bcrypt');

// On signup: hash the password before storing it
const SALT_ROUNDS = 12;

async function hashPassword(plain) {
  return bcrypt.hash(plain, SALT_ROUNDS);
}

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

// Usage
const hash = await hashPassword('correct horse battery staple');
// store `hash` in the database...

const ok = await verifyPassword('correct horse battery staple', hash);
console.log(ok); // true

Notice there's no manual salting, no concatenation, no storing a salt column. The library does it. The less bespoke crypto you write, the better.

If you just want to eyeball what a hash looks like, test a cost factor, or generate one for a quick script without wiring up a project, our Bcrypt Hash Generator runs in the browser and is handy for sanity checks.

Gotchas that bite people

The 72-byte limit

Bcrypt only looks at the first 72 bytes of the input. Anything past that is silently ignored. For normal passwords this never matters, but if you let users paste long passphrases — or if you pre-hash passwords and pass the result in — you can run into trouble. A common fix is to SHA-256 the password first and then bcrypt the result, but be careful: feed the hash as hex or base64, because a raw binary SHA-256 can contain a null byte and some bcrypt implementations truncate at the first null.

Picking a cost factor

A cost of 10 was fine years ago; 12 is a reasonable default on modern servers. The right number depends on your hardware and tolerance for login latency. Aim for roughly 200–300ms per hash. Measure it on your actual production tier, not your laptop. Too low and you're not slowing attackers enough; too high and a login spike can pin your CPU.

Rehashing on cost change

When you raise the cost factor, old hashes still use the old cost. The clean way to migrate is opportunistic: on a successful login, check whether the stored hash's cost is below your current target, and if so, rehash the plaintext (which you have in hand at that moment) and update the row. Most libraries expose a helper for the "needs rehash" check.

Timing and dummy comparisons

Always use the library's compare function rather than hashing the input and doing == yourself — the compare path is constant-time. Also consider running a bcrypt comparison even when the username doesn't exist, using a dummy hash, so that response times don't reveal which accounts are real.

Don't roll your own anything

No custom salting scheme. No "hash it twice for extra security." No homegrown stretching loops. Use a maintained library, pass the password and the cost, and let it do its thing. Bcrypt is safe largely because the hard parts are already handled for you.

Bcrypt vs. the alternatives

Argon2 (specifically Argon2id) is the current recommendation from most security bodies, because it's memory-hard and resists GPU and ASIC attacks better. If you're starting a greenfield project and have a well-maintained Argon2 library available, it's a great pick. But bcrypt is not broken, and it's often the path of least resistance with mature library support everywhere. Migrating from bcrypt to Argon2 later is straightforward using the same opportunistic-rehash trick.

The short version: never store plaintext, never use a fast general-purpose hash for passwords, pick a sane cost factor, and lean on a trusted library. Get those four things right and you're ahead of a surprising number of production systems.

Photo by FlyD on Unsplash