简体   繁体   English

检查字符串上的SHA1

[英]Checking SHA1 on a String

The word Fox produces the following sha1 hash: 单词Fox产生以下sha1哈希:

dfcd3454bbea788a751a696c24d97009ca992d17

In python I'm simply trying to get this same output by doing the following: 在python中,我只是通过执行以下操作来尝试获得相同的输出:

import hashlib

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

myhash just produces the following byte object: myhash只生成以下字节对象:

b'\\xdf\\xcd4T\\xbb\\xeax\\x8au\\x1ail$\\xd9p\\t\\xca\\x99-\\x17'

I've tried binascii and none of the methods there seem to be able to produce the above output. 我尝试了binascii ,似乎没有任何方法能够产生上述输出。

How can I produce the resulting ascii hash from here? 如何从这里生成生成的ascii哈希?

You have a hexadecimal representation of a digest. 您有一个摘要的十六进制表示 You can use the hash.hexdigest() method to produce the same in Python: 您可以使用hash.hexdigest()方法在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'

You could also convert the binary digest to hexadecimal with the binascii.hexlify() function : 您还可以使用binascii.hexlify()函数将二进制摘要转换为十六进制:

>>> import binascii
>>> binascii.hexlify(myhash.digest())
b'dfcd3454bbea788a751a696c24d97009ca992d17'
>>> binascii.hexlify(myhash.digest()).decode('ascii')
'dfcd3454bbea788a751a696c24d97009ca992d17'

However, that's just a more verbose way of achieving the same thing. 然而,这只是实现同样事情的更冗长的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM