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'x0cxc1uxb9xc0xf1xb6xa81xc3x99xe2iw&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'(xb1xf9xd9xf4x90x90jN;
~x82)FF'
16位md5加密结果再进行base64编码:KLH52fSQkGpOOwp+gilGRg==
加密结果与java实现的加密结果一致
注意:区分加密是hash.digest()
还是hash.hexdigest(),之前一直用的是hexdigest方法导致加密的结果不正确