hashlib和hmac模块

import hashlib

#创建md5加密实例
md5 = hashlib.md5()

#update必须是bytes类型
md5.update(b"Hello,xiaobai!")

#打印十六进制哈希值
print(md5.hexdigest())
md5.update(b"Nice to meet you!")

# md5.update(b"Hello,xiaobai!Nice to meet you!")   #和update分开写得到的哈希值一样

print(md5.hexdigest())

#创建sha256加密实例
sha256 = hashlib.sha256()
#如果写中文,要encode成bytes类型
sha256.update("你好啊!".encode())
print(sha256.hexdigest())

import hmac

h = hmac.new('天王盖地虎'.encode(),"小鸡炖蘑菇".encode())
print(h.hexdigest())

  

猜你喜欢

转载自www.cnblogs.com/ericbai/p/8917323.html