Skip to content

Instantly share code, notes, and snippets.

@SolomonSklash
Forked from mengzhuo/hash_djb2.py
Created November 29, 2021 19:42
Show Gist options
  • Save SolomonSklash/0f6dd6e7f25045f5d394b721148bdbc6 to your computer and use it in GitHub Desktop.
Save SolomonSklash/0f6dd6e7f25045f5d394b721148bdbc6 to your computer and use it in GitHub Desktop.
DJB2 Hash in Python
#!/usr/bin/env python
# encoding: utf-8
def hash_djb2(s):
hash = 5381
for x in s:
hash = (( hash << 5) + hash) + ord(x)
return hash & 0xFFFFFFFF
hex(hash_djb2(u'hello world, 世界')) # '0xa6bd702fL'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment