根据RFC 2104的定义,我想用SHA-1实现hmac算法。代码正在运行,但结果与test-vectors from RFC不同。我不确定是否正确地加载了值(字符串转换为十六进制,还是字符串转换为字节?)。
作为模板,我使用了wikipedia中的伪代码
我不确定“块大小”和“输出大小”这两个术语。在维基百科的代码中,outputsize是输入值之一,但从未使用过。
到目前为止,这是我的代码:首先我设置了一个哈希函数,然后我将输入字符串(键和消息)转换成十六进制值。下一步是查看key是否被散列或用零填充。接下来,我将键中的单个字符与这些值进行xor运算(我不知道它们来自哪里,但它们出现在每个示例中,没有任何注释)。最后但并非最不重要的一点是,我组合了一个内部字符串(I_key_pad+ message)并对其进行散列,这导致了一个与外部填充相结合的外部字符串,并再次对其进行散列。
import hashlib
from functools import reduce
def hmac(key, message, hashfunc):
hasher = hashlib.sha1
blocksize = 40
message = toHex(message) #is this right?
key = toHex(key)
#alternative: loading values as bytes
#message = bytes(message, 'utf-8')
#key = bytes(key, 'utf-8')
if len(key) > blocksize:
key = hasher(key)
else:
#key = key.ljust(blocksize, '0') #filling from right to left
#key = key.ljust(blocksize, b'\0') #same as above but for bytes
key = pad(key, blocksize) #filling from left to right
val1 = 0x5c
val2 = 0x36
i = 0
o_key_pad = ""
i_key_pad = ""
while i < blocksize:
o_key_pad += str(ord(key[i]) ^ val1)
i_key_pad += str(ord(key[i]) ^ val2)
i += 1
tmp_string = str(i_key_pad) + str(message)
tmp_string = tmp_string.encode()
inner_hash = hasher(tmp_string).hexdigest()
fullstring = str(o_key_pad) + inner_hash
fullstring = fullstring.encode()
fullstring = hasher(fullstring).hexdigest()
print(fullstring)
def pad(key, blocksize):
key = str(key)
while len(key) < blocksize:
key = '0' + key
key = key
return key
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0' + hv
lst.append(hv)
return reduce(lambda x, y: x + y, lst)
def main():
while (1):
key = input("key = ")
message = input("message = ")
hash = input("hash (0: SHA-256, 1: SHA-1) = ")
hmac(key, message, hash)
if __name__ == "__main__":
main()发布于 2019-05-11 06:53:57
我不理解您代码中的所有步骤,但这里有一个简短的示例,展示了只使用hashlib.sha1的HMAC-SHA1,以及一个辅助函数xor。
import hashlib
def xor(x, y):
return bytes(x[i] ^ y[i] for i in range(min(len(x), len(y))))
def hmac_sha1(key_K, data):
if len(key_K) > 64:
raise ValueError('The key must be <= 64 bytes in length')
padded_K = key_K + b'\x00' * (64 - len(key_K))
ipad = b'\x36' * 64
opad = b'\x5c' * 64
h_inner = hashlib.sha1(xor(padded_K, ipad))
h_inner.update(data)
h_outer = hashlib.sha1(xor(padded_K, opad))
h_outer.update(h_inner.digest())
return h_outer.digest()
def do_tests():
# test 1
k = b'\x0b' * 20
data = b"Hi There"
result = hmac_sha1(k, data)
print(result.hex())
# add tests as desiredhttps://stackoverflow.com/questions/56063248
复制相似问题