Reputation: 2488
I'm doing a web application login automation. The web app prefix and suffix few octal escaped character with password , make md5 hash of the password at client side and send to server.
So when I Md5 encrypt the string using Java Script, I get below result.
The webapp uses https://ideone.com/2C1b5 JS lib for client side MD5 conversion. hexMD5() belongs to that lib.
But when I am trying to do the same using python I get different result.
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'))
Result
c7862e873e9bc54a93aec58c199cda37
Can any one please explain what I'm doing wrong here ?
Upvotes: 2
Views: 863
Reputation: 180532
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
Not using unicode gives the same output as your Javascript.
If I understand your comment correctly:
len(r"\051") == 4 # use raw string r
len("\051") == 1
Upvotes: 2
Reputation: 21
this question is old but I came across with this problem a few day ago and above answer couldn't solve it, so I solved it in this way.
1- first convert all character to octal equal format
HELLO
=> \110\105\114\114\117
by this functions stringToOctal
def dectoOct(decimal):
octal = 0
ctr = 0
temp = decimal
while (temp > 0):
octal += ((temp % 8) * (10 ** ctr))
temp = int(temp / 8)
ctr += 1
return octal
def stringToOctal(string):
result = ""
for character in list(string):
result += "\\"+ str(dectoOct(ord(character)))
return result
then result shuold be something like
\051\110\105\114\114\117\341\303\026\153\155\271\161\166\030\054\324\011\046\035\344\274
2- convert octal numbers to bytes
final code is some thing like:
import hashlib
def dectoOct(decimal):
octal = 0
ctr = 0
temp = decimal
while (temp > 0):
octal += ((temp % 8) * (10 ** ctr))
temp = int(temp / 8)
ctr += 1
return octal
def stringToOctal(string):
result = ""
for character in list(string):
result += "\\"+ str(dectoOct(ord(character)))
return result
prefix = r"\051"
suffix =r"\341\303\026\153\155\271\161\166\030\054\324\011\046\035\344\274"
word = "HELLO"
octalCharacters = prefix + stringToOctal(word) + suffix
hashed = hashlib.md5(bytes([int(i, 8) for i in octalCharacters.split("\\")[1:]]))
result = hashed.hexdigest()
Result:
37a0c199850b36090b439c3ac152fd70
Upvotes: 0