The word Fox produces the following sha1 hash:
dfcd3454bbea788a751a696c24d97009ca992d17
In python I'm simply trying to get this same output by doing the following:

import hashlib

myhash = hashlib.sha1("Fox".encode('utf-8'))

myhash只生成以下字节对象:
b'\xdf\xcd4T\xbb\xeax\x8au\x1ail$\xd9p\t\xca\x99-\x17'

最佳答案

您有一个十六进制的摘要表示。您可以使用hash.hexdigest() method在Python中生成相同的内容:

>>> import hashlib
>>> myhash = hashlib.sha1("Fox".encode('utf-8'))
>>> myhash.digest()
b'\xdf\xcd4T\xbb\xeax\x8au\x1ail$\xd9p\t\xca\x99-\x17'
>>> myhash.hexdigest()
'dfcd3454bbea788a751a696c24d97009ca992d17'

您还可以使用binascii.hexlify() function将二进制摘要转换为十六进制:
>>> import binascii
>>> binascii.hexlify(myhash.digest())
b'dfcd3454bbea788a751a696c24d97009ca992d17'
>>> binascii.hexlify(myhash.digest()).decode('ascii')
'dfcd3454bbea788a751a696c24d97009ca992d17'

10-08 14:23