我的代码:
filename = filename.strip().replace(' ', '_')
filename = urllib.quote(filename.encode('utf-8')).strip()
print filename
filenameHash = hashlib.md5(filename).hexdigest()
print filenameHash
Printed filenameHash:5e6243a3a207220e4e386fcde5191697
但应该是4a8ce85f21814d6d0010a3bc5bf4f82d
打印文件名:Gda%C5%84sk_%E2%80%93_Strefa_Historyczna_Wolnego_Miasta_Gda%C5%84ska_%28D%C5%82ugi_Targ%29.JPG
出了什么问题?
答案 0 :(得分:6)
您测试MD5的页面错误地解码了数据两次:
>>> import hashlib
>>> filename = 'Gda%C5%84sk_%E2%80%93_Strefa_Historyczna_Wolnego_Miasta_Gda%C5%84ska_%28D%C5%82ugi_Targ%29.JPG'
>>> hashlib.md5(filename).hexdigest()
'5e6243a3a207220e4e386fcde5191697'
>>> hashlib.md5(urllib.unquote(filename)).hexdigest()
'4a8ce85f21814d6d0010a3bc5bf4f82d'
比随机在线网页更信任Python模块。
另一方面,如果您打算测试文件名的UTF-8编码的MD5哈希值,那么就不要申请urllib.quote()
,并且网页无意中暴露了您的方法中的错误