Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active March 3, 2024 16:06
Show Gist options
  • Save techtonik/df09baeacbebc52d234b to your computer and use it in GitHub Desktop.
Save techtonik/df09baeacbebc52d234b to your computer and use it in GitHub Desktop.
Calculate SHA-1 of a file in Python
#!/usr/bin/env python
"""
usage: python -m sha1 <filename>
"""
import sys
import hashlib
# --- these fields are required for packaging
__version__ = '1.0'
__author__ = 'anatoly techtonik <[email protected]>'
__license__ = 'Public Domain'
__url__ = 'https://gist.github.com/techtonik/df09baeacbebc52d234b'
# /-- these fields are required for packaging
if not sys.argv[1:]:
sys.exit(__doc__.strip())
sha1sum = hashlib.sha1()
with open(sys.argv[1], 'rb') as source:
block = source.read(2**16)
while len(block) != 0:
sha1sum.update(block)
block = source.read(2**16)
print(sha1sum.hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment