forked from WolfByttner/cryptocurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.py
30 lines (25 loc) · 953 Bytes
/
key.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
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidSignature
class Key:
def __init__(self):
self.private_key = rsa.generate_private_key(
public_exponent=65537, key_size=4098
)
self.public_key = self.private_key.public_key()
def get_public_key(self):
return self.public_key
def sign(self, message):
# Use SHA-256 instead of SHA-1
return self.private_key.sign(
message.encode("utf-8"), padding.PKCS1v15(), hashes.SHA256()
)
def verify(self, message, signature, public_key):
try:
# Use SHA-256 instead of SHA-1
public_key.verify(
signature, message.encode("utf-8"), padding.PKCS1v15(), hashes.SHA256()
)
return True
except InvalidSignature:
return False