欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python的md5

程序员文章站 2024-03-20 20:04:28
...

Python之Md5()使用

Md5()

功能:MD5签名是一个哈希函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示);可用于文件命名
传入参数:bytes类型
导入:from hashlib import md5
例子1:

import requests
from hashlib import md5
url = "http://p3-tt.bytecdn.cn/list/pgc-image/cd7367fde2454137b2874f62dba5c43f"
md5_url = md5(url.encode('utf8')).hexdigest()
print(md5_url)  # 2f7108ac307fd06f5995948f35a70f2f

response = requests.get(url)  
md5_content = md5(response.content).hexdigest()
print(md5_content)  # bf93cde0b6edb50b9e95106c11fdd5ad

例子2:

from hashlib import md5
chinese = '你好呀'
md5_chinese = md5(chinese.encode('utf8')).hexdigest()
print(md5_chinese)  # 4f65fdb33e0f2bd0de6bd1b41fdea968

例子3:

from hashlib import md5
english = 'd8hgeo-2d73k0s2'
md5_str = md5(english.encode('utf8')).hexdigest()
print(md5_str)  # 74335cc0ff464e7006d1530260385a52
 

原文链接:https://blog.csdn.net/weixin_43411585/article/details/88863222

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方法导致加密的结果不正确

相关标签: 开发笔记