tab_hash.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import random
  2. import string
  3. # bin(random.getrandbits(64))
  4. # tab hashing
  5. n = 11
  6. seed = 31
  7. key = 'hellp'
  8. def fimurhash(key, seed, n):
  9. hash = seed
  10. for c in key:
  11. c_int = ord(c)
  12. hash *= (11400714819323198485 * (c_int << 15)) & 0xFFFFFFFFFFFFFFFF
  13. hash = ((hash << 7) | (hash >> (32 - 7)) ) & 0xFFFFFFFFFFFFFFFF
  14. return (hash >> (64 - n))
  15. hmap = [random.getrandbits(n + 7) for r in range(256)]
  16. def tab_hash(key):
  17. hash = 0
  18. i = 0
  19. for c in key:
  20. c_int = ord(c)
  21. hash_partial = hmap[c_int] ^ (c_int << i)
  22. hash ^= hash_partial
  23. i += 1
  24. # print(f"{c} : {hash_partial} : {hash}")
  25. # i+= hmap[i & 0xFF]
  26. return hash
  27. def tab_gash(key):
  28. hash = 0
  29. # i = 0
  30. for c in key:
  31. c_int = ord(c)
  32. hash_partial = hmap[(c_int + hash) & 0xFF] ^ c_int
  33. hash ^= hash_partial
  34. # print(f"{c} : {hash_partial} : {hash}")
  35. # i+= hmap[i & 0xFF]
  36. return hash
  37. def bad_hash(key):
  38. hash = 0
  39. for c in key:
  40. c_int = ord(c)
  41. hash_partial = hmap[c_int] ^ c_int
  42. hash ^= hash_partial
  43. # print(f"{c} : {hash_partial} : {hash}")
  44. return hash
  45. # generate random keys
  46. # def random_key(size=6, charset=string.digits):
  47. # def random_key(size=6, charset=string.ascii_letters + string.digits):
  48. def random_key(size=6, charset="abc"):
  49. return ''.join(random.choices(charset, k=size))
  50. # test drive tab hashing
  51. for i in range(2**(n-1)):
  52. # key = random_key(random.randint(1, 10))
  53. key = random_key(8)
  54. t_hash = tab_gash(key)
  55. fimur_hash = fimurhash(key, seed, n)
  56. # mixed_hash = tab_hash(str(fimur_hash))
  57. # print(f"{key} ; {t_hash} ; {fimur_hash} ; {mixed_hash}")
  58. print(f"{key} ; {t_hash} ; {fimur_hash}")