tags:

views:

286

answers:

4

Hey guys, am getting MD5 of several files using python function

filehash = hashlib.md5(file)
print "FILE HASH: " + filehash.hexdigest()

though when I go to the terminal and do a

md5 file

the result I'm getting is not the same my python script is outputting (they don't match). Any chance someone knows why? Thanks.

+10  A: 

hashlib.md5() takes the contents of the file not its name.

See http://docs.python.org/library/hashlib.html

You need to open the file, and read its contents before hashing it.

f = open(filename,'rb')
m = hashlib.md5()
while True:
    ## Don't read the entire file at once...
    data = f.read(10240)
    if len(data) == 0:
        break
    m.update(data)
print m.hexdigest()
Douglas Leeder
Thanks for the answer, worked flawlessly !
balgan
Great answer. I'm using this is a project now. Thanks
Liam
+1  A: 

what is file? it should equal to open(filename, 'rb').read(). is it?

SilentGhost
+2  A: 

Try this

filehash = hashlib.md5(open('filename','rb').read())
print "FILE HASH: " + filehash.hexdigest()
MattH
+2  A: 
$ md5 test.py
MD5 (test.py) = 04523172fa400cb2d45652d818103ac3
$ python
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> s = open('test.py','rb').read()
>>> hashlib.md5(s).hexdigest()
'04523172fa400cb2d45652d818103ac3'
telliott99