cosmicledger-verifiable-off.../cosmic_ledger/crypto.py

20 lines
611 B
Python

import hmac
import hashlib
class Signer:
"""Simple HMAC-based signer for MVP. In a real system, replace with public-key cryptography."""
def __init__(self, key: bytes):
if isinstance(key, str):
key = key.encode('utf-8')
self._key = key
def sign(self, data: bytes) -> bytes:
return hmac.new(self._key, data, hashlib.sha256).digest()
def verify(self, data: bytes, signature: bytes) -> bool:
expected = self.sign(data)
return hmac.compare_digest(expected, signature)
def digest(data: bytes) -> bytes:
return hashlib.sha256(data).digest()