python内置模块之hashlib/hmac模块

#hashlib模块
#用于加密相关操作,3.x版本以后代替了2.x的md5和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法
import  hashlib
trc=hashlib.md5()#生成一个md5对象
trc.update(b"hi")
print(trc.hexdigest())#打印出hi对应的16进制编码
trc.update(b"hello")
print(trc.hexdigest())#打印出hi+hello对应的16进制编码
trc1=hashlib.md5()
trc1.update(b"hihello")
print(trc1.hexdigest())


#如果觉得还不够安全,那就用hmac模块,内部对我们创建的key 和内容再进行处理然后再加密。相当于双层加密
import hmac
#h=hmac.new(b"123",b"nihao")
h=hmac.new(b"nihao")
h.update(b"hellohi")
print(h.hexdigest())#

#如果是中文要转码后面加.encode(encoding="utf-8")

猜你喜欢

转载自blog.csdn.net/qq_37181884/article/details/81676600