Skip to content

Instantly share code, notes, and snippets.

@cgthayer
Created October 17, 2017 17:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cgthayer/decdef8b704e2f23c8cbdd94cb1fbf9d to your computer and use it in GitHub Desktop.
Checksum files (sha1)
#!/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())
@cgthayer
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment