import hashlib
# 待加密信息
str1 = "this is md5 test"
# 创建md5对象
m = hashlib.md5()
# tips
# 此处必须encode
# 若写法为m.update(str) 报错为:Unicode-objects must be encoded before hashing
# 因为python3里默认的str是unicode
# 或者b = bytes(str, encoding='utf-8'), 作用相同,都是encode为bytes
b = str1.encode(encoding='utf-8')
m.update(b)
str1_md5 = m.hexdigest()
print("MD5加密前为:" + str1)
print("MD5加密后为:" + str1_md5)
# 另一种写法:b " 前缀代表的就是bytes
str2_md5 = hashlib.md5(b'this is a md5 test').hexdigest()
print("MD5加密后为:" + str2_md5)
···