-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData Integrity And Authentication
55 lines (46 loc) · 1.9 KB
/
Data Integrity And Authentication
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
from liboqs import Signature
class DataIntegrityAndAuthenticationPQC:
@staticmethod
def generate_keys(algorithm='Dilithium2'):
"""
Generates a quantum-resistant key pair using the specified algorithm.
Parameters:
algorithm: The quantum-resistant algorithm to use for key generation. Defaults to 'Dilithium2'.
Returns:
A tuple containing the public key and private key.
"""
sig = Signature(algorithm)
public_key, private_key = sig.generate_keypair()
return public_key, private_key
@staticmethod
def sign_data(private_key, data, algorithm='Dilithium2'):
"""
Signs data using a quantum-resistant digital signature algorithm.
Parameters:
private_key: Private key for signing.
data: Data to sign.
algorithm: The quantum-resistant algorithm to use. Defaults to 'Dilithium2'.
Returns:
The digital signature.
"""
sig = Signature(algorithm)
signature = sig.sign(data.encode(), private_key)
return signature
@staticmethod
def verify_signature(public_key, data, signature, algorithm='Dilithium2'):
"""
Verifies a signature using a quantum-resistant algorithm.
Parameters:
public_key: Public key for verification.
data: Original data that was signed.
signature: Digital signature to verify.
algorithm: The quantum-resistant algorithm used for signing. Defaults to 'Dilithium2'.
Returns:
True if verification is successful, False otherwise.
"""
sig = Signature(algorithm)
try:
return sig.verify(data.encode(), signature, public_key)
except Exception as e: # Consider catching a more specific exception if possible
print(f"Verification failed: {e}")
return False