Hash a password with an old, fast algorithm and you get the same output every time, for everyone, forever. Hash it with bcrypt and the result changes on every run, even when the password is identical. That difference is not a bug. It is the salt doing its job, and understanding it explains most of what makes bcrypt safe for storing passwords.
You can watch it happen with the free bcrypt Hash Generator on Ovatly: hash the same password twice and compare the results.
Contents
- Quick answer
- What a salt actually is
- Why precomputation fails against salt
- Salt also hides password reuse
- How to hash a password with bcrypt, step by step
- Worked example: one password, one hash, and why yours will differ
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
A salt is a random value generated fresh for every password hash and mixed into the password before hashing. Because the salt differs each time, two hashes of the same password never match, yet both verify correctly, because the salt is stored inside the result and read back automatically. Salt kills rainbow tables, since a precomputed table cannot account for randomness it has never seen, and it hides which users share a password. Try it in the bcrypt Hash Generator: the same input gives a new result on every run.
What a salt actually is
A salt is randomness with one job: to make every hash unique before the hashing even starts. When bcrypt hashes a password, it generates a fresh random salt, mixes it with the password, and hashes the combination. Change the salt and the output changes completely, even though the password stayed the same.
Two properties surprise people. First, the salt is not secret. It is stored in plain sight inside the result string, next to the algorithm version and the cost factor. Secrecy was never the point: the salt protects by being unique, not by being hidden. Second, you never manage it yourself. Because the salt travels inside the result, a verifier needs nothing extra: it pulls the salt out of the stored string, hashes the password you just typed with that same salt, and checks whether the outputs match.
Why precomputation fails against salt
The cheapest way to attack a stolen password database is to do the work in advance. A rainbow table is a precomputed store of hashes for enormous numbers of likely passwords. If passwords are hashed with a fast, unsalted algorithm, the attacker simply looks each stolen hash up in the table. No guessing, no waiting: the answer was computed long before the database was stolen.
Salt removes that shortcut entirely. A table only helps if the hash it stores is the hash the victim system produced, and with a unique random salt per password, that hash depends on randomness the table builder could not predict. Precomputing against a salted database means a separate table for every salt. At that point the attacker is reduced to guessing passwords one at a time, where bcrypt's cost factor makes every guess slow.
Salt also hides password reuse
Salt quietly solves a second problem. With an unsalted hash, the output is a pure function of the password, so two users who chose the same password get byte-for-byte identical hashes. In a leaked database that repetition is visible at a glance, and it tells an attacker which accounts share a password and which password is most common across the whole user base.
With bcrypt, each user's password is mixed with a different salt, so the same password produces unrelated results. The pattern disappears. An attacker cannot tell that two people picked the same password, and cannot rank passwords by how often they appear.
How to hash a password with bcrypt, step by step
- Open the bcrypt Hash Generator and enter the password or text you want to hash.
- Set the cost factor. The classic default is 10. Each step up multiplies the work: cost 12 is four times slower than cost 10, which is the point, since an attacker pays that cost on every guess too.
- Generate the bcrypt hash and copy the result. Look at the beginning of the string: it records the version and the cost, and the salt sits inside the same string.
- To check a password later, use the verify function with the stored hash. The tool extracts the salt from the stored result automatically and reports a simple match or no match.
Worked example: one password, one hash, and why yours will differ
Hashing the well-known test phrase "correct horse battery staple" at cost 10 produced this result in one run:
$2b$10$jT61zASMuZO.BYMYrC8tVOrG4f70YI6WdfYJA2RvF4xQe9jYzcUa.
Read it in parts: the opening marks it as a bcrypt result at cost 10, the salt follows inside the same string, and the rest is the hash of the password combined with that salt.
Now the honest part: run the identical phrase through the generator yourself and you will get a different string. That is expected, not a failure. Your run generated a different salt, so the output differs, yet verification still confirms your password against your hash, because the salt your run used is stored inside your result. Two different strings, both correct, both proving the same password.
Limitations and honest tradeoffs
Salt is not a complete defence on its own. It stops precomputation and hides reuse, but an attacker who targets one specific account can still guess passwords directly, one at a time. Bcrypt's answer is its cost: reversing a bcrypt hash is only possible by guessing, and the cost factor makes each guess expensive. Salt and slowness work as a pair; neither replaces the other.
There is also a hard input limit: bcrypt truncates input at 72 bytes. Characters beyond that point are ignored, so a very long passphrase gains nothing past the cutoff, and two passwords sharing the same first 72 bytes collide. Finally, a salt is per hash, not per system. A tool that reuses one fixed salt for every password has thrown away the entire benefit, because the randomness is the protection.
Common mistakes
- Expecting the same output twice. A different result on every run is the design working. If a bcrypt tool returns identical hashes for identical input, something is wrong with it.
- Storing the salt separately, or encrypting it. The salt is meant to travel inside the result; separating it protects nothing, since the salt was never a secret.
- Using one fixed salt for everyone. A shared salt restores the attacker's favourite conditions: one table built for that salt works across the whole database.
- Assuming salt makes weak passwords safe. Salt stops shortcuts, not guessing. A short, common password still falls quickly once an attacker guesses it directly, cost factor or not.
- Hashing passwords with a fast unsalted algorithm because it looks scrambled. A hex string is not protection. Speed and determinism are precisely what an attacker wants.
FAQs
Why does the same password give a different bcrypt hash every time? Because a fresh random salt is generated for every hash and mixed in before hashing. Two runs of the same password use two different salts, so the results differ. Both still verify, because the salt is stored inside the result and is read back automatically during verification.
Where is the salt kept? Inside the hash result itself. A bcrypt result carries the version, the cost factor and the salt alongside the hash, so a verifier needs nothing else: it extracts the salt from the stored result, hashes the password you present with it, and compares.
What is a rainbow table and why does salt defeat it? A rainbow table is a precomputed store of hashes for huge numbers of likely passwords, letting an attacker look a stolen hash up instead of guessing. A unique salt per password makes every hash a one-off: the table would have to be rebuilt for each salt, which destroys the point of precomputing it.
Does salt hide the fact that two users share a password? Yes. With a fast unsalted hash, two users with the same password produce identical hashes, and the repetition is visible in a leaked database. With bcrypt, each user gets a different salt, so identical passwords produce unrelated results and the pattern disappears.
Does a longer password still matter if salt is used? Yes. Salt stops precomputation, but an attacker who targets one account still guesses passwords one by one. Bcrypt's cost makes each guess expensive, and a longer password makes the number of guesses needed far larger. Note that bcrypt truncates input at 72 bytes, so characters beyond that add nothing.
Conclusion
The salt is a small idea with an outsized effect: mix fresh randomness into every password before hashing, store that randomness inside the result, and the two cheapest attacks on a password database, precomputed tables and reuse spotting, both stop working. The bcrypt Hash Generator lets you watch it happen, since no two runs ever agree. Its natural companion is our guide to HMAC and how it proves a message was not changed, which applies the same honesty to a related primitive. For more guides in this series, see our Security and Privacy guides.
References
- OWASP Cheat Sheet Series, Password Storage Cheat Sheet, on salting and slow hashing for stored passwords: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
- Wikipedia, bcrypt, on the algorithm, its salt and its cost factor: https://en.wikipedia.org/wiki/Bcrypt