1
如何在hashlib.sha1(int)中取整数。 请参阅对此我以IP为字符串转换它作为整数现在hash.sha1利己整数取代码...python sha1取整数
import hashlib
import socket
import struct
class blommy(object):
def __init__(self):
self.bitarray= [0]*2048
def hashes(self,ip):
#convert decimal dotted quad string to long integer"
intip= struct.unpack('>L',socket.inet_aton(ip))[0]
index = [0, 1]
hbyte = hashlib.sha1(intip) # how to take sha1 of integer??
index[0] = ord(hbyte[0])| ord(hbyte[1])<< 8
index[1] = ord(hbyte[2])| ord(hbyte[3])<< 8
需要将此C代码转换为蟒蛇。请建议上面编写代码的一部分。如果我把IP作为int我不能计算sha1 +,即使使用套接字转换IP比sha1不接受它的建议?看到下面的评论
//fixed parameters
k = 2
m = 256*8
//the filter
byte[m/8] bloom ##
function insertIP(byte[] ip) {
byte[20] hash = sha1(ip)
int index1 = hash[0] | hash[1] << 8 # how to in python?
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})
除了八位字节序列以外,没有任何其他概念可以用作sha1摘要;你真的想做什么? – SingleNegationElimination 2012-01-27 21:07:34
这就是我想要做的,你可以看到这个C代码?参数 //过滤器 k = 2 m = 256 * 8 //过滤器 byte [m/8] bloom ##这部分是什么? 函数insertIP(byte [] ip){ byte [20] hash = sha1(ip) int index1 = hash [0] | hash [1] << 8 int index2 = hash [2] | hash [3] << 8 //截断索引到m(需要11位) index1%= m ##? index2%= m ##? //在index1和index2设置位 bloom [index1/8] | = 0x01 << index1%8 ## ?? bloom [index2/8] | = 0x01 << index2%8 ## ?? } //将IP 192.168.1.1插入过滤器: insertIP(byte [4] {192,168,1,1}) – Shazib 2012-01-28 08:00:35
请将新代码添加到您的答案;它在评论形式中是不可取的。 – SingleNegationElimination 2012-01-28 13:38:16