如何将 HASHLIB 摘要转换为 python 中的数组?
How to cast HASHLIB digest to array in python?
我正在尝试将 SHA1
摘要的结果与初始化数组进行比较。
但是当比较第一个字节时,return它们不相等,而当打印摘要的第一个字节时,它与我初始化数组中的第一个字节相同。
我应该以某种方式施放它吗?
import hashlib
my_digest = [0x7,0x3,0x8,0x2,0x2,0x5,0x6,0xa,0xb,0xb,0x3,0xe,0xe,0xa,0x3,0x2,0x5,0xf,0x9,0xa,0xd,0xe,0x1,0xc,0xc,0x0,0x4,0xe,0x4,0x9,0x3,0x3,0xe,0xe,0x8,0x1,0x7,0xc,0xd,0x3]
digest = hashlib.sha1(b"im a very good speaker").hexdigest()
# digest = 7382256abb3eea325f9ade1cc04e4933ee817cd3
if(digest[0] == my_digest[0]):
print("correct")
else:
print("not correct")
print(digest)
输出:
not correct
7382256abb3eea325f9ade1cc04e4933ee817cd3
print(digest[0])
return 7
在 Python 3 中,您使用您的代码将 str
与 int
进行比较,虽然这可以完成,但不会尝试解释字符串的内容,所以 '7' != 7
.
避免这种情况的一种方法是 将您的 int
转换为 str
,例如:
if digest[0] == str(my_digest[0]):
问题出在 type
。您正在将字符串与 int 进行比较。请参阅下文以了解可能的修复方法。
import hashlib
my_digest = [0x7,0x3,0x8,0x2,0x2,0x5,0x6,0xa,0xb,0xb,0x3,0xe,0xe,0xa,0x3,0x2,0x5,0xf,0x9,0xa,0xd,0xe,0x1,0xc,0xc,0x0,0x4,0xe,0x4,0x9,0x3,0x3,0xe,0xe,0x8,0x1,0x7,0xc,0xd,0x3]
digest = hashlib.sha1(b"im a very good speaker").hexdigest()
# digest = 7382256abb3eea325f9ade1cc04e4933ee817cd3
print("digest[0]", type(digest[0]))
print("my_digest[0]", type(my_digest[0]))
if(int(digest[0]) == my_digest[0]):
print("correct")
else:
print("not correct")
print(digest)
首先,如果您想将摘要表示为十六进制整数列表,它将如下所示:
>>> my_digest = [0x73, 0x82, 0x25, 0x6a, 0xbb, 0x3e, 0xea, 0x32, 0x5f, 0x9a, 0xde, 0x1c, 0xc0, 0x4e, 0x49, 0x33, 0xee, 0x81, 0x7c, 0xd3]
其次,您希望 digest()
而不是 hexdigest()
来获取原始字节散列:
>>> digest = hashlib.sha1(b"im a very good speaker").digest()
最后,在比较之前将其转换为列表:
>>> list(digest) == my_digest
True
我正在尝试将 SHA1
摘要的结果与初始化数组进行比较。
但是当比较第一个字节时,return它们不相等,而当打印摘要的第一个字节时,它与我初始化数组中的第一个字节相同。
我应该以某种方式施放它吗?
import hashlib
my_digest = [0x7,0x3,0x8,0x2,0x2,0x5,0x6,0xa,0xb,0xb,0x3,0xe,0xe,0xa,0x3,0x2,0x5,0xf,0x9,0xa,0xd,0xe,0x1,0xc,0xc,0x0,0x4,0xe,0x4,0x9,0x3,0x3,0xe,0xe,0x8,0x1,0x7,0xc,0xd,0x3]
digest = hashlib.sha1(b"im a very good speaker").hexdigest()
# digest = 7382256abb3eea325f9ade1cc04e4933ee817cd3
if(digest[0] == my_digest[0]):
print("correct")
else:
print("not correct")
print(digest)
输出:
not correct
7382256abb3eea325f9ade1cc04e4933ee817cd3
print(digest[0])
return 7
在 Python 3 中,您使用您的代码将 str
与 int
进行比较,虽然这可以完成,但不会尝试解释字符串的内容,所以 '7' != 7
.
避免这种情况的一种方法是 将您的 int
转换为 str
,例如:
if digest[0] == str(my_digest[0]):
问题出在 type
。您正在将字符串与 int 进行比较。请参阅下文以了解可能的修复方法。
import hashlib
my_digest = [0x7,0x3,0x8,0x2,0x2,0x5,0x6,0xa,0xb,0xb,0x3,0xe,0xe,0xa,0x3,0x2,0x5,0xf,0x9,0xa,0xd,0xe,0x1,0xc,0xc,0x0,0x4,0xe,0x4,0x9,0x3,0x3,0xe,0xe,0x8,0x1,0x7,0xc,0xd,0x3]
digest = hashlib.sha1(b"im a very good speaker").hexdigest()
# digest = 7382256abb3eea325f9ade1cc04e4933ee817cd3
print("digest[0]", type(digest[0]))
print("my_digest[0]", type(my_digest[0]))
if(int(digest[0]) == my_digest[0]):
print("correct")
else:
print("not correct")
print(digest)
首先,如果您想将摘要表示为十六进制整数列表,它将如下所示:
>>> my_digest = [0x73, 0x82, 0x25, 0x6a, 0xbb, 0x3e, 0xea, 0x32, 0x5f, 0x9a, 0xde, 0x1c, 0xc0, 0x4e, 0x49, 0x33, 0xee, 0x81, 0x7c, 0xd3]
其次,您希望 digest()
而不是 hexdigest()
来获取原始字节散列:
>>> digest = hashlib.sha1(b"im a very good speaker").digest()
最后,在比较之前将其转换为列表:
>>> list(digest) == my_digest
True