hashlib exposes cryptographic hash algorithms such as SHA-256. A digest acts as a content fingerprint: changing any byte is expected to change the output, making it useful for download checks and corruption detection.

from hashlib import sha256
from pathlib import Path

def sha256_arquivo(caminho: Path) -> str:
    digest = sha256()
    with caminho.open("rb") as arquivo:
        for bloco in iter(lambda: arquivo.read(1024 * 1024), b""):
            digest.update(bloco)
    return digest.hexdigest()

print(sha256_arquivo(Path("pacote.zip")))

Chunked reads keep memory usage stable for large files. Compare the computed digest with a value obtained through a trusted channel. If an attacker can replace both the file and the adjacent published hash, the check provides no authenticity.

Practical guidance

Choose a modern algorithm accepted by your protocol and avoid MD5 or SHA-1 for new security decisions. Hashing does not encrypt data and cannot recover the original content. Use a password derivation function for passwords and HMAC with constant-time comparison for shared-secret message authentication.

Read about Python secrets for token generation and Python API security for broader controls.

The official hashlib documentation, accessed July 22, 2026, documents the API and its constraints.