Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate hash on ruby

On production server:

"KYK_iphone3.jpg".hash.abs%1000
=> 908

But on staging server:

"KYK_iphone3.jpg".hash.abs%1000
=> 740

The two hashes do not match. Can you please give me your ideas on what to do in this situation?

like image 662
khanh Avatar asked Jan 19 '23 15:01

khanh


1 Answers

Ruby's default hashing is not guaranteed to be consistent across implementations. You should use a standardized hashing algorithm such as MD5, SHA1 or similar, if you require consistent hashes.

EDIT: On further investigation, it turns out it is not even consistent across different processes:

$ irb
> "abc".hash
 => 3669021835949727595
> exit 
$ irb
> "abc".hash
 => 2091809102525897616

It appears this might be an intended security feature inherited from Perl to protect against algorithmic complexity attacks.

like image 94
hammar Avatar answered Jan 25 '23 23:01

hammar