2013-05-30 52 views
0

我需要生成k个散列值(0 .. m-1),并且k个数字应该是不同的。 根据不同的散列种子,散列值应该不同。使用python生成k个散列值

我发现了这个代码,但是对于我来说只用一个值就太大了。

import hashlib, uuid 
password = "abc" <-- key 
salt = str(10) # <-- hash seed 
value = hashlib.sha1(password + salt).hexdigest() 
print value # 105dee46d56df0c97ca9b6a09e59fbf63d8ceae2 

如何获得0和m-1之间的良好k散列值?或者是否可以将值分成k个部分来应用mod操作?

+0

你想使它具有可变'M'号工作?或者它是一个特定的m? –

+0

...你试图产生一个哈希碰撞什么的? – muhmuhten

+0

@JoranBeasley:它可以是两个。 – prosseek

回答

0

我发现mmh3是迄今为止最好的选择。

import mmh3 

def getHash(key, m, k): 
    result = set() 
    seed = 1 
    while True: 
     if len(result) == k: 
      return list(result) 
     else: 
      b = mmh3.hash(key, seed) % m 
      result.add(b) 
      seed += 10 
      print result 

if __name__ == "__main__": 
    print getHash("Hello", 100, 5) 
    print getHash("Good Bye", 100, 5) 

结果:

set([12]) 
set([43, 12]) 
set([43, 12, 29]) 
set([88, 43, 12, 29]) 
set([88, 80, 43, 12, 29]) 
[88, 80, 43, 12, 29] 
set([20]) 
set([2, 20]) 
set([2, 20, 70]) 
set([2, 75, 20, 70]) 
set([2, 75, 20, 70, 39]) 
[2, 75, 20, 70, 39] 
0

关于“k”和“m”是什么,你的问题还不清楚。但是任何合理的散列函数输出的所有位都是“随机的”。所以你可以把它切碎并分开使用。

0

这是工作的代码。

import hashlib, uuid 
# http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python 

def getHash(key, hashseed, m, k): 
    """ 
    We use sha256, and it generates 64 bytes of hash number, so k should be 2 <= k <= 32 
    However, because of duplicity the real limit should be much lower. 

    Todo: You can concatenate more sha256 values to get more k values 
    """ 
    salt = str(hashseed) 
    hashed_password = hashlib.sha256(key + salt).hexdigest() 
    if k > 32: raise Error("k should be less than 32") 
    if k <= 1: raise Error("k should be more than 2") 

    result = [] 
    index = 0 

    # make the non-overwrapping hash value below m 
    while True: 
     value = int(hashed_password[index:index+2], 16) % m 
     index += 2 

     # second loop for detecting the duplicate value 
     while True: 
      if value not in result: 
       result.append(value) 
       break 
      # Try the next value 
      value = int(hashed_password[index:index+2], 16) % m 
      index += 2 
     if len(result) == k: break 

    return result 

if __name__ == "__main__": 
    res = getHash("abc", 1, 10, 5) # seed:1, m = 10, k = 5 
    assert len(res) == 5