Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the hash generated by BCrypt non-deterministic

I've worked with a number of different hashing algorithms in the past and I was under the impression that they were all deterministic.

I just switched some of my code to use BCrypt.Net and I have to admit I was completely stumped when all of my comparison tests failed.

After looking for errors in my test for an embarrassing amount of time I realized that my assumption that the hashes are deterministic was completely incorrect. There is a verify method which works and it was easy enough to fix the code but I'd like to understand what is going on a little bit better.

Is it salting the values internally or is something else going on?

enter image description here

  • Please note I am salting this in my real code - this is just a test
like image 622
Kelly Robins Avatar asked Oct 16 '25 06:10

Kelly Robins


1 Answers

Is it salting the values internally

Yep. bcrypt is more than a raw hash function, it includes the salt and a few other bits to allow the hash to be validated without extra input:

$2a$12$q6r.MpvzPrUszrWLgaRdlOs04kPcjk0syCDelrzES9O8.UNlHON.u
 ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^
 |  |  \- salt
 |  \---- work factor
 \------- format

The API you're using doesn't expose it as you don't generally need to manipulate the salt, but it's there and you don't need to add your own.

like image 81
bobince Avatar answered Oct 18 '25 01:10

bobince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!