Created
September 16, 2019 14:26
-
-
Save initbar/c311f11b3a518b01e108a3e948ad17cb to your computer and use it in GitHub Desktop.
This file contains 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
# 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