40 lines
976 B
Python
40 lines
976 B
Python
|
|
# sid_hashers.py
|
||
|
|
|
||
|
|
from abc import ABC, abstractmethod
|
||
|
|
import hashlib
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------
|
||
|
|
# Structural hash strategy interface
|
||
|
|
# ---------------------------------------------------------------------
|
||
|
|
|
||
|
|
class StructureHashStrategy(ABC):
|
||
|
|
"""
|
||
|
|
Abstract interface for derivation / structural hashers.
|
||
|
|
"""
|
||
|
|
|
||
|
|
@abstractmethod
|
||
|
|
def hash_struct(self, payload: bytes) -> str:
|
||
|
|
"""
|
||
|
|
Hash a byte payload deterministically and return hex digest.
|
||
|
|
"""
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------
|
||
|
|
# SHA-256 default implementation
|
||
|
|
# ---------------------------------------------------------------------
|
||
|
|
|
||
|
|
class SHA256SIDHash(StructureHashStrategy):
|
||
|
|
"""
|
||
|
|
Default SHA-256 SID hasher.
|
||
|
|
"""
|
||
|
|
|
||
|
|
name = "sha256.sid.v1"
|
||
|
|
|
||
|
|
def hash_struct(self, payload: bytes) -> str:
|
||
|
|
h = hashlib.sha256()
|
||
|
|
h.update(payload)
|
||
|
|
return h.hexdigest()
|
||
|
|
|