Spaces:
Sleeping
Sleeping
File size: 698 Bytes
2f1a5fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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
|