Skip to content

Instantly share code, notes, and snippets.

@yobabyte
Created November 13, 2016 20:45
Show Gist options
  • Save yobabyte/b6acc5969ac5e37f3dabc7f2315d54d5 to your computer and use it in GitHub Desktop.
Save yobabyte/b6acc5969ac5e37f3dabc7f2315d54d5 to your computer and use it in GitHub Desktop.
COD: MWR hash function (FNV1a)
# python cod_hash_dvars.py <dvars.txt>
import os
import sys
import ctypes
# COD: MWR
FNV32_PRIME = 0xB3CB2E29
FNV32_BASIS = 0x319712C3
def uint32(v):
return ctypes.c_uint32(v).value
def fnv1a_hash(data):
hval = FNV32_BASIS
for i in range(0, len(data)):
hval = uint32(hval ^ data[i])
hval = uint32(hval * FNV32_PRIME)
return hval
if len(sys.argv) == 2:
input_file_path = sys.argv[1]
output_file_path = os.path.splitext(sys.argv[1])[0] + '_hashes.txt'
with open(input_file_path, "r") as input_file:
with open(output_file_path, "w") as output_file:
for dvar_name in input_file:
dvar_name = dvar_name.rstrip("\r\n")
dvar_hash = fnv1a_hash(bytearray(dvar_name.lower() + '\x00'))
output_file.write("%-30s = 0x%08X\n" % (dvar_name, dvar_hash))
print "done, output file is \"%s\"" % output_file_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment