0

I would like to show my students that MD5 collides the two integer "messages" given here. This is my original demonstration for Python 2.7:

from hashlib import md5

m1 = "d131dd02c5e6eec4693d9a0698aff95c2fcab58712467eab4004583eb8fb7f8955ad340609f4b30283e488832571415a085125e8f7cdc99fd91dbdf280373c5bd8823e3156348f5bae6dacd436c919c6dd53e2b487da03fd02396306d248cda0e99f33420f577ee8ce54b67080a80d1ec69821bcb6a8839396f9652b6ff72a70"
m2 = "d131dd02c5e6eec4693d9a0698aff95c2fcab50712467eab4004583eb8fb7f8955ad340609f4b30283e4888325f1415a085125e8f7cdc99fd91dbd7280373c5bd8823e3156348f5bae6dacd436c919c6dd53e23487da03fd02396306d248cda0e99f33420f577ee8ce54b67080280d1ec69821bcb6a8839396f965ab6ff72a70"
# differences                               ^                                                   ^                           ^                                               ^                                                   ^                           ^

print md5(m1.decode("hex")).hexdigest()
print md5(m2.decode("hex")).hexdigest()

As expected, it prints twice 79054025255fb1a26e4bc422aef54eb4. Now when I try to translate the last two lines into Python 3.5 as:

print(md5(int(m1, 16)).hexdigest())
print(md5(int(m2, 16)).hexdigest())

All I get is a TypeError: object supporting the buffer API required message. The method decode no longer works in Python 3, but I am not sure it's a good idea to replace it by int(m, base) as above. May be the problem is there, and not with the hash function API?

1 Answer 1

5

In Python 2, decoding strings as hex gives you a bytestring still, not integers. Don't try to interpret your hex strings as integers.

Use the binascii.unhexlify() function to turn your hex strings into a bytestring:

from binascii import unhexlify

print(md5(unhexlify(m1)).hexdigest())

Note that the same code will work fine in Python 2 as well.

If you want to use decoding anyway, use the codecs.decode() function:

import codecs

print(md5(codecs.decode(m1, 'hex')).hexdigest())

Again, the same code works in Python 2 as well.

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

1 Comment

This works like a charm, thanks! I didn't know this module.

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.