-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.py
94 lines (79 loc) · 3.04 KB
/
sign.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
83
84
85
86
87
88
89
90
91
92
93
94
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.backends import default_backend
import struct
import os
ped_message = ''
def sign():
# Load the private key of the signer
with open("administrator_private_key.pem", "rb") as f:
pem_data = f.read()
private_key = serialization.load_pem_private_key(
pem_data,
password=None
)
message = b"Hello, this is a test message."
# Compute the BLAKE2b hash of the message and random bits
hasher = hashes.Hash(hashes.BLAKE2b(64), backend=default_backend())
hasher.update(message)
# hasher.update(ass := os.urandom(32))
digest = hasher.finalize()
# print("public_key1: ", ass, "\n", "message: ", message)
# Create a message to sign
signature = private_key.sign(
digest,
padding.PKCS1v15(),
hashes.SHA256()
)
# Pack the message
payload_length = len(message).to_bytes(4, byteorder='big')
payload = message
random_bits = os.urandom(32)
signature_length = len(signature).to_bytes(4, byteorder='big')
packed_message = payload_length + payload + random_bits + signature_length + signature
return packed_message
def verify(packed_message):
# Extract the fields from the packed message
payload_length = int.from_bytes(packed_message[:4], byteorder='big')
payload = packed_message[4:4+payload_length]
random_bits = packed_message[4+payload_length:4+payload_length+32]
signature_length = int.from_bytes(packed_message[4+payload_length+32:4+payload_length+32+4], byteorder='big')
signature = packed_message[4+payload_length+32+4:]
# Compute the BLAKE2b hash of the payload and random bits
hasher = hashes.Hash(hashes.BLAKE2b(64), backend=default_backend())
hasher.update(payload)
# hasher.update(random_bits)
digest = hasher.finalize()
# print("public_key1: ", random_bits, "\n", "message: ", payload)
# Verify the signature using the public key of the signer
with open("administrator_public.pem", "rb") as f:
pem_data = f.read()
public_key = serialization.load_pem_public_key(
pem_data,
backend=default_backend()
)
# try:
public_key.verify(
signature,
digest,
padding.PKCS1v15(),
hashes.SHA256()
)
# Reconstruct the original message by concatenating the payload and random bits
# message = payload + random_bits
message = payload
# Verify that the computed hash matches the hash in the packed message
hasher = hashes.Hash(hashes.BLAKE2b(64), backend=default_backend())
hasher.update(message)
computed_digest = hasher.finalize()
if computed_digest != digest:
return False
else:
return True
# except:
# return False
# packed_message = sign()
# print(verify(packed_message))
# receive(ped_message)
for i in range(1, 6):
print(i)