jekeyhui99 发表于 2019-1-17 11:21:19

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>&nbsp;<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>&nbsp;<br>返回摘要,作为十六进制数据字符串值</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">&nbsp;</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: &quot;Courier New&quot; !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: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span><span style="line-height: 1.5 !important;"> hashlib

md5 </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: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !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&amp;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;">&nbsp;</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("\\&gt;\\s+\\&lt;", "&gt;&lt;")) 3.替换后的字符串+key('alogalog')&nbsp;</p><p style="margin: 10px auto; color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">4.第3步获取的字符串md5加密 &nbsp;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: &quot;Courier New&quot; !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: &quot;Courier New&quot; !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;">&lt;request&gt;&lt;waybills&gt;&lt;waybill&gt;&lt;receiver&gt;&lt;receiverName&gt;张三&lt;/receiverName&gt;&lt;receiverMobile&gt;13000000000&lt;/receiverMobile&gt;&lt;receiverZip&gt;431400&lt;/receiverZip&gt;&lt;receiverProvince&gt;甘肃省&lt;/receiverProvince&gt;&lt;receiverCity&gt;兰州市&lt;/receiverCity&gt;&lt;receiverArea&gt;新洲区&lt;/receiverArea&gt;&lt;receiverDistrict&gt;李集街道&lt;/receiverDistrict&gt;&lt;receiverAddress&gt;天水南路222号&lt;/receiverAddress&gt;&lt;/receiver&gt;&lt;sender&gt;&lt;shopName&gt;天猫超市&lt;/shopName&gt;&lt;senderName&gt;天猫超市仓库&lt;/senderName&gt;&lt;senderPhone&gt;02781739210&lt;/senderPhone&gt;&lt;senderZip&gt;430208&lt;/senderZip&gt;&lt;senderProvince&gt;甘肃省&lt;/senderProvince&gt;&lt;senderCity&gt;兰州市&lt;/senderCity&gt;&lt;senderArea&gt;新洲区&lt;/senderArea&gt;&lt;senderAddress&gt;金口街旭光村菜鸟物流园3号库&lt;/senderAddress&gt;&lt;/sender&gt;&lt;packageInfo&gt;&lt;packageCode&gt;test0926001&lt;/packageCode&gt;&lt;isExpensive&gt;false&lt;/isExpensive&gt;&lt;weight&gt;2302&lt;/weight&gt;&lt;volume&gt;7888000&lt;/volume&gt;&lt;length&gt;290&lt;/length&gt;&lt;width&gt;170&lt;/width&gt;&lt;height&gt;160&lt;/height&gt;&lt;storeOutTime&gt;2017-09-22 08:55:04&lt;/storeOutTime&gt;&lt;/packageInfo&gt;&lt;carrier/&gt;&lt;sortingInfo&gt;&lt;routetype&gt;1&lt;/routetype&gt;&lt;storeCode&gt;pressureTest&lt;/storeCode&gt;&lt;deliveryCode&gt;CHENGBANGPEISONG-0001&lt;/deliveryCode&gt;&lt;deliveryWlbCode&gt;NJCB-001&lt;/deliveryWlbCode&gt;&lt;cpSimplyCode&gt;C&lt;/cpSimplyCode&gt;&lt;citySimplyCode&gt;H1&lt;/citySimplyCode&gt;&lt;routePath&gt;{'nextRouteId':890,'nextRouteType':2,'targerRdcType':2,'targetRdcId':890}&lt;/routePath&gt;&lt;siteId&gt;4859&lt;/siteId&gt;&lt;siteCode&gt;1619095&lt;/siteCode&gt;&lt;carrierCode&gt;CBWL&lt;/carrierCode&gt;&lt;sortingRequireTimes&gt;&lt;requireSendTime&gt;2017-09-24 23:59:00&lt;/requireSendTime&gt;&lt;/sortingRequireTimes&gt;&lt;sortingService&gt;&lt;expressSerType&gt;108&lt;/expressSerType&gt;&lt;/sortingService&gt;&lt;/sortingInfo&gt;&lt;order&gt;&lt;lgOrderSource&gt;WLB&lt;/lgOrderSource&gt;&lt;storeOrderCode&gt;ydhtest1341573&lt;/storeOrderCode&gt;&lt;logisticsId&gt;LP00079477100697&lt;/logisticsId&gt;&lt;mailNo&gt;ddhtest5454253&lt;/mailNo&gt;&lt;customerCode&gt;SB-ZFB&lt;/customerCode&gt;&lt;deliveryType&gt;1&lt;/deliveryType&gt;&lt;distributionType&gt;1&lt;/distributionType&gt;&lt;/order&gt;&lt;deliveryNodeInfo&gt;&lt;nodeCode&gt;1619095&lt;/nodeCode&gt;&lt;nodeName&gt;晟邦湖北分拨中心&lt;/nodeName&gt;&lt;deliveryStatus&gt;MainWaybillAccess&lt;/deliveryStatus&gt;&lt;firstOwnerRdcCode&gt;1619095&lt;/firstOwnerRdcCode&gt;&lt;/deliveryNodeInfo&gt;&lt;uniqueCode&gt;MainWaybillAccesstest09260012017-09-22 09:13:11&lt;/uniqueCode&gt;&lt;remark&gt;zpb_chuyan_cb&lt;/remark&gt;&lt;/waybill&gt;&lt;/waybills&gt;&lt;/request&gt;</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;">\\&gt;\\s+\\&lt;</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;">&gt;&lt;</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: &quot;Courier New&quot; !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: &quot;Courier New&quot; !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>&nbsp;还是hash.hexdigest(),之前一直用的是hexdigest方法导致加密的结果不正确</span></p><p></p>
页: [1]
查看完整版本: python中digest()和hexdigest()区别