实施 HMAC-SHA1 的问题给出了错误的哈希值?
problem implementing HMAC-SHA1 giving wrong Hash?
我正在尝试在 python 中实现我自己的 HMAC-SHA1 函数,但它似乎总是在最后给我错误的校验和,我不明白为什么
""" An Hmac Implementation of SHA-1 """
from Crypto.Hash import SHA1
hasher = SHA1.new()
password = b"test"
message = b"NAME"
pad_length = 64 - len(password)
key = bytes(pad_length) + password
ipad_num = "01011100" * 64
opad_num = "00110110" * 64
ipad = int.from_bytes(key, byteorder="big") ^ int(ipad_num, base=2)
opad = int.from_bytes(key, byteorder="big") ^ int(opad_num, base=2)
ipad = int.to_bytes(ipad, length=64, byteorder="big")
opad = int.to_bytes(opad, length=64, byteorder="big")
hasher.update(ipad + message)
inner_hash = hasher.digest()
print("inner hash {}".format(inner_hash.hex()))
hasher = SHA1.new()
hasher.update(opad + inner_hash)
print("final hash {}".format(hasher.hexdigest()))
应该给我这个校验和:
对于消息 = NAME
密码 = test
3e0f1cc6c2d787afe49345986212f60d3d4d300d
但它给了我这个校验和
7d6b1ba137a44ee9e083d8e3ba5a84fd739751f4
除非您正在学习密码学,否则我建议使用 python 提供的 hmac 库。
固定代码如下:
""" An Hmac Implementation of SHA-1 """
from Crypto.Hash import SHA1
hasher = SHA1.new()
password = b"test"
message = b"NAME"
pad_length = 64 - len(password) # TODO: support longer key
key = password + bytes(pad_length) # padding should be on the right
ipad_num = "00110110" * 64 # ipad should be 0x36
opad_num = "01011100" * 64 # opad should be 0x5c
ipad = int.from_bytes(key, byteorder="big") ^ int(ipad_num, base=2)
opad = int.from_bytes(key, byteorder="big") ^ int(opad_num, base=2)
ipad = int.to_bytes(ipad, length=64, byteorder="big")
opad = int.to_bytes(opad, length=64, byteorder="big")
hasher.update(ipad + message)
inner_hash = hasher.digest()
print("inner hash {}".format(inner_hash.hex()))
hasher = SHA1.new()
hasher.update(opad + inner_hash)
print("final hash {}".format(hasher.hexdigest()))
我正在尝试在 python 中实现我自己的 HMAC-SHA1 函数,但它似乎总是在最后给我错误的校验和,我不明白为什么
""" An Hmac Implementation of SHA-1 """
from Crypto.Hash import SHA1
hasher = SHA1.new()
password = b"test"
message = b"NAME"
pad_length = 64 - len(password)
key = bytes(pad_length) + password
ipad_num = "01011100" * 64
opad_num = "00110110" * 64
ipad = int.from_bytes(key, byteorder="big") ^ int(ipad_num, base=2)
opad = int.from_bytes(key, byteorder="big") ^ int(opad_num, base=2)
ipad = int.to_bytes(ipad, length=64, byteorder="big")
opad = int.to_bytes(opad, length=64, byteorder="big")
hasher.update(ipad + message)
inner_hash = hasher.digest()
print("inner hash {}".format(inner_hash.hex()))
hasher = SHA1.new()
hasher.update(opad + inner_hash)
print("final hash {}".format(hasher.hexdigest()))
应该给我这个校验和:
对于消息 = NAME
密码 = test
3e0f1cc6c2d787afe49345986212f60d3d4d300d
但它给了我这个校验和
7d6b1ba137a44ee9e083d8e3ba5a84fd739751f4
除非您正在学习密码学,否则我建议使用 python 提供的 hmac 库。
固定代码如下:
""" An Hmac Implementation of SHA-1 """
from Crypto.Hash import SHA1
hasher = SHA1.new()
password = b"test"
message = b"NAME"
pad_length = 64 - len(password) # TODO: support longer key
key = password + bytes(pad_length) # padding should be on the right
ipad_num = "00110110" * 64 # ipad should be 0x36
opad_num = "01011100" * 64 # opad should be 0x5c
ipad = int.from_bytes(key, byteorder="big") ^ int(ipad_num, base=2)
opad = int.from_bytes(key, byteorder="big") ^ int(opad_num, base=2)
ipad = int.to_bytes(ipad, length=64, byteorder="big")
opad = int.to_bytes(opad, length=64, byteorder="big")
hasher.update(ipad + message)
inner_hash = hasher.digest()
print("inner hash {}".format(inner_hash.hex()))
hasher = SHA1.new()
hasher.update(opad + inner_hash)
print("final hash {}".format(hasher.hexdigest()))