介绍
这篇文章将为大家详细讲解有关如何在Python中使用hmac模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
hmac模块的作用:
用于验证信息的完整性。
<强> 1,hmac消息签名(默认使用MD5加算法)强>
hmac_md5。py
# !/usr/bin/env python #,- *安康;编码:utf-8 - * - import hmac #默认使用是md5算法 时间=digest_maker hmac.new (& # 39; secret-shared-key& # 39; .encode (& # 39; utf - 8 # 39;)) with 开放(& # 39;content.txt& # 39;,, & # 39; rb # 39;), as f: while 才能正确的: ,,,block =, f.read (1024) ,,,if not 块: ,,,,,休息 ,,,digest_maker.update(块) 时间=digest digest_maker.hexdigest () 打印(消化)
内容。txt
Lorem ipsum dolor sit amet,, consectetuer adipiscing elit只Donec 排泄物,enim et  consectetuer ullamcorper,, lectus ligula rutrum 利奥,一个 elementum elit  tortor eu  quam只Duis tincidunt nisi ut 赌注只木棒 facilisi只Sed tristique  eros eu 自由后卫只Pellentesque vel arcu只Vivamus 奥利奇,purus iaculis 交流,suscipit sit amet,, pulvinar 欧盟、 湖只Praesent placerat  tortor sed nisl只Nunc blandit diam 排泄物 酒后驾车只Pellentesque habitant  morbi tristique senectus et netus 等 malesuada fames  ac turpis 排泄物只Aliquam viverra fringilla 狮子座只Nulla feugiat  augue eleifend 木棒只Vivamus 毛里只Vivamus sed mauris nibh 拷贝;placerat 排泄物只Suspendisse potenti只mauris 马萨只Ut eget velit  auctor tortor blandit  sollicitudin只Suspendisse imperdiet 胡斯托。
运行效果
[root@ mnt] #, python3 hmac_md5.py 79 cbf5942e8f67be558bc28610c02117
<强> 2,hmac消息签名摘要(使用SHA1加算法)强>
hmac_sha1。py
# !/usr/bin/env python #,- *安康;编码:utf-8 - * - import hmac 时间=digest_maker hmac.new (& # 39; secret-shared-key& # 39; .encode (& # 39; utf - 8 # 39;),, b # 39; & # 39;,, digestmod=& # 39; sha1 # 39;) #,hmac.new(味精,关键digestmod) #,关键:加盐的关键, #,味精:加密的内容, #,digestmod:加密的方式 with 开放(& # 39;hmac_sha1.py& # 39;,, & # 39; rb # 39;), as f: while 才能正确的: ,,,block =, f.read (1024) ,,,if not 块: ,,,,,休息 ,,,digest_maker.update(块) 时间=digest digest_maker.hexdigest () 打印(消化)
运行效果
[root@ mnt] #, python3 hmac_sha1.py e5c012eac5fa76a274f77ee678e6cc98cad8fff9
<强> 3,hmac二进制消息签名摘要(使用SHA1加算法)强>
hmac_base64。py
# !/usr/bin/env python #,- *安康;编码:utf-8 - * - import hmac import base64 import hashlib with 开放(& # 39;test.py& # 39;,, & # 39; rb # 39;), as f: 时间=body 才能;f.read () #,默认使用是md5算法 时间=digest_maker hmac.new (& # 39; secret-shared-key& # 39; .encode (& # 39; utf - 8 # 39;),,,, hashlib.sha1) #,hmac.new(味精,关键digestmod) #,关键:加盐的关键, #,味精:加密的内容, #,digestmod:加密的方式 时间=digest digest_maker.digest(), #,默认内容是字节类型,所以需要base64 打印(base64.encodebytes(消化),#注意base64结果是以\ n结束,所以Http头部或其它传输时,需要去除\ n
运行效果
[root@ mnt] #, python3 hmac_base64.py b # 39; Y9a4OMRqU4DB6Ks/hGfru + MNXAw=\ n # 39;
<强> 4,hmac摘,要数据比较示例强>
hmac_pickle。py
# !/usr/bin/env python #,- *安康;编码:utf-8 - * - import hashlib import hmac import io import 泡菜 def make_digest(信息): “才能返消息摘要,加密码后的结果, 时间=hash 才能;hmac.new ( ,,,& # 39;secret-shared-key& # 39; .encode (& # 39; utf - 8 # 39;), ,,,的信息, ,,hashlib.sha1 ,,) return 才能;hash.hexdigest () .encode (& # 39; utf - 8 # 39;) class SimpleObject(对象): def 才能__init__(自我,,名字): ,,,self.name =,名字 def 才能__str__(自我): ,,,return self.name #,输出缓冲区 时间=out_s io.BytesIO () 时间=o SimpleObject (& # 39; digest 比赛# 39;) 时间=pickle_data pickle.dumps (o), #,序列化 时间=digest make_digest (pickle_data), #,使用sha1加密算法 时间=header b # 39; % s , % d \ n # 39;, %,(消化,,len (pickle_data)) 打印(& # 39;提示:{}& # 39;.format(头) null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在Python中使用hmac模块