PyCon 2011 - Hidden Treasures of the Python Standard Library - 模拟hmac摘要算法的实现

本文介绍了HMAC(散列消息鉴别码)的概念及其工作原理,通过一个Python示例展示了如何使用HMAC算法来确保消息在传输过程中的完整性和真实性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-03-28


hmac简介


HMAC即(Hash Message Authentication Code) 中文名称是:散列消息鉴别码,基于密钥的Hash算法的认证协议。消息鉴别码实现鉴别的原理是,用公开函数和密钥产生一个固定长度的值作为认证标识,用这个标识鉴别消息的完整性。使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输。接收方利用与发送方共享的密钥进行鉴别认证等。

发送方首先使用HMAC算法和共享密钥计算校验和

message = 'The message' encoded_message = pickle.dumps(message) ################################################################################ # 发送方首先使用HMAC算法和共享密钥计算消息检查和 digest_maker = hmac.new('shared-secret-value') # 密钥 digest_maker.update(encoded_message) # 加密串 signature = digest_maker.hexdigest() # 生成签名 print 'Outgoing signature:', signature

将计算结果A封装进数据包中一起发送


################################################################################ # 将计算结果A封装进数据包中一起发送 # Simulate sending the message buffer = StringIO('%s\n%d\n%s' % (signature, # 发送签名 len(encoded_message), # 发送信息长度 encoded_message # 发送信息 ))

接受方接受封装的数据包


################################################################################ # 接受方接受封装的数据包 # "Receive" the message read_signature = buffer.readline().rstrip() # 获得签名 message_len = int(buffer.readline()) # 获得发送的信息长度 read_message = buffer.read(message_len) # 获得信息

接收方再对所接收的消息执行HMAC计算得出结果B


################################################################################ # 接收方再对所接收的消息执行HMAC计算得出结果B # Check the signature of the incoming data digest_maker = hmac.new('shared-secret-value', read_message) computed_signature = digest_maker.hexdigest() print 'Computed signature:', signature

将B与A进行比较。如果消息在传输中遭篡改致使B与A不一致, 接收方则发出异常


################################################################################ # 将B与A进行比较。如果消息在传输中遭篡改致使B与A不一致, 接收方丢弃该数据包。 if computed_signature == read_signature: print '\nValid message, processed' safe_message = pickle.loads(read_message) print 'Message:', safe_message else: raise ValueError('Invalid message, discarded')

运行结果


# python hmac_message_signing.py Outgoing signature: 59b586ff81ed215c7bd1ad3583ffb400 Computed signature: 59b586ff81ed215c7bd1ad3583ffb400 Valid message, processed Message: The message

完整源码


#!/usr/bin/env python # encoding: utf-8 """Signing and verifying messages with hmac """ import hashlib import hmac from cStringIO import StringIO import cPickle as pickle #import pickle message = 'The message' encoded_message = pickle.dumps(message) ################################################################################ # 发送方首先使用HMAC算法和共享密钥计算消息检查和 digest_maker = hmac.new('shared-secret-value') # 密钥 digest_maker.update(encoded_message) # 加密串 signature = digest_maker.hexdigest() # 生成签名 print 'Outgoing signature:', signature ################################################################################ # 将计算结果A封装进数据包中一起发送 # Simulate sending the message buffer = StringIO('%s\n%d\n%s' % (signature, # 发送签名 len(encoded_message), # 发送信息长度 encoded_message # 发送信息 )) ################################################################################ # 接受方接受封装的数据包 # "Receive" the message read_signature = buffer.readline().rstrip() # 获得签名 message_len = int(buffer.readline()) # 获得发送的信息长度 read_message = buffer.read(message_len) # 获得信息 ################################################################################ # 接收方再对所接收的消息执行HMAC计算得出结果B # Check the signature of the incoming data digest_maker = hmac.new('shared-secret-value', read_message) computed_signature = digest_maker.hexdigest() print 'Computed signature:', signature ################################################################################ # 将B与A进行比较。如果消息在传输中遭篡改致使B与A不一致, 接收方丢弃该数据包。 if computed_signature == read_signature: print '\nValid message, processed' safe_message = pickle.loads(read_message) print 'Message:', safe_message else: raise ValueError('Invalid message, discarded')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值