我有一个生成的 MD5 哈希值,我想将其与字符串中的另一个 MD5 哈希值进行比较。下面的陈述是错误的,尽管它们在打印时看起来是一样的并且应该是正确的。
hashlib.md5("foo").hexdigest() == "acbd18db4cc2f85cedef654fccc4a4d8"
谷歌告诉我,我应该对结果进行编码hexdigest()
,因为它不返回字符串。但是,下面的代码似乎也不起作用。
hashlib.md5("foo").hexdigest().encode("utf-8") == "foo".encode("utf-8")
Python 2.7,.hexdigest() 确实返回一个 str
>>> hashlib.md5("foo").hexdigest() == "acbd18db4cc2f85cedef654fccc4a4d8"
True
>>> type(hashlib.md5("foo").hexdigest())
<type 'str'>
Python 3.1
.md5() 不采用 unicode(“foo”就是),因此需要将其编码为字节流。
>>> hashlib.md5("foo").hexdigest()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
hashlib.md5("foo").hexdigest()
TypeError: Unicode-objects must be encoded before hashing
>>> hashlib.md5("foo".encode("utf8")).hexdigest()
'acbd18db4cc2f85cedef654fccc4a4d8'
>>> hashlib.md5("foo".encode("utf8")).hexdigest() == 'acbd18db4cc2f85cedef654fccc4a4d8'
True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)