吾知网

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 7484|回复: 0
打印 上一主题 下一主题

python中digest()和hexdigest()区别

[复制链接]
跳转到指定楼层
楼主
发表于 2019-1-17 11:21:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法接口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。

其中

hash.digest() 
返回摘要,作为二进制数据字符串值

hash.hexdigest() 
返回摘要,作为十六进制数据字符串值

 

举个例子

复制代码
import hashlib

md5 = hashlib.md5()
md5.update("a".encode('utf-8'))
print(u"digest返回的摘要:%s"% md5.digest())
print(u"hexdigest返回的摘要:%s"% md5.hexdigest())
复制代码

结果

digest返回的摘要:b'\x0c\xc1u\xb9\xc0\xf1\xb6\xa81\xc3\x99\xe2iw&a'
hexdigest返回的摘要:0cc175b9c0f1b6a831c399e269772661

 

众配宝接口的加密规则是java实现的,转换成python

加密规则:1.请求报文 2.替换请求报文中的字符串(replace("\\>\\s+\\<", "><")) 3.替换后的字符串+key('alogalog') 

4.第3步获取的字符串md5加密  5.将加密后的字符串base64编码

python实现加密

复制代码
import hashlib
import base64

md5 = hashlib.md5()


a = "<request><waybills><waybill><receiver><receiverName>张三</receiverName><receiverMobile>13000000000</receiverMobile><receiverZip>431400</receiverZip><receiverProvince>甘肃省</receiverProvince><receiverCity>兰州市</receiverCity><receiverArea>新洲区</receiverArea><receiverDistrict>李集街道</receiverDistrict><receiverAddress>天水南路222号</receiverAddress></receiver><sender><shopName>天猫超市</shopName><senderName>天猫超市仓库</senderName><senderPhone>02781739210</senderPhone><senderZip>430208</senderZip><senderProvince>甘肃省</senderProvince><senderCity>兰州市</senderCity><senderArea>新洲区</senderArea><senderAddress>金口街旭光村菜鸟物流园3号库</senderAddress></sender><packageInfo><packageCode>test0926001</packageCode><isExpensive>false</isExpensive><weight>2302</weight><volume>7888000</volume><length>290</length><width>170</width><height>160</height><storeOutTime>2017-09-22 08:55:04</storeOutTime></packageInfo><carrier/><sortingInfo><routetype>1</routetype><storeCode>pressureTest</storeCode><deliveryCode>CHENGBANGPEISONG-0001</deliveryCode><deliveryWlbCode>NJCB-001</deliveryWlbCode><cpSimplyCode>C</cpSimplyCode><citySimplyCode>H1</citySimplyCode><routePath>{'nextRouteId':890,'nextRouteType':2,'targerRdcType':2,'targetRdcId':890}</routePath><siteId>4859</siteId><siteCode>1619095</siteCode><carrierCode>CBWL</carrierCode><sortingRequireTimes><requireSendTime>2017-09-24 23:59:00</requireSendTime></sortingRequireTimes><sortingService><expressSerType>108</expressSerType></sortingService></sortingInfo><order><lgOrderSource>WLB</lgOrderSource><storeOrderCode>ydhtest1341573</storeOrderCode><logisticsId>LP00079477100697</logisticsId><mailNo>ddhtest5454253</mailNo><customerCode>SB-ZFB</customerCode><deliveryType>1</deliveryType><distributionType>1</distributionType></order><deliveryNodeInfo><nodeCode>1619095</nodeCode><nodeName>晟邦湖北分拨中心</nodeName><deliveryStatus>MainWaybillAccess</deliveryStatus><firstOwnerRdcCode>1619095</firstOwnerRdcCode></deliveryNodeInfo><uniqueCode>MainWaybillAccesstest09260012017-09-22 09:13:11</uniqueCode><remark>zpb_chuyan_cb</remark></waybill></waybills></request>"
b = a.replace("\\>\\s+\\<", "><")+"alogalog"


md5.update(b.encode('utf-8'))
b = md5.digest()
print(u"16位md5加密结果:%s"% b)
print(u"16位md5加密结果再进行base64编码:%s" % base64.b64encode(b).decode('utf-8')
)
复制代码

结果

16位md5加密结果:b'(\xb1\xf9\xd9\xf4\x90\x90jN;\n~\x82)FF'
16位md5加密结果再进行base64编码:KLH52fSQkGpOOwp+gilGRg==

加密结果与java实现的加密结果一致

注意:区分加密是hash.digest() 还是hash.hexdigest(),之前一直用的是hexdigest方法导致加密的结果不正确

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|吾知网 ( 粤ICP备13013563号-1 )

GMT+8, 2024-12-22 10:34 , Processed in 1.109375 second(s), 8 queries , Memcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表