hashlib使用时出现: Unicode-objects must be encoded before hashing

2023-05-16,,

# hashlib.md5(data)函数中,data参数的类型应该是bytes
# hash前必须把数据转换成bytes类型
>>> from hashlib import md5
File "<stdin>", line 1, in <module>
>>> c = md5("helloworld")
TypeError: Unicode-objects must be encoded before hashing
>>> c = md5("helloworld".encode("utf-8"))
>>> print(c.hexdigest())
fc5e038d38a57032085441e7fe7010b0

hashlib使用时出现: Unicode-objects must be encoded before hashing的相关教程结束。

《hashlib使用时出现: Unicode-objects must be encoded before hashing.doc》

下载本文的Word格式文档,以方便收藏与打印。