的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