File size: 902 Bytes
f913c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""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()