-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
keypair.py
63 lines (54 loc) · 1.46 KB
/
keypair.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from .publickey import PublicKey
from .privatekey import PrivateKey
from dissononce.dh.x25519.x25519 import X25519DH
from dissononce.dh.x25519.keypair import KeyPair as X25519KeyPair
class KeyPair(object):
def __init__(self, public, private):
"""
:param public:
:type public: PublicKey
:param private:
:type private: PrivateKey
"""
self._public = public # type: PublicKey
self._private = private # type: PrivateKey
@property
def public(self):
return self._public
@property
def private(self):
return self._private
def __eq__(self, other):
return type(other) is KeyPair and other.public == self.public and other.private == self.private
@classmethod
def generate(cls):
"""
:return:
:rtype: KeyPair
"""
keypair = X25519DH().generate_keypair()
return KeyPair(
PublicKey(
keypair.public.data
),
PrivateKey(
keypair.private.data
)
)
@classmethod
def from_bytes(cls, data):
"""
:param data:
:type data: bytes
:return:
:rtype: KeyPair
"""
keypair = X25519KeyPair.from_bytes(data)
return KeyPair(
PublicKey(
keypair.public.data
),
PrivateKey(
keypair.private.data
)
)