的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
你最后一段代码工作正常。不知何故,我在AppEngine开发服务器上测试时没有发现错误信息。我应该在python控制台中测试它。我道歉并且会在下次做。 – nip3o 2010-08-27 11:49:15