-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
43 lines (31 loc) · 1.03 KB
/
crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# type: ignore
import ecdsa
class PrivateKey:
def __init__(self, key: ecdsa.SigningKey):
self._key = key
@classmethod
def generate(cls):
return cls(ecdsa.SigningKey.generate())
@classmethod
def from_bytes(cls, data: bytes):
return cls(ecdsa.SigningKey.from_string(data))
def to_bytes(self):
return self._key.to_string()
def to_public(self):
return PublicKey(self._key.get_verifying_key())
def sign(self, data: bytes) -> bytes:
return self._key.sign_deterministic(data)
class PublicKey:
def __init__(self, key: ecdsa.VerifyingKey):
self._key = key
@classmethod
def from_bytes(cls, data: bytes):
return cls(ecdsa.VerifyingKey.from_string(data))
def to_bytes(self) -> bytes:
return self._key.to_string(encoding="compressed")
def verify(self, data: bytes, sig: bytes) -> bool:
try:
self._key.verify(sig, data)
return True
except ecdsa.BadSignatureError:
return False