Last active
August 29, 2015 13:58
-
-
Save bdunnette/9955517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import hashlib | |
import os | |
import sys | |
import csv | |
# Set the directory you want to start from | |
rootDir = sys.argv[1] | |
BLOCKSIZE = 65536 | |
csv_filename = "checksums.csv" | |
csvfile = open(os.path.join(sys.argv[1], csv_filename), 'w') | |
csv_fields = ['filename', 'md5', 'sha1'] | |
csvwriter = csv.DictWriter(csvfile, delimiter='|', fieldnames=csv_fields) | |
csvwriter.writerow(dict((fn,fn) for fn in csv_fields)) | |
for dirName, subdirList, fileList in os.walk(rootDir): | |
print('Found directory: %s' % dirName) | |
fileList.sort() | |
for fname in fileList: | |
print('\t%s' % fname) | |
if fname != csv_filename: | |
hasher1 = hashlib.md5() | |
hasher2 = hashlib.sha1() | |
with open(os.path.join(dirName, fname), 'rb') as afile: | |
buf = afile.read(BLOCKSIZE) | |
while len(buf) > 0: | |
hasher1.update(buf) | |
hasher2.update(buf) | |
buf = afile.read(BLOCKSIZE) | |
md5 = hasher1.hexdigest() | |
sha1 = hasher2.hexdigest() | |
file_row = {'filename': fname, 'md5': md5, 'sha1': sha1} | |
csvwriter.writerow(file_row) | |
csvfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment