我是python的新手,正尝试根据Bit torrent BEP 33创建bloomFilter。我已经创建了Bloom Filter,但这并不是我想要的。这是我需要的,但我还没有完全了解这种情况。如果有人可以在这里解释...
//fixed parameters
k = 2
m = 256*8
//the filter
byte[m/8] bloom ## What is this part?
function insertIP(byte[] ip) {
byte[20] hash = sha1(ip)
int index1 = hash[0] | hash[1] << 8
int index2 = hash[2] | hash[3] << 8
// truncate index to m (11 bits required)
index1 %= m ## ?
index2 %= m ## ?
// set bits at index1 and index2
bloom[index1 / 8] |= 0x01 << index1 % 8 ## ??
bloom[index2 / 8] |= 0x01 << index2 % 8 ## ??
}
// insert IP 192.168.1.1 into the filter:
insertIP(byte[4] {192,168,1,1})
Run Code Online (Sandbox Code Playgroud)
这就是我创造的
import hashlib
m = 2048
def hashes(s):
index = [0, 0]
#for c in s:
#o = ord(c)
index[0] = hashlib.sha224(index[0]).hexdigest ## This needs integer hash
index[1] = hashlib.sha224(index[1]).hexdigest ## same as above
return [x % m for x in index]
class BloomFilter(object):
def __init__(self):
self.bitarray = [0] * m
def add(self, s):
for x in hashes(s):
self.bitarray[x] = 1
#print self.bitarray
def query(self, s):
return all(self.bitarray[x] == 1 for x in hashes(s))
shazib=BloomFilter()
shazib.add('192.168.0.1')
print shazib.query('192.168.0.1')
Run Code Online (Sandbox Code Playgroud)
首先,对代码进行说明...
//fixed parameters
k = 2
Run Code Online (Sandbox Code Playgroud)
这是我最困惑的路线。k根本不使用...
m = 256*8
Run Code Online (Sandbox Code Playgroud)
这是256个字节中的位数。
//the filter
byte[m/8] bloom ## What is this part?
Run Code Online (Sandbox Code Playgroud)
bloom是256个字节的数组,即256 * 8位,即位m。输入的每个位bloom将包含有关过滤器中有哪些值的信息。
function insertIP(byte[] ip) {
byte[20] hash = sha1(ip)
Run Code Online (Sandbox Code Playgroud)
这将创建一个20字节的哈希值ip。
int index1 = hash[0] | hash[1] << 8
int index2 = hash[2] | hash[3] << 8
Run Code Online (Sandbox Code Playgroud)
这两行bloom基于哈希计算两个索引。基本上,index1是的头两个字节的级联hash,并且index2是第二两个字节的级联hash。
// truncate index to m (11 bits required)
index1 %= m ## ?
index2 %= m ## ?
Run Code Online (Sandbox Code Playgroud)
这两行截断了值,以使它们不会超出的可能索引范围bloom。的%是mod运算符; 它在除法后返回余数。(17%4 = 1,22%5 = 2,依此类推。)还记得Bloom的长度为256 * 8位吗?11位允许我们编码2 ** 11个可能的索引,即2048个值,即256 * 8个值。
// set bits at index1 and index2
bloom[index1 / 8] |= 0x01 << index1 % 8 ## ??
bloom[index2 / 8] |= 0x01 << index2 % 8 ## ??
Run Code Online (Sandbox Code Playgroud)
我们将其bloom视为位数组,因此我们必须进行一些位操作以访问正确的位。首先,除以indexA8,得到正确的字节,然后indexA使用%运算符截断以得到该字节内的正确位。
}
// insert IP 192.168.1.1 into the filter:
insertIP(byte[4] {192,168,1,1})
Run Code Online (Sandbox Code Playgroud)
瞧,我们有一个布隆过滤器。如果按位打印,它将如下所示:
data-> 001011000101110011000001001000100...
indices-> 000000000011111111112222222222333...
012345678901234567890123456789012...
Run Code Online (Sandbox Code Playgroud)
并且,如果特定的ip在进行哈希处理时生成了index1of 5和index2of of 9,则将其视为“在”过滤器中,因为在index 5和bit处的位9设置为1。当然,可能存在误报,因为多个不同的值可能会导致相同的索引。但不会有假阴性。
import hashlib
m = 2048
def hashes(s):
index = [0, 0]
#for c in s:
#o = ord(c)
index[0] = hashlib.sha224(index[0]).hexdigest ## This needs integer hash
index[1] = hashlib.sha224(index[1]).hexdigest ## same as above
Run Code Online (Sandbox Code Playgroud)
这是你的第一个问题。index[0]并且index[1]必须是整数。另外,hashlib.sha224(index[0]).hexdigest返回一个方法。您必须调用方法以从中获取任何收益,例如:hashlib.sha224(index[0]).hexdigest()。另外,如果您希望以与上述代码相同的方式工作,则可以将散列转换为int(可以int(x, 16)用来将十六进制字符串转换为整数),然后使用提取前两个字节& 65535,然后移动使用>> 16,将其减少两个字节,然后& 65535再次使用提取这两个字节。一旦您确定正确,其余的工作。
return [x % m for x in index]
class BloomFilter(object):
def __init__(self):
self.bitarray = [0] * m
def add(self, s):
for x in hashes(s):
self.bitarray[x] = 1
#print self.bitarray
def query(self, s):
return all(self.bitarray[x] == 1 for x in hashes(s))
shazib=BloomFilter()
shazib.add('192.168.0.1')
print shazib.query('192.168.0.1')
Run Code Online (Sandbox Code Playgroud)