Skip to content

Instantly share code, notes, and snippets.

@odoku
Created July 17, 2020 05:29
Show Gist options
  • Save odoku/0edf457159a028f31b2fc9290cb485fd to your computer and use it in GitHub Desktop.
Save odoku/0edf457159a028f31b2fc9290cb485fd to your computer and use it in GitHub Desktop.
import hashlib
def compute_file_checksum(
filepath: str,
algorithm: str = "md5",
chunk_size: int = 1024,
) -> str:
hasher = hashlib.new(algorithm)
with open(filepath, "rb") as fp:
while True:
buffer = fp.read(chunk_size * hasher.block_size)
if not buffer:
break
hasher.update(buffer)
return hasher.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment