Search code examples
laravelbcryptpassword-hash

Why bcrypt always returns different results?


Consider the following typescript:

$ ./artisan tinker
>>> bcrypt('123456')
=> "$2y$10$YLswQefA6JXTYMM5nH90we9siAtG71I1/LMa5XIkplCF32EMtXmKK"
>>> bcrypt('123456')
=> "$2y$10$LoakjerqalqFxI6r.BR.x.K1fycqWS59Xqfj.pblSzlPNLOcbWa/6"

Why is that?


Solution

  • There is a different salt used each time. A random salt is mixed in to the hash to prevent precomputed hash tables from being used. Without salting, an attacker would be able to detect common strings like password123 due to their recognizable hashes. Salting ensures the hashes aren't predictable.