Created
October 17, 2017 17:17
-
-
Save cgthayer/decdef8b704e2f23c8cbdd94cb1fbf9d to your computer and use it in GitHub Desktop.
Checksum files (sha1)
This file contains 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 sys | |
bsz = 8192 | |
for f in sys.argv[1:]: | |
sha1sum = hashlib.sha1() | |
with open(f, 'rb') as source: | |
while True: | |
block = source.read(bsz) | |
if len(block) == 0: | |
break | |
sha1sum.update(block) | |
print(f, sha1sum.hexdigest()) | |
for f in sys.argv[1:]: | |
sha1sum = hashlib.sha1() | |
with open(f, 'rb') as fd: | |
sha1sum.update(fd.read()) | |
print(f, sha1sum.hexdigest()) | |
for f in sys.argv[1:]: | |
with open(f, 'rb') as fd: | |
print(f, hashlib.sha1(fd.read()).hexdigest()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first one helps limit the block size per read to 8K, so for big files you don't read everything into memory. The last one is just the most compact form.