Skip to content

Instantly share code, notes, and snippets.

@AltimorTASDK
Created November 1, 2022 23:01
Show Gist options
  • Save AltimorTASDK/2ee54ed82e73d9210bdb24e0d8f04240 to your computer and use it in GitHub Desktop.
Save AltimorTASDK/2ee54ed82e73d9210bdb24e0d8f04240 to your computer and use it in GitHub Desktop.
from functools import reduce
def fnv(data: bytes, prime: int, basis: int, mod: int):
return reduce(lambda hash, b: ((hash * prime) ^ b) % mod, data, basis)
def fnv_a(data: bytes, prime: int, basis: int, mod: int):
return reduce(lambda hash, b: ((hash ^ b) * prime) % mod, data, basis)
def find_basis_a(hash: int, data: bytes, prime: int, mod: int):
# reverse the operations using the prime's modular multiplicative inverse
inverse = pow(prime, -1, mod)
return fnv(reversed(data), prime=inverse, basis=hash, mod=mod)
def dvar_hash(name: str):
return fnv_a(name.lower().encode(),
prime=0x10000000233,
basis=0x7238A5E2001AEB8E,
mod=2**64)
def main():
names = ["db_keyServer1", "db_keyServer2", "db_keyServer3"]
hashes = [0xFB94649B6B0A4A16, 0xFB94639B6B0A47E3, 0xFB94629B6B0A45B0]
prime = hashes[1] - hashes[2]
print(f"prime {prime:X}")
basis = find_basis_a(hashes[0], names[0].lower().encode(), prime, 2**64)
print(f"basis {basis:X}")
for name, expected in zip(names, hashes):
print(f"hash {dvar_hash(name):016X} expected {expected:016X}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment