-
Notifications
You must be signed in to change notification settings - Fork 0
/
prac-5-SHA5.py
33 lines (25 loc) · 1.07 KB
/
prac-5-SHA5.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
#implementing SHA Algorithm5
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random
def generate_signature(private_key, message):
key = RSA.importKey(private_key)
hashed_message = SHA256.new(message.encode('utf-8'))
signer = PKCS1_v1_5.new(key)
signature = signer.sign(hashed_message)
return signature
def verify_signature(public_key, message, signature):
key = RSA.importKey(public_key)
hashed_message = SHA256.new(message.encode('utf-8'))
verifier = PKCS1_v1_5.new(key)
return verifier.verify(hashed_message, signature)
random_generator = Random.new().read
key_pair = RSA.generate(2048, random_generator)
public_key = key_pair.publickey().export_key()
private_key = key_pair.export_key()
message = "Hello, World!"
signature = generate_signature(private_key, message)
print("Generated Signature:", signature.hex())
is_valid = verify_signature(public_key, message, signature)
print("Signature Verification Result:", is_valid)