DevKits
Concept

How bcrypt Works — Salting, Cost Factor & Why It's Slow on Purpose

bcrypt is a password-hashing function designed to be deliberately slow so that stolen password hashes are expensive to crack. It builds in a per-password salt (stopping rainbow tables) and a tunable cost factor you raise as hardware gets faster. This guide explains the anatomy of a bcrypt hash, why slowness is a feature, and how to choose a cost factor.

Last updated:

Core Concepts

Why a password hash should be slow
General-purpose hashes like SHA-256 are built to be fast — billions per second on a GPU — which is exactly wrong for passwords, because an attacker who steals your hash database can try billions of guesses per second too. bcrypt is an adaptive hash: it's deliberately slow (milliseconds per hash), so a login is barely affected but brute-forcing a stolen database becomes economically infeasible. 'Fast' is a feature for checksums and a vulnerability for passwords.
Salt — defeating rainbow tables
A salt is a random value mixed into each password before hashing, and bcrypt generates and stores a unique salt per password automatically. Because every user's salt differs, two users with the same password get different hashes, and precomputed 'rainbow table' attacks become useless — an attacker would have to build a separate table per salt. The salt isn't secret; it's stored inside the hash string itself.
The cost factor (work factor)
bcrypt's cost factor (e.g. 10, 12) is a power-of-two number of iterations: each increment doubles the work. You raise it over the years as hardware gets faster, keeping cracking expensive without changing your code. A cost of 10–12 is a common balance in 2026 — high enough to resist offline attacks, low enough that verifying a login stays fast. Benchmark on your hardware and target ~50–250 ms per hash.
Anatomy of a bcrypt hash
A bcrypt hash looks like `$2b$12$R9h/...22-char-salt...hash`. The parts are: the algorithm identifier (`$2b$`), the cost factor (`12$`), then a 22-character Base64 salt immediately followed by the 31-character hash — all in one self-describing string. Because the algorithm, cost, and salt travel with the hash, verification needs only the stored hash and the candidate password. This is also exactly the format Apache's `htpasswd -B` writes.

Frequently Asked Questions

Is bcrypt still secure in 2026?

Yes, bcrypt remains a solid, widely-recommended password hash. Its salting and tunable cost factor keep it effective — just raise the cost factor as hardware improves. Newer memory-hard functions like Argon2 and scrypt add resistance to GPU/ASIC attacks and are preferred for new systems that can use them, but bcrypt is a perfectly acceptable, battle-tested choice.

What cost factor should I use for bcrypt?

Pick the highest cost your login latency can tolerate — commonly 10 to 12 in 2026. Benchmark on your production hardware aiming for roughly 50–250 ms per hash. Too low is weak against offline cracking; too high adds noticeable login delay and lets attackers tie up your server.

Why do two hashes of the same password look different?

Because bcrypt adds a unique random salt to each one. The salt is stored inside the hash string, so verification still works, but it means identical passwords produce different hashes — which is exactly what defeats rainbow-table attacks.

Try these related tools