首页 > 编程语言 > 详细

python3 hmac模块

时间:2019-05-29 12:05:46      阅读:410      评论:0      收藏:0      [点我收藏+]

hmac: hex-based message authentication code 哈希消息认证码

需要注意传入的key和message都是bytes类型,str类型需要首先编码为bytes

 

import hmac

secret_key1 = bThis is my secret key
message1 = bHello world
hex_res1 = hmac.new(secret_key1, message1, digestmod="MD5").hexdigest()
print(hex_res1)  # b8908a20bd70f465330b434e18441d3b

secret_key2 = bThis is my secret key
message2 = bHello world
hex_res2 = hmac.new(secret_key2, message2, digestmod="MD5").hexdigest()
print(hex_res2)  # b8908a20bd70f465330b434e18441d3b

compare_res = hmac.compare_digest(hex_res1, hex_res2)  # 比较两个密文是否相同
print(compare_res)  # True

secret_key3 = bThis is my secret key
message3 = bHello world!
hex_res3 = hmac.new(secret_key3, message3, digestmod="MD5").hexdigest()
print(hex_res3)  # a314490e13ff3d1dfa9cd18db8c4c3e8

compare_res = hmac.compare_digest(hex_res1, hex_res3)  # 比较两个密文是否相同
print(compare_res)  # False

 

python3 hmac模块

原文:https://www.cnblogs.com/lilyxiaoyy/p/10942922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!