Python 实现MD5加密

python学习网 2018-09-20 08:43:04
# MD5加密 #
import hashlib

def encrypt_md5(str=''):
    md = hashlib.md5()  # 创建md5对象
    md.update(str.encode(encoding='utf-8'))  # 这里必须用encode()函数对字符串进行编码,不然会报 TypeError: Unicode-objects must be encoded before hashing
    return md.hexdigest()  # 加密

# 调用
print(encrypt_md5('hello world'))

输入结果:
5eb63bbbe01eeed093cb22bb8f5acdc3
    

 

阅读(2055) 评论(0)