1

I am having some trouble converting this Python 2 code:

import md5

steamid=76561197960435530
temp = ""
for i in range(8):
    temp +=  chr((steamid & 0xFF))
    steamid >>= 8
m = md5.new("BE"+temp)

print m.hexdigest()

into Python 3, the result from the code above is 'a071c58205ec529c5c19fa7dadcaae93'. However, when I try to reproduce the same code in Python 3 using the hashlib library, I get a completely different return. I want it to return the exact same thing as the Python 2 code. Here is my attempt to reproduce the code in Python 3:

from hashlib import md5

steamid = 76561197960435530
temp = ""
for i in range(8):
    temp +=  chr((steamid & 0xFF))
    steamid >>= 8
m = md5()
m.update(("BE"+temp).encode())

print(m.hexdigest())

which returns 'a96d4d4b56a3b5c1a747e01dd7045c6d' which is not what I want it to return.

1 Answer 1

4

In Python 3 you are building a string with the correct code points, and then using encode() to turn it into bytes. This is where the problem lies: encode() will UTF8-encode the code points in the string, which will result in a byte sequence that differs from the intended one. To avoid this problem, you should use bytes from the ground up:

from hashlib import md5

steamid = 76561197960435530
temp = bytearray()
for i in range(8):
    temp.append(steamid & 0xFF)
    steamid >>= 8
m = md5()
m.update((b"BE" + temp))

print(m.hexdigest())

As an added bonus, this version of the code also runs unchanged on Python 2.7, and produces the correct result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.