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

# hash前必须把数据转换成bytes类型

1、未编码

import hashlib      # 调用hashlib模块进行md5加密
temp = 'hello123' # 需要加密的字符串
m = hashlib.md5()
m.update(temp)
temp1 = m.hexdigest()

TypeError: Unicode-objects must be encoded before hashing  # TypeError:必须在hashing之前对unicode对象进行编码

2、进行编码

temp = 'hello123' # 需要加密的字符串
m = hashlib.md5()
m.update(temp.encode('utf-8'))
temp1 = m.hexdigest()
print(temp1)
f30aa7a662c728b7407c54ae6bfd27d1

你可能感兴趣的:(python)