python3中digest()和hexdigest()区别
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方法导致加密的结果不正确
python3中digest()和hexdigest()区别的更多相关文章
- python2和python3中TestSuite().addTest的区别
Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam&qu ...
- Python3中__repr__和__str__区别
示例: class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t ...
- python3 中is和==的区别
is 身份运算符,用来判断对象是否属于同一地址 (python内置函数id() 可以返回对象地址) == 比较运算符,用于判断值是否相同
- python3中的 zip()函数 和python2中的 zip()函数 的区别
python3中的 zip()函数 和python2中的 zip()函数 的区别: 描述: zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象. ...
- python2和python3中range的区别
参考自 python2和python3中的range区别 - CSDN博客 http://blog.csdn.net/xiexingshishu/article/details/48581379 py ...
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- python2与python3中除法的区别
python2中的除法 >>>1/2 0 即一个整数(无小数部分的数)被另外一个整数除,计算结果的小数部分被截除了,只留下了整数部分 有时候,这个功能比较有用,譬如在做一些需要取位数 ...
- python2 与python3中最大的区别(编码问题bytes&str
1,在python2.x 中是不区分bytes和str类型的,在python3中bytes和str中是区分开的,str的所有操作bytes都支持 python2 中 >>> s = ...
- python2和python3中str,bytes区别
python2中,有basestring.str.bytes.unicode四种类型 其中str == bytes ,basestring = (str,unicode) >>> i ...
随机推荐
- bzoj 2463 [中山市选2009]谁能赢呢? 博弈
[中山市选2009]谁能赢呢? Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3014 Solved: 2165[Submit][Status][D ...
- bzoj 1700 Problem Solving 解题 dp
[Usaco2007 Jan]Problem Solving 解题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 492 Solved: 288[Sub ...
- Hihocoder #1067 : 最近公共祖先·二
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中 ...
- Memcached 管理与监控工具 MemAdmin
MemAdmin是一款可视化的Memcached管理与监控工具,基于 PHP5 & JQuery 开发,体积小,操作简单. 主要功能: 服务器参数监控:STATS.SETTINGS.ITEMS ...
- HDU 1028 整数拆分 HDU 2082 找单词 母函数
生成函数(母函数) 母函数又称生成函数.定义是给出序列:a0,a1,a2,...ak,...an, 那么函数G(x)=a0+a1*x+a2*x2+....+ak*xk +...+an* xn 称为序 ...
- Scrambled Polygon--poj2007(极角排序模板)
http://poj.org/problem?id=2007 #include<stdio.h> #include<math.h> #include<algorithm& ...
- Java日志框架-Logback手册中文版以及官方配置文档教程
Logback手册中文版:(链接: https://pan.baidu.com/s/1bpMyasR 密码: 6u5c),虽然版本有点旧,但是大体意思差不多,先用中文版了解个大概,然后一切最新的配置以 ...
- OSGI是什么
OSGI(Open Services Gateway Initiative),或者通俗点说JAVA动态模块系统,定义了一套模块应用开发的框架.OSGI容器实现方案如Knopflerfish, Equi ...
- Myeclipse配置jad
下载地址:http://pan.baidu.com/s/1bnpMEuF 1.下载jad158g.win.zip 下载后解压.解压缩后将jad.exe拷贝到自定义的文件夹内:我这里用的是D:/jad/ ...
- 定制 ArcEngine 要素编辑工具
来自:http://blog.sina.com.cn/s/blog_4d780fc10101d2d5.html 先初步了解到大概用到的下面的接口和类: IEngineEditor IEngineEdi ...