Skip to content

Instantly share code, notes, and snippets.

@dpk
Created May 29, 2012 21:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/2830988 to your computer and use it in GitHub Desktop.
Save dpk/2830988 to your computer and use it in GitHub Desktop.
djb2 and djb2a hash functions in Ruby
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