Skip to content

Instantly share code, notes, and snippets.

@initbar
Created September 16, 2019 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save initbar/c311f11b3a518b01e108a3e948ad17cb to your computer and use it in GitHub Desktop.
Save initbar/c311f11b3a518b01e108a3e948ad17cb to your computer and use it in GitHub Desktop.
# https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
import base64
import struct
def fnv64(data):
hash_ = 0xcbf29ce484222325
for b in data:
hash_ *= 0x100000001b3
hash_ &= 0xffffffffffffffff
hash_ ^= b
return hash_
def hash_dn(dn, salt):
# Turn dn into bytes with a salt, dn is expected to be ascii data
data = salt.encode("ascii") + dn.encode("ascii")
# Hash data
hash_ = fnv64(data)
# Pack hash (int) into bytes
bhash = struct.pack("<Q", hash_)
# Encode in base64. There is always a padding "=" at the end, because the
# hash is always 64bits long. We don't need it.
return base64.urlsafe_b64encode(bhash)[:-1].decode("ascii")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment