Spaces:
Running
Running
| """Hashing and unique ID generation.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import secrets | |
| import string | |
| from config import settings | |
| def hash_tmdb_id(tmdb_id: int) -> str: | |
| """Generate a deterministic 10-char alphanumeric hash from a TMDB ID.""" | |
| raw = f"{tmdb_id}:{settings.hash_secret_key}" | |
| return hashlib.sha256(raw.encode()).hexdigest()[:10] | |
| def generate_unique_id(length: int = 10) -> str: | |
| """Generate a cryptographically random alphanumeric string.""" | |
| alphabet = string.ascii_lowercase + string.digits | |
| return "".join(secrets.choice(alphabet) for _ in range(length)) | |
| def file_checksum(path: str, algorithm: str = "sha256") -> str: | |
| """Return hex digest of a file's checksum.""" | |
| h = hashlib.new(algorithm) | |
| with open(path, "rb") as f: | |
| for chunk in iter(lambda: f.read(65536), b""): | |
| h.update(chunk) | |
| return h.hexdigest() | |