Python: Hashing files

Wednesday 6. February, 2008


Once again I decided I needed to make some md5sums for files, “Pythonic”-way. I have done it before, but I didn’t quite remember it, so some Googling gave the instructions, I write them down here, in case I’ll need it again

import hashlib
md5sum = hashlib.md5(open('tehfile','rb').read()).hexdigest()
sha1sum = hashlib.sha1(open('tehfile','rb').read()).hexdigest()

(I since sha1sum is longer, I decided to go with it instead of md5)

Basically the code above imports the hashlib-module and then generates md5 and sha1 sums in hexadecimal presentation for ‘tehfile’ opened in for reading in binary mode.