0

When i calculate MD5 for a file in Python i get a strange output. My function :

def md5_for_file(self, fname, block_size=2**20):
    f = open(fname)
    data = f.read()
    m = md5.new()
    if len(data)>0:
        m.update(data)
        f.close()
        return m.digest()

Output :

output http://img51.imageshack.us/img51/6615/20j7.png

I need to convert that to utf8 or what?!

1

1 Answer 1

1

digest is returning the digest as a binary string representing the array of bytes. If you want your digest in hex, e.g. to display it to the user, use the hexdigest method instead.

Also, as pointed out by Cfreak, md5 is deprecated, you should use hashlib instead. Finally, your function is not using the block_size parameter - it will always read the whole file into memory in order to calculate the digest.

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

3 Comments

Thank you! hexdigest works! Yeah...i forgot about the block_size and i'm forced to use python 2.2 so i can't use hashlib and also i couldn't find hashlib for windows and so on... Thank you again! :D
Hmm... Why python MD5 is different than php/visual basic MD5 of the same file?
@Cataclismo Maybe you forgot to open the file in binary mode? Either way, this seems like material for a new question - with more information and reproducible examples, of course...

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.