Python Octal Escape String

时间:2015-02-26 16:38:02

标签: python

我正在进行Web应用程序登录自动化。 web应用前缀和后缀很少八进制转义字符和密码,在客户端进行密码的md5哈希并发送到服务器。

因此,当我使用Java Script对Md5加密字符串时,我得到了以下结果。

webapp使用https://ideone.com/2C1b5 JS lib进行客户端MD5转换。 hexMD5()属于该lib。

enter image description here

但是当我尝试使用python做同样的事情时,我会得到不同的结果。

import hashlib
def getMd5(string):
    m = hashlib.md5()
    m.update(string)
    return m.hexdigest()
prefix = "\051"
suffix  = "\341\303\026\153\155\271\161\166\030\054\324\011\046\035\344\274"

prefix = unicode(prefix,'unicode-escape')
suffix = unicode(suffix,'unicode-escape')
salted = prefix+"HELLO"+suffix
print getMd5(salted.encode('utf8'))

结果

c7862e873e9bc54a93aec58c199cda37

任何人都能解释一下我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

import hashlib
def getMd5(string):
    m = hashlib.md5()
    m.update(string)
    return m.hexdigest()
prefix = "\051"
suffix  ="\341\303\026\153\155\271\161\166\030\054\324\011\046\035\344\274"


salted = prefix+"HELLO"+suffix
print getMd5(salted)

37a0c199850b36090b439c3ac152fd70

不使用unicode会提供与Javascript相同的输出。

如果我理解你的评论:

len(r"\051" == 4 # use raw string r
len("\051") == 1 
相关问题