BirdNET-Uploader-App / src /uploader /hash_utils.py
jrrribeiro's picture
Import uploader files from validator workspace
4e31d1f
Raw History Blame Contribute Delete
698 Bytes
from __future__ import annotations
import hashlib
from pathlib import Path
def compute_file_hash(path: str | Path, *, chunk_size: int = 1024 * 1024) -> str:
"""Compute a streaming SHA-256 hash for a file."""
digest = hashlib.sha256()
file_path = Path(path)
with file_path.open("rb") as handle:
for chunk in iter(lambda: handle.read(chunk_size), b""):
digest.update(chunk)
return digest.hexdigest()
def verify_file_integrity(path: str | Path, expected_hash: str, *, chunk_size: int = 1024 * 1024) -> bool:
"""Return True when a file's SHA-256 matches the expected hash."""
return compute_file_hash(path, chunk_size=chunk_size) == expected_hash