Created
May 29, 2012 21:47
-
-
Save dpk/2830988 to your computer and use it in GitHub Desktop.
djb2 and djb2a hash functions in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def djb2 str | |
hash = 5381 | |
str.each_byte do |b| | |
hash = (((hash << 5) + hash) + b) % (2 ** 32) | |
end | |
hash | |
end | |
def djb2a str | |
hash = 5381 | |
str.each_byte do |b| | |
hash = (((hash << 5) + hash) ^ b) % (2 ** 32) | |
end | |
hash | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment