Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created May 29, 2013 15:04
Show Gist options
  • Save vaiorabbit/5670985 to your computer and use it in GitHub Desktop.
Save vaiorabbit/5670985 to your computer and use it in GitHub Desktop.
FNV-1a Hash (http://isthe.com/chongo/tech/comp/fnv/) in Python.
import sys
import re
def fnv32a( str ):
hval = 0x811c9dc5
fnv_32_prime = 0x01000193
uint32_max = 2 ** 32
for s in str:
hval = hval ^ ord(s)
hval = (hval * fnv_32_prime) % uint32_max
return hval
# USAGE : $ cat words.txt | python fnv32a.py > tmp_py.txt
if __name__ == '__main__':
# print(fnv32a("lorem"))
for line in sys.stdin:
m = re.search('"(.*)"', line)
word = m.group(1)
print(word + " -> " + str(fnv32a(word)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment