Question

I am using python to do make a download manager to verify md5 checksums automatically... The thing is python gives a wrong md5, I cross-checked with a third party md5 verifier software.

I am using hashlib to check md5.. here's my piece of code for md5 checksum For some files, it gets md5 right but for others its just completely wrong...

x= sys.path[0]
x= x + '\\' + file_name
print 'file successfully saved to path', x
file_ref=open(x,'rb').read()
hashlib.md5(file_ref).hexdigest()
print 'MD5 of file is:',hashlib.md5(file_ref).hexdigest()

md5 for original file on website: e557fa76ed485fd10e8476377ad5be95

md5 given by python: cb3b2227733d3344dba15e5e39d04f43

md5 given by md5 verifier: e557fa76ed485fd10e8476377ad5be95

please help :/

Was it helpful?

Solution

Reading that for some file it's right, but for others it's wrong, you can check your path. This is what I use for md5:

def hashsum(path, hex=True, hash_type=hashlib.md5):
    hashinst = hash_type()
    with open(path, 'rb') as f:
        for chunk in iter(lambda: f.read(hashinst.block_size * 128), b''):
            hashinst.update(chunk)
    return hashinst.hexdigest() if hex else hashinst.digest()

You can use this to compare:

myhash = hashsum(cfile)
sproc = subprocess.Popen(['md5', cfile], stdout=subprocess.PIPE)
syshash = sproc.communicate()[0].split()[0]
print myhash
print syshash
print 'Hash idetntical' if myhash == syshash else 'Hash check fail'

where cfile is the path to the file. I guess your path is wrong. I'm guessing windows so sys.path[0] is not the proper way to get the current directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top