-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
82 lines (66 loc) · 2.29 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pickle
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization
class Wallet:
def __init__(self):
self.public_key_pem = None
self.private_key_pem = None
def generate_key_pair(self):
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
public_key = private_key.public_key()
self.public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
self.private_key_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
def save_key_pair_to_file(self):
with open('private.pem', 'wb') as file:
pickle.dump(self.private_key_pem, file)
with open('public.pem', 'wb') as file:
pickle.dump(self.public_key_pem, file)
def load_key_pair_from_file(self):
with open('private.pem', 'rb') as file:
self.private_key_pem = pickle.load(file)
with open('public.pem', 'rb') as file:
self.public_key_pem = pickle.load(file)
def sign_message(private_key_pem, message):
private_key = serialization.load_pem_private_key(
private_key_pem,
password=None,
backend=default_backend()
)
return private_key.sign(
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
def verify_signature(public_key_pem, signature, message) -> bool:
public_key = serialization.load_pem_public_key(
public_key_pem,
backend=default_backend()
)
try:
public_key.verify(
signature,
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
return True
except Exception:
return False