Python 2 vs 3。相同的输入,不同的结果。 MD5哈希
Python 2 vs 3. Same inputs, different results. MD5 hash
Python3个代码:
def md5hex(data):
""" return hex string of md5 of the given string """
h = MD5.new()
h.update(data.encode('utf-8'))
return b2a_hex(h.digest()).decode('utf-8')
Python2个代码:
def md5hex(data):
""" return hex string of md5 of the given string """
h = MD5.new()
h.update(data)
return b2a_hex(h.digest())
输入python3:
>>> md5hex('bf5¤7¤8¤3')
'61d91bafe643c282bd7d7af7083c14d6'
输入python2:
>>> md5hex('bf5¤7¤8¤3')
'46440745dd89d0211de4a72c7cea3720'
怎么回事?
编辑:
def genurlkey(songid, md5origin, mediaver=4, fmt=1):
""" Calculate the deezer download url given the songid, origin and media+format """
data = b'\xa4'.join(_.encode("utf-8") for _ in [md5origin, str(fmt), str(songid), str(mediaver)])
data = b'\xa4'.join([md5hex(data), data])+b'\xa4'
if len(data)%16:
data += b'\x00' * (16-len(data)%16)
return hexaescrypt(data, "jo6aey6haid2Teih").decode('utf-8')
所有这些问题都是从另一个函数中 python 2 代码中的 b'\xa4' 开始的。此字节在 python 3.
中不起作用
然后我得到了正确的 MD5 散列...
python3 中的默认编码是 Unicode。在 python 2 中,它是 ASCII。因此,即使字符串在读取时匹配,它们也会以不同的方式呈现。
改为使用 hashlib 和语言不可知的实现:
import hashlib
text = u'bf5¤7¤8¤3'
text = text.encode('utf-8')
print(hashlib.md5(text).hexdigest())
在 Python 2/3 中工作,结果相同:
Python2:
'61d91bafe643c282bd7d7af7083c14d6'
Python3(通过 repl.it):
'61d91bafe643c282bd7d7af7083c14d6'
您的代码失败的原因是编码字符串与未编码字符串不是相同的字符串:您只编码Python 3.
如果你需要它来匹配未编码的 Python 2:
import hashlib
text = u'bf5¤7¤8¤3'
print(hashlib.md5(text.encode("latin1")).hexdigest())
作品:
46440745dd89d0211de4a72c7cea3720
Python 2 的默认编码是 latin1
而不是 utf-8
Python3个代码:
def md5hex(data):
""" return hex string of md5 of the given string """
h = MD5.new()
h.update(data.encode('utf-8'))
return b2a_hex(h.digest()).decode('utf-8')
Python2个代码:
def md5hex(data):
""" return hex string of md5 of the given string """
h = MD5.new()
h.update(data)
return b2a_hex(h.digest())
输入python3:
>>> md5hex('bf5¤7¤8¤3')
'61d91bafe643c282bd7d7af7083c14d6'
输入python2:
>>> md5hex('bf5¤7¤8¤3')
'46440745dd89d0211de4a72c7cea3720'
怎么回事?
编辑:
def genurlkey(songid, md5origin, mediaver=4, fmt=1):
""" Calculate the deezer download url given the songid, origin and media+format """
data = b'\xa4'.join(_.encode("utf-8") for _ in [md5origin, str(fmt), str(songid), str(mediaver)])
data = b'\xa4'.join([md5hex(data), data])+b'\xa4'
if len(data)%16:
data += b'\x00' * (16-len(data)%16)
return hexaescrypt(data, "jo6aey6haid2Teih").decode('utf-8')
所有这些问题都是从另一个函数中 python 2 代码中的 b'\xa4' 开始的。此字节在 python 3.
中不起作用然后我得到了正确的 MD5 散列...
python3 中的默认编码是 Unicode。在 python 2 中,它是 ASCII。因此,即使字符串在读取时匹配,它们也会以不同的方式呈现。
改为使用 hashlib 和语言不可知的实现:
import hashlib
text = u'bf5¤7¤8¤3'
text = text.encode('utf-8')
print(hashlib.md5(text).hexdigest())
在 Python 2/3 中工作,结果相同:
Python2:
'61d91bafe643c282bd7d7af7083c14d6'
Python3(通过 repl.it):
'61d91bafe643c282bd7d7af7083c14d6'
您的代码失败的原因是编码字符串与未编码字符串不是相同的字符串:您只编码Python 3.
如果你需要它来匹配未编码的 Python 2:
import hashlib
text = u'bf5¤7¤8¤3'
print(hashlib.md5(text.encode("latin1")).hexdigest())
作品:
46440745dd89d0211de4a72c7cea3720
Python 2 的默认编码是 latin1
而不是 utf-8