python中digest()和hexdigest()区别
<p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"><strong>hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法接口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。</strong></p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"><strong>其中</strong></p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"><code>hash.digest()</code> <br>返回摘要,作为二进制数据字符串值</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"><code>hash.hexdigest()</code> <br>返回摘要,作为十六进制数据字符串值</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">举个例子</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="text-decoration-line: underline; color: rgb(0, 102, 204); border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span><span style="line-height: 1.5 !important;"> hashlibmd5 </span>=<span style="line-height: 1.5 !important;"> hashlib.md5()
md5.update(</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">a</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>.encode(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">utf-8</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">))
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(u<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">digest返回的摘要:%s</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>%<span style="line-height: 1.5 !important;"> md5.digest())
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(u<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">hexdigest返回的摘要:%s</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>% md5.hexdigest())</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="text-decoration-line: underline; color: rgb(0, 102, 204); border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div></div><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">结果</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;">digest返回的摘要:b<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">\x0c\xc1u\xb9\xc0\xf1\xb6\xa81\xc3\x99\xe2iw&a</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">
hexdigest返回的摘要:0cc175b9c0f1b6a831c399e269772661</span></pre></div><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">众配宝接口的加密规则是java实现的,转换成python</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">加密规则:1.请求报文 2.替换请求报文中的字符串(replace("\\>\\s+\\<", "><")) 3.替换后的字符串+key('alogalog') </p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">4.第3步获取的字符串md5加密 5.将加密后的字符串base64编码</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">python实现加密</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="text-decoration-line: underline; color: rgb(0, 102, 204); border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span><span style="line-height: 1.5 !important;"> hashlib
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span><span style="line-height: 1.5 !important;"> base64
md5 </span>=<span style="line-height: 1.5 !important;"> hashlib.md5()
a </span>= <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;"><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></span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">
b </span>= a.replace(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">\\>\\s+\\<</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>)+<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">alogalog</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">
md5.update(b.encode(</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">utf-8</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">))
b </span>=<span style="line-height: 1.5 !important;"> md5.digest()
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(u<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">16位md5加密结果:%s</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>%<span style="line-height: 1.5 !important;"> b)
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(u<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">16位md5加密结果再进行base64编码:%s</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span> % base64.b64encode(b).decode('utf-8')</pre><pre style="white-space: pre-wrap; font-family: "Courier New" !important;">)</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="text-decoration-line: underline; color: rgb(0, 102, 204); border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div></div><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">结果</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><p style="margin: 10px auto;">16位md5加密结果:b'(\xb1\xf9\xd9\xf4\x90\x90jN;\n~\x82)FF'<br>16位md5加密结果再进行base64编码:KLH52fSQkGpOOwp+gilGRg==</p></div><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">加密结果与java实现的加密结果一致</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"><span style="color: rgb(255, 0, 0);">注意:区分加密是<code>hash.digest()</code> 还是hash.hexdigest(),之前一直用的是hexdigest方法导致加密的结果不正确</span></p><p></p>
页:
[1]