hashlib模块
import hashlib
md=hashlib.md5()
md.update("你好")
md=md.hexdigest()
print(md)
出现以下错误:
D:\PycharmProjects\untitled\venv\Scripts\python.exe D:\PycharmProjects\untitled\myself\test2.py
Traceback (most recent call last):
File "D:\PycharmProjects\untitled\myself\test2.py", line 4, in <module>
md.update("你好")
TypeError: Unicode-objects must be encoded before hashing
由上图可知我们是没有对字符串进行编码,因为update()的括号里不支持将字符串对象引入,要将update括号里的字符串以一种编码格式(最好是utf-8)进行编码
import hashlib #导入hashlib模块
md=hashlib.md5() #将hashlib.md5的算法赋值给md
md.update("你好"