SPB Git

Repository Public Research preview

hashing.py 905 B 30 lines python

1"""Stable hashing and identifier helpers."""2from __future__ import annotations34import hashlib5import unicodedata678def sha256_bytes(data: bytes) -> str:9    return hashlib.sha256(data).hexdigest()101112def sha256_text(text: str) -> str:13    return hashlib.sha256(text.encode("utf-8")).hexdigest()141516def md5_bytes(data: bytes) -> str:17    return hashlib.md5(data).hexdigest()  # noqa: S324 - integrity check against provider-supplied MD5 only181920def short_id(prefix: str, *parts: str, n: int = 16) -> str:21    """Deterministic short identifier: ``prefix_<first n hex of sha256(parts)>``."""22    h = hashlib.sha256("\x1f".join(parts).encode("utf-8")).hexdigest()[:n]23    return f"{prefix}_{h}"242526def normalize_for_hash(text: str) -> str:27    """Casefold + NFKC + whitespace collapse; used for exact-duplicate detection."""28    t = unicodedata.normalize("NFKC", text).casefold()29    return " ".join(t.split())30
Files main
Menu

Theme