Python 实现MD5加密

from hashlib import md5


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


# 调用
if __name__ == '__main__':
    print(encrypt_md5('admin'))

输出结果:

21232f297a57a5a743894a0e4a801fc3

 

posted @ 2018-09-19 17:06  寒爵  阅读(7354)  评论(0编辑  收藏  举报