Calculate sha1 hash of a binary file

LaundroMat Laundro at
Wed Aug 6 21:31:51 CEST 2008

Hi -

I'm trying to calculate unique hash values for binary files,
independent of their location and filename, and I was wondering
whether I'm going in the right direction.

Basically, the hash values are calculated thusly:

f = open('binaryfile.bin')
import hashlib
h = hashlib.sha1()
hash = h.hexdigest()

A quick try-out shows that effectively, after renaming a file, its
hash remains the same as it was before.

I have my doubts however as to the usefulness of this. As
does not seem to read until the end of the file (for a 3.3MB file only
a string of 639 bytes is being returned, perhaps a 00-byte counts as
EOF?), is there a high danger for collusion?

Are there better ways of calculating hash values of binary files?

Thanks in advance,


More information about the Python-list mailing list