Skip to content

Instantly share code, notes, and snippets.

@edwintcloud
Last active April 20, 2019 19:21
Show Gist options
  • Save edwintcloud/d80b9fc0eb4e1b9441dd8eb22eae027b to your computer and use it in GitHub Desktop.
Save edwintcloud/d80b9fc0eb4e1b9441dd8eb22eae027b to your computer and use it in GitHub Desktop.
djb2 string hashing
def _hash_str(self, string):
"""hash_str uses the djb2 algorithm to compute the hash
value of a string http://www.cse.yorku.ca/~oz/hash.html"""
hash = 5381
for char in string[1:]:
# (hash << 5) + hash is equivalent to hash * 33
hash = (hash << 5) + hash + ord(char)
return hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment