打印 SHA-256 哈希的两种方式在 Python 中有 1 个字节的差异
Two ways of printing a SHA-256 hash differ in 1 byte in Python
考虑片段:
from Cryptodome.Hash import SHA256
text = b'Jeanny'
print('Hash of', text)
hx = SHA256.new(text).hexdigest()
print(hx)
h = SHA256.new(text).digest()
[print('{0:x}'.format(h[i]), end = '' ) for i in range(0,len(h))]
它打印:
Hash of b'Jeanny'
f51c7dbd56cc25c565f7c7ef951b06121e87e34f2e3bb466e873a2518715fe50
f51c7dbd56cc25c565f7c7ef951b6121e87e34f2e3bb466e873a2518715fe50
为什么第二个打印的十六进制数字字符串在第 29 位缺少 0
?
因为它正在尝试打印“06”,但您没有告诉它对数字进行零填充。
考虑片段:
from Cryptodome.Hash import SHA256
text = b'Jeanny'
print('Hash of', text)
hx = SHA256.new(text).hexdigest()
print(hx)
h = SHA256.new(text).digest()
[print('{0:x}'.format(h[i]), end = '' ) for i in range(0,len(h))]
它打印:
Hash of b'Jeanny'
f51c7dbd56cc25c565f7c7ef951b06121e87e34f2e3bb466e873a2518715fe50
f51c7dbd56cc25c565f7c7ef951b6121e87e34f2e3bb466e873a2518715fe50
为什么第二个打印的十六进制数字字符串在第 29 位缺少 0
?
因为它正在尝试打印“06”,但您没有告诉它对数字进行零填充。