nem.ec

Programming Information Security Recipes Personal Code Snippets

Hash a large file in Python

Hashes a large file in chunks, supports multiple hash algorithms

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))
Copied!

Use your own input

If you have values you'd like to use instead of the provided variables, type or paste your data into the variable inputs below. If Javascript is enabled, your text will automatically be added to the command and you can click to copy.