@
chinvo 最近用到的一个,但是不确定不同值是否会产生相同 hash.
# Doc:
http://www.drdobbs.com/database/hashing-rehashed/184409859# code:
https://sourceforge.net/p/cuckoo2mist/code/HEAD/tree/trunk/class_mist.pydef ELFHash(key):
hash = 0
x = 0
for i in range(len(key)):
hash = (hash << 4) + ord(key[i])
x = hash & 0xF0000000
if x != 0:
hash ^= (x >> 24)
hash &= ~x
return hash
def int2hex(n, len):
assert n is not None
assert len is not None
try:
hexval = ('0' * len) + "%x" % int(n)
except ValueError:
hexval = ('0' * len) + "%x" % int(n, 16)
return hexval[len * -1:]
def hashPJW(text):
number = ELFHash(text)
res = int2hex(number, 8)
return res