Grant McKinnon Grant McKinnon - 1 year ago 41
Python Question

reading hash from md5 in python

I can't seem to find anything on the interwebs on this.

Basically I have a bunch of xml files with a respective md5 file for each

I want to be able to compare each xml with its md5 hash to ensure they have been sent to us correctly.

I have tried running this as a binary file but I don't think it works.

def read_hash_from_md5_file(md5_filename):

with open(md5_filename,"rb") as file:
for line in file:

this returns
b' \xc0\xff\xae\x91\x1c\tJ\xc5~\xbe\x8d\x93\xad\xf3\x0c'

where when I calculate the hash for the xml I get '20c0ffae911c094ac57ebe8d93adf30c'

thanks for the help.


Use binascii.hexlify to get hexa-decimal representation:

>>> import binascii
>>> binascii.hexlify(b' \xc0\xff\xae\x91\x1c\tJ\xc5~\xbe\x8d\x93\xad\xf3\x0c')

and decode it if you want to get string instead of bytes object:

>>> binascii.hexlify(b' \xc0\xff\xae\x91\x1c\tJ\xc5~\xbe\x8d\x93\xad\xf3\x0c').decode()