#!/usr/bin/env python # -*- coding: utf-8 -*- ''' hashlib:用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ''' import hashlib # ######## md5 ######## hash = hashlib.md5() #创建一个md5对象 # help(hash.update) hash.update(bytes('admin', encoding='utf-8')) #进行加密,python3中参数必须为字节类型(bytes),python2不需要 print(hash.hexdigest()) #获取md5加密后的结果 print(hash.digest()) #二进制表示加密结果 ''' 下面两种加密等价 import hashlib s = 'lihui' hash = hashlib.md5() hash.update(bytes('123',encoding='utf-8')) hash.update(bytes(s,encoding='utf-8')) print(hash.hexdigest()) s = 'lihui' hash = hashlib.md5(bytes('123',encoding='utf-8')) hash.update(bytes(s,encoding='utf-8')) print(hash.hexdigest()) ''' ######## sha1 ######## hash = hashlib.sha1() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest())
关注我的公众号,不定期推送资讯
本文来自博客园,作者:链条君,转载请注明原文链接:https://www.cnblogs.com/MacoLee/articles/6030029.html