from typing import BinaryIO
import hashlib
# Can replace algorithm with hashlib.sha1, hashlib.sha256, etc.
def hash_file(f: BinaryIO, blocksize: int=8192, algorithm=hashlib.md5):
file_hash = algorithm()
for chunk in iter(lambda: f.read(blocksize), b""):
file_hash.update(chunk)
return file_hash.hexdigest()
with open(filename, 'rb') as f:
print(hash_file(f))