hash_functions.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #
  2. #**************************************************************************
  3. #* *
  4. #* General Purpose Hash Function Algorithms Library *
  5. #* *
  6. #* Author: Arash Partow - 2002 *
  7. #* URL: http://www.partow.net *
  8. #* URL: http://www.partow.net/programming/hashfunctions/index.html *
  9. #* *
  10. #* Copyright notice: *
  11. #* Free use of the General Purpose Hash Function Algorithms Library is *
  12. #* permitted under the guidelines and in accordance with the most current *
  13. #* version of the Common Public License. *
  14. #* http://www.opensource.org/licenses/cpl1.0.php *
  15. #* *
  16. #**************************************************************************
  17. #
  18. def RSHash(key):
  19. a = 378551
  20. b = 63689
  21. hash = 0
  22. for i in range(len(key)):
  23. hash = hash * a + ord(key[i])
  24. a = a * b
  25. return hash
  26. def JSHash(key):
  27. hash = 1315423911
  28. for i in range(len(key)):
  29. hash ^= ((hash << 5) + ord(key[i]) + (hash >> 2))
  30. return hash
  31. def PJWHash(key):
  32. BitsInUnsignedInt = 4 * 8
  33. ThreeQuarters = long((BitsInUnsignedInt * 3) / 4)
  34. OneEighth = long(BitsInUnsignedInt / 8)
  35. HighBits = (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
  36. hash = 0
  37. test = 0
  38. for i in range(len(key)):
  39. hash = (hash << OneEighth) + ord(key[i])
  40. test = hash & HighBits
  41. if test != 0:
  42. hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
  43. return (hash & 0x7FFFFFFF)
  44. def ELFHash(key):
  45. hash = 0
  46. x = 0
  47. for i in range(len(key)):
  48. hash = (hash << 4) + ord(key[i])
  49. x = hash & 0xF0000000
  50. if x != 0:
  51. hash ^= (x >> 24)
  52. hash &= ~x
  53. return hash
  54. def BKDRHash(key):
  55. seed = 131 # 31 131 1313 13131 131313 etc..
  56. hash = 0
  57. for i in range(len(key)):
  58. hash = (hash * seed) + ord(key[i])
  59. return hash
  60. def SDBMHash(key):
  61. hash = 0
  62. for i in range(len(key)):
  63. hash = ord(key[i]) + (hash << 6) + (hash << 16) - hash;
  64. return hash
  65. def DJBHash(key):
  66. hash = 5381
  67. for i in range(len(key)):
  68. hash = ((hash << 5) + hash) + ord(key[i])
  69. return hash
  70. def DEKHash(key):
  71. hash = len(key);
  72. for i in range(len(key)):
  73. hash = ((hash << 5) ^ (hash >> 27)) ^ ord(key[i])
  74. return hash
  75. def BPHash(key):
  76. hash = 0
  77. for i in range(len(key)):
  78. hash = hash << 7 ^ ord(key[i])
  79. return hash
  80. def FNVHash(key):
  81. fnv_prime = 0x811C9DC5
  82. hash = 0
  83. for i in range(len(key)):
  84. hash *= fnv_prime
  85. hash ^= ord(key[i])
  86. return hash
  87. def APHash(key):
  88. hash = 0xAAAAAAAA
  89. for i in range(len(key)):
  90. if ((i & 1) == 0):
  91. hash ^= ((hash << 7) ^ ord(key[i]) * (hash >> 3))
  92. else:
  93. hash ^= (~((hash << 11) + ord(key[i]) ^ (hash >> 5)))
  94. return hash