ruby on rails - Why is SHA1 hashing the same string differently? -


this code have salt , hash user's password when register (with actual password hashing taken out because hashing consistently). happening when use first block of code, if try , hash salt find in database after user registers, hashes different gets stored in password_digest. however, if use second or third blocks of code, hash same thing, behavior want.

salt = rand write_attribute :password_digest, digest::sha1.hexdigest("#{salt}") write_attribute :salt, salt 

vs.

salt = "#{rand}" write_attribute :password_digest, digest::sha1.hexdigest(salt) write_attribute :salt, salt 

[edit]

or

salt = rand write_attribute :password_digest, digest::sha1.hexdigest("#{salt}") write_attribute :salt, "{#salt}" 

both salt , password_digest of type :string.

[end edit]

why this? seems me should have same behavior. if store "#{salt}" directly in password_digest without hashing it, matches salt attribute exactly, seems should hash same thing regardless of when hashing happens. furthermore, if manually create user in database involves calling same function set password, hashes match regardless of block of code using, hash conflict seems occur when create user through browser form.

[edit] third block of code seems indicate calling write_attribute on salt without explicitly coercing (is right term?) string changes value of salt somehow when gets stored in database... though seems unlikely me.

[edit] give example of first block of code's behavior: in database,

password_digest = "2f4d39ae81f480f6ad8a759eec6b8b386fbdf636" ,

salt = 0.17127103546001.

however, digest::sha1.hexdigest("0.17127103546001") = "c46566206c072dd453220dac835de6204e64c044".


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -