-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP7.py
More file actions
43 lines (37 loc) · 1.05 KB
/
P7.py
File metadata and controls
43 lines (37 loc) · 1.05 KB
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
#Demonstrate the usage/sending of a digitally signed document.
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
# Generate private key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
# Get the public key
public_key = private_key.public_key()
# Message to sign
document = b"Confidential document needs to be signed"
# Sign the message
signature = private_key.sign(
document,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
# Verify the signature
try:
print("Document:", document)
print("Signature:", signature.hex()) # print in hex for readability
public_key.verify(
signature,
document,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
print("✅ Signature is valid.")
except Exception as e:
print("❌ Signature is invalid:", e)