Files
quantumtunnel/quantumpipe/kdf.py
T
2026-10-07 03:07:34 +00:00

20 lines
599 B
Python

"""Length-framed SHA-512 KDF (docs/protocol.md)."""
from __future__ import annotations
import hashlib
__all__ = ["kdf"]
def kdf(*data: str | bytes, length: int = 32) -> bytes:
"""kdf(info, *items, length<=64) -> SHA-512 over length-framed items."""
if not 1 <= length <= 64:
raise ValueError("length must be in 1..64")
items = [d.encode() if isinstance(d, str) else bytes(d) for d in data]
h = hashlib.sha512(len(items).to_bytes(8, "little"))
for item in items:
h.update(len(item).to_bytes(8, "little"))
h.update(item)
return h.digest()[:length]