Skip to content

Instantly share code, notes, and snippets.

Last active December 22, 2020 17:45
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
sha256 file checksum function in python
def sha256_file(file_path, chunk_size=65336):
Get the sha 256 checksum of a file.
:param file_path: path to file
:type file_path: unicode or str
:param chunk_size: number of bytes to read in each iteration. Must be > 0.
:type chunk_size: int
:return: sha 256 checksum of file
:rtype : str
# Read the file in small pieces, so as to prevent failures to read particularly large files.
# Also ensures memory usage is kept to a minimum. Testing shows default is a pretty good size.
assert isinstance(chunk_size, int) and chunk_size > 0
import hashlib
import functools
digest = hashlib.sha256()
with open(file_path, 'rb') as f:
[digest.update(chunk) for chunk in iter(functools.partial(, chunk_size), '')]
return digest.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment