Lazo Lab Sign up

Python Projects & Skills / Security tools in Python

Lesson 24 of 28

Hashing files

hashlib makes SHA-256 fingerprints. Compare a download's hash with the one the developer published to check it wasn't changed.

Key points

  • hashlib.sha256()
  • Read big files in chunks
  • Any change gives a different hash
import hashlib
h = hashlib.sha256()
with open("file.iso", "rb") as f:
    for chunk in iter(lambda: f.read(65536), b""):
        h.update(chunk)
print(h.hexdigest())
Watch a video on thisOpens YouTube search results for “Hashing files” in a new tab

Quiz · +10 XP

Why compare a file's hash with the published one?

Log in to save progress and earn XP.