Skip to content

Instantly share code, notes, and snippets.

@akar-0
Forked from techtonik/sha1.py
Created August 20, 2020 19:52
Show Gist options
  • Save akar-0/048153bec560fba5390c41df1724fada to your computer and use it in GitHub Desktop.
Save akar-0/048153bec560fba5390c41df1724fada 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 <techtonik@gmail.com>'
__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