public
Last active

A quick checker for hashes

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
from os.path import join, splitext, basename
import os
import sys
import hashlib
 
def file_get_contents(path):
with open(path, 'rb') as file:
return file.read()
 
for root, dirs, files in os.walk('.'):
for file in files:
file = join(root, file)
name, ext = splitext(file)
if ext == '.md5':
file_contents = file_get_contents(name)
md5_given = file_get_contents(file)
md5_actual = hashlib.md5(file_contents).hexdigest()
if (md5_given == md5_actual):
print "%s md5 OK" % basename(name)
else:
print "\nERROR: md5 mismatch\nfile : %s\ncalculated: %s\ngiven : %s\n" % (name, md5_actual, md5_given)
 
if ext == '.sha1':
file_contents = file_get_contents(name)
sha1_given = file_get_contents(file)
sha1_actual = hashlib.sha1(file_contents).hexdigest()
if (sha1_given == sha1_actual):
print "%s sha1 OK" % basename(name)
else:
print "\nERROR: sha1 mismatch\nfile : %s\ncalculated: %s\ngiven : %s\n" % (name, sha1_actual, sha1_given)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.