This question already has an answer here:
- How can bcrypt have built-in salts? 3 answers
- Do I need to store the salt with bcrypt? 1 answer
I am using Coda Hale's Ruby bcrypt library. I noticed recently that it wasn't working like I thought it worked. I had thought that the proper procedure is:
- Generate a salt
- Obtain a password
- Concatenate the salt and the password strings
- Hash them through your hashing function
But when I look at the results of the bcrypt function it appears that the salt is concatenated to the hash not the password. That is the salt concatenation happens after step #4, not before. I'm assuming Coda Hale is doing this right, but I am wondering why it behaves like this.
Here's a brief IRB session to show what's weird (to me). Notice that in the results of the hash_secret
function, the first 29 chars are the same as the salt. Any information as to why this is the case would be appreciated.
My only theory is that the salt is prepended as well as embedded in the hash, which eliminates the need to store the salt in a separate DB field (essentially a record packing strategy)?
irb#1(main):004:0> password_salt = BCrypt::Engine.generate_salt
=> "$2a$10$OrKdcWORLL8Gorhy9XR3UO"
irb#1(main):005:0> password='abc'
=> "abc"
irb#1(main):006:0> BCrypt::Engine.hash_secret(password, password_salt)
=> "$2a$10$OrKdcWORLL8Gorhy9XR3UOY8Sebzq92m7r02XPitzoazPdO7tmsEO"
irb#1(main):007:0>