Sooner or later every application with a login has to store passwords, and the one thing everyone agrees on is that you must not store them as plain text. The next step, "just hash them with SHA-256", sounds responsible but is not good enough. Password storage needs a hash that is deliberately slow, salted and tunable, and bcrypt has been one of the most widely used answers for decades.
This guide explains how bcrypt works, how to read a bcrypt hash, how to choose a cost factor for your hardware, what the 72-byte limit means for you, and how to hash and verify passwords in code. It also shows how to create and check a hash in the browser, which is handy for seeding test accounts or debugging a login.
Why fast hashes are wrong for passwords
General-purpose hash functions such as MD5, SHA-1 and SHA-256 are designed to be fast. That is great for checksums and signatures, and terrible for passwords. If a database leaks, an attacker can compute billions of fast hashes per second on graphics cards, trying common passwords and variations until the hashes match.
Password hashing functions fix this in two ways:
- Salt. A random value is mixed into each hash, so two users with the same password get different hashes, and precomputed "rainbow tables" are useless.
- Work factor. The function is intentionally expensive, and the cost can be raised as hardware gets faster. A hash that takes a quarter of a second is barely noticeable at login, but it slows a mass guessing attack enormously.
bcrypt, designed in 1999 and based on the Blowfish cipher, does both. Newer alternatives such as Argon2id and scrypt also make attacks expensive in terms of memory, which is why current guidance, such as the OWASP Password Storage Cheat Sheet, generally lists Argon2id first. bcrypt remains a solid, widely supported choice and is the default in several popular frameworks.
Reading a bcrypt hash
A bcrypt hash is a 60-character string that contains everything needed to verify a password later:
$2b$12$7kSSdwunpinx8OIyoOHjMulHhGJQQZaDUxFbt9vjqE1qh9oLns2y2
| Part | Example | Meaning |
|---|---|---|
| Version | $2b$ |
Algorithm variant (2a, 2b and 2y are common) |
| Cost | 12$ |
Work factor: 2^12 = 4,096 rounds of key setup |
| Salt | 7kSSdwunpinx8OIyoOHjMu |
22 characters: a 128-bit random salt |
| Hash | lHhGJQQZaDUx... |
31 characters: the actual hash output |
Because the salt and cost are stored inside the hash, you never store them separately. Verification reads them from the stored hash, hashes the candidate password the same way and compares the result.
The version prefixes exist for historical reasons: $2a$ was the original, $2y$ was introduced by PHP after a bug in one implementation, and $2b$ was introduced by OpenBSD after a different bug. In correct modern implementations they all produce the same result, and most libraries can verify all three.
Choosing a cost factor
Each increment of the cost doubles the work. Cost 11 is twice as slow as 10, cost 12 four times as slow. As a rough illustration, one run of the pure-JavaScript bcryptjs library on an ordinary laptop took about 0.1 seconds at cost 10, 0.2 seconds at cost 11 and 0.5 seconds at cost 12. Native libraries are faster, and your server may be slower or faster, so measure on your own hardware.
A practical approach:
- Pick the highest cost that keeps a single hash around a few hundred milliseconds on your production servers under normal load.
- Treat 10 as a minimum for new systems; many frameworks default to 10 or 12.
- Remember that logins and password changes are what pay the cost. A high cost on a busy login endpoint needs capacity planning, and makes it easier to overload with deliberate login attempts, so combine it with rate limiting.
- Plan to raise the cost over time. When a user logs in successfully and their hash uses an old cost, re-hash the password with the new cost and save it.
The 72-byte limit
bcrypt only uses the first 72 bytes of its input. Anything after that is ignored, so two long passwords that share the same first 72 bytes produce matching hashes. Note that it is bytes, not characters: many non-Latin characters and emoji take two to four bytes each in UTF-8.
For typical passwords this is not an issue. If you allow very long passphrases, enforce a maximum length, or use an algorithm without this limit, such as Argon2id. Pre-hashing long passwords with SHA-256 before bcrypt is a known technique, but it has its own subtleties (for example, raw binary output can contain null bytes that some implementations truncate at), so follow a vetted library's guidance rather than inventing a scheme.
Hash and verify passwords in code
In real applications, always use a well-maintained library. In Node.js, the bcryptjs package works without native compilation:
const bcrypt = require("bcryptjs");
async function main() {
const hash = await bcrypt.hash("correct horse battery staple", 12);
console.log(hash); // e.g. $2b$12$... (60 characters, different every time)
console.log(await bcrypt.compare("correct horse battery staple", hash)); // true
console.log(await bcrypt.compare("Correct horse battery staple", hash)); // false
}
main();
Always use the library's compare function to verify. Do not hash the candidate yourself and compare strings: the library handles the salt and cost for you and uses a comparison designed not to leak timing information. Other ecosystems have equivalents: password_hash() and password_verify() in PHP, the bcrypt package in Python, and built-in password encoders in frameworks such as Spring Security, Laravel and Rails.
Generate or check a hash in your browser
The Bcrypt Hash Generator is useful for seeding a test user, resetting a password directly in a development database, or checking why a login fails. Hashing runs locally in your browser, so the password is never sent anywhere.
- Open the Bcrypt Hash Generator and stay on the Generate hash tab.
- Type into Password or text to hash. If it is longer than 72 bytes, the tool warns you that the rest is ignored.
- Set the Cost factor (rounds) slider between 4 and 15. The label shows the number of iterations; costs of 13 and above can take several seconds in a browser.
- Choose a Version prefix: $2b$ (current), $2a$ or $2y$ (PHP), to match what your system expects.
- Click Generate hash. The result is shown with its prefix, cost, salt and hash parts highlighted, ready to copy.
To check a hash, switch to the Verify hash tab, enter the Password and paste the bcrypt hash, then click Verify password. You will see either "Match" or "No match".
Avoid pasting real users' production passwords into any website, including this one. Use the tool for test data and debugging.
Troubleshooting
Every hash is different for the same password
That is correct. A new random salt is generated each time. Verify with a compare function rather than comparing hashes.
A $2y$ hash from PHP fails to verify elsewhere
Some libraries only accept $2a$ or $2b$. Because the algorithm is the same, replacing the prefix with $2b$ usually works. Test with a known password first.
Logins became slow
The cost may be too high for your server, or hashing runs on a busy single thread. Measure the time per hash and adjust, or move hashing to a worker pool.
Related tools
For web server basic authentication files, which can also use bcrypt, use the .htpasswd Generator. For strong random test passwords, use the Password Generator; for fast digests such as SHA-256 checksums, which are not for passwords, use the Hash Generator.
Frequently asked questions
Can a bcrypt hash be decrypted?
No. bcrypt is a one-way function. The only way to find the password is to guess candidates and hash each one, which the cost factor makes slow.
What cost factor should I use for bcrypt?
At least 10 for new systems, and preferably the highest value that keeps hashing to a few hundred milliseconds on your servers. Re-evaluate as hardware improves.
Is bcrypt still secure?
Yes, when used with a sensible cost factor. Argon2id is generally preferred for new designs because it is also memory-hard, but bcrypt remains widely recommended as an acceptable option.
What is the difference between $2a$, $2b$ and $2y$?
They are version markers created after fixes to specific implementations. In correct modern libraries they produce identical hashes, and most libraries verify all three.
Why does bcrypt ignore characters after 72 bytes?
The algorithm uses the password as a Blowfish key, and the key schedule accepts at most 72 bytes. Longer input is truncated, so enforce a maximum length or use a different algorithm for very long passphrases.