Python hmac模塊使用實(shí)例解析
這篇文章主要介紹了Python hmac模塊使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
hmac模塊的作用:
用于驗(yàn)證信息的完整性。
1、hmac消息簽名(默認(rèn)使用MD5加算法)
hmac_md5.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import hmac #默認(rèn)使用是md5算法 digest_maker = hmac.new('secret-shared-key'.encode('utf-8')) with open('content.txt', 'rb') as f: while True: block = f.read(1024) if not block: break digest_maker.update(block) digest = digest_maker.hexdigest() print(digest)
content.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec egestas, enim et consectetuer ullamcorper, lectus ligula rutrum leo, a elementum elit tortor eu quam. Duis tincidunt nisi ut ante. Nulla facilisi. Sed tristique eros eu libero. Pellentesque vel arcu. Vivamus purus orci, iaculis ac, suscipit sit amet, pulvinar eu, lacus. Praesent placerat tortor sed nisl. Nunc blandit diam egestas dui. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam viverra fringilla leo. Nulla feugiat augue eleifend nulla. Vivamus mauris. Vivamus sed mauris in nibh placerat egestas. Suspendisse potenti. Mauris massa. Ut eget velit auctor tortor blandit sollicitudin. Suspendisse imperdiet justo.
運(yùn)行效果
[root@ mnt]# python3 hmac_md5.py 79cbf5942e8f67be558bc28610c02117
2、hmac消息簽名摘要(使用SHA1加算法)
hmac_sha1.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import hmac digest_maker = hmac.new('secret-shared-key'.encode('utf-8'), b'', digestmod='sha1') # hmac.new(key,msg,digestmod) # key:加鹽的key, # msg:加密的內(nèi)容, # digestmod:加密的方式 with open('hmac_sha1.py', 'rb') as f: while True: block = f.read(1024) if not block: break digest_maker.update(block) digest = digest_maker.hexdigest() print(digest)
運(yùn)行效果
[root@ mnt]# python3 hmac_sha1.py e5c012eac5fa76a274f77ee678e6cc98cad8fff9
3、hmac二進(jìn)制消息簽名摘要(使用SHA1加算法)
hmac_base64.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import hmac import base64 import hashlib with open('test.py', 'rb') as f: body = f.read() # 默認(rèn)使用是md5算法 digest_maker = hmac.new('secret-shared-key'.encode('utf-8'), body, hashlib.sha1) # hmac.new(key,msg,digestmod) # key:加鹽的key, # msg:加密的內(nèi)容, # digestmod:加密的方式 digest = digest_maker.digest() # 默認(rèn)內(nèi)容是字節(jié)類型,所以需要base64 print(base64.encodebytes(digest)) #注意base64結(jié)果是以\n結(jié)束,所以Http頭部或其它傳輸時(shí),需要去除\n
運(yùn)行效果
[root@ mnt]# python3 hmac_base64.py b'Y9a4OMRqU4DB6Ks/hGfru+MNXAw=\n'
4、hmac摘要數(shù)據(jù)比較示例
hmac_pickle.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import hashlib import hmac import io import pickle def make_digest(message): "返消息摘要,加密碼后的結(jié)果" hash = hmac.new( 'secret-shared-key'.encode('utf-8'), message, hashlib.sha1 ) return hash.hexdigest().encode('utf-8') class SimpleObject(object): def __init__(self, name): self.name = name def __str__(self): return self.name # 輸出緩沖區(qū) out_s = io.BytesIO() o = SimpleObject('digest matches') pickle_data = pickle.dumps(o) # 序列化 digest = make_digest(pickle_data) # 使用sha1加密算法 header = b'%s %d\n' % (digest, len(pickle_data)) print('提示:{}'.format(header)) out_s.write(header) # 將消息頭寫入緩沖區(qū) out_s.write(pickle_data) # 將序列化內(nèi)容寫入緩沖區(qū) o = SimpleObject('digest does not matches') pickle_data = pickle.dumps(o) digest = make_digest(b'not the pickled data at all') header = b'%s %d\n' % (digest, len(pickle_data)) print('提示:{}'.format(header)) out_s.write(header) # 將消息頭寫入緩沖區(qū) out_s.write(pickle_data) # 將序列化內(nèi)容寫入緩沖區(qū) out_s.flush() # 刷新緩沖區(qū) # 輸入緩沖區(qū) in_s = io.BytesIO(out_s.getvalue()) while True: first_line = in_s.readline() if not first_line: break incoming_digest, incoming_length = first_line.split(b' ') incoming_length = int(incoming_length.decode('utf-8')) print('讀取到:', incoming_digest, incoming_length) incoming_pickled_data = in_s.read(incoming_length) actual_digest = make_digest(incoming_pickled_data) # 實(shí)際的摘要 print('實(shí)際值:', actual_digest) if hmac.compare_digest(actual_digest, incoming_digest): # 比較兩個(gè)摘要是否相等 obj = pickle.loads(incoming_pickled_data) print('OK:', obj) else: print('數(shù)據(jù)不完整')
運(yùn)行效果
[root@ mnt]# python3 hmac_pickle.py 提示:b'00e080735a8de379e19fe2aa731c92fc9253a6e2 69\n' 提示:b'1d147690f94ea374f6f8c3767bd5a5f9a8989a53 78\n' 讀取到: b'00e080735a8de379e19fe2aa731c92fc9253a6e2' 69 實(shí)際值: b'00e080735a8de379e19fe2aa731c92fc9253a6e2' OK: digest matches 讀取到: b'1d147690f94ea374f6f8c3767bd5a5f9a8989a53' 78 實(shí)際值: b'4dcaad9b05bbb67b571a64defa52e8960a27c45d' 數(shù)據(jù)不完整
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何從csv文件構(gòu)建Tensorflow的數(shù)據(jù)集
這篇文章主要介紹了如何從csv文件構(gòu)建Tensorflow的數(shù)據(jù)集,幫助大家更好的理解和使用Tensorflow,感興趣的朋友可以了解下2020-09-09python:刪除離群值操作(每一行為一類數(shù)據(jù))
這篇文章主要介紹了python:刪除離群值操作(每一行為一類數(shù)據(jù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素
今天小編就為大家分享一篇python實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python中使用Selenium環(huán)境安裝的方法步驟
這篇文章主要介紹了Python中使用Selenium環(huán)境安裝的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python實(shí)現(xiàn)類的創(chuàng)建與使用方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)類的創(chuàng)建與使用方法,結(jié)合簡(jiǎn)單計(jì)算器功能實(shí)例分析了Python類的定義與使用方法,需要的朋友可以參考下2017-07-07