30 lines
962 B
Python
30 lines
962 B
Python
from Crypto.PublicKey import ECC
|
|
from Crypto.Hash import SHA256
|
|
from Crypto.Signature import DSS
|
|
import base58
|
|
|
|
|
|
class Signer:
|
|
def __init__(self, seed: bytes):
|
|
if len(seed) != 32:
|
|
raise ValueError("Seed must be 32 bytes")
|
|
|
|
self.key = ECC.generate(curve='P-256', randfunc=lambda n: seed)
|
|
self.verifier = DSS.new(self.key, 'fips-186-3')
|
|
|
|
def sign(self, data_bytes: bytes) -> str:
|
|
hash_obj = SHA256.new(data_bytes)
|
|
signature = self.verifier.sign(hash_obj)
|
|
signature_str = base58.b58encode(signature).decode()
|
|
return signature_str
|
|
|
|
def verify(self, data_bytes: str, signature: str) -> bool:
|
|
data_bytes = base58.b58decode(data_bytes)
|
|
signature_bytes = base58.b58decode(signature)
|
|
hash_obj = SHA256.new(data_bytes)
|
|
try:
|
|
self.verifier.verify(hash_obj, signature_bytes)
|
|
return True
|
|
except ValueError:
|
|
return False
|