Skip to content

Instantly share code, notes, and snippets.

@wadeschulz
Created January 6, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadeschulz/4406656d920e64dc794a to your computer and use it in GitHub Desktop.
Save wadeschulz/4406656d920e64dc794a to your computer and use it in GitHub Desktop.
Python script to generate md5 and sha1 hashes for files of given extension in a directory
import hashlib, os
def hashfile(afile, hasher, blocksize=65536):
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
return hasher.hexdigest()
path = raw_input("Enter directory to scan: ")
extension = raw_input("Enter file extension: ")
print "File\tmd5\tsha1"
os.chdir(path)
for filename in os.listdir(path):
if filename.endswith(extension):
print filename + "\t" + hashfile(open(filename, 'rb'), hashlib.md5()) + "\t" + hashfile(open(filename, 'rb'), hashlib.sha1())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment