Skip to content

Instantly share code, notes, and snippets.

@cmutel
Created September 11, 2017 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmutel/4e62473afb36d6cdd647e5ccb20eea46 to your computer and use it in GitHub Desktop.
Save cmutel/4e62473afb36d6cdd647e5ccb20eea46 to your computer and use it in GitHub Desktop.
Python file hashing
import hashlib
def sha256(filepath, blocksize=65536):
"""Generate SHA 256 hash for file at ``filepath``.
``blocksize`` (default is 65536) is block size to feed to hasher.
Returns a ``str``."""
hasher = hashlib.sha256()
fo = open(filepath, 'rb')
buf = fo.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = fo.read(blocksize)
return hasher.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment