|
1 | 1 | # Signature Python example |
2 | 2 |
|
| 3 | +import logging |
| 4 | +from pprint import pformat |
| 5 | +from sys import stdout |
| 6 | + |
3 | 7 | import oqs |
4 | | -from pprint import pprint |
5 | 8 |
|
6 | | -print("liboqs version:", oqs.oqs_version()) |
7 | | -print("liboqs-python version:", oqs.oqs_python_version()) |
8 | | -print("Enabled signature mechanisms:") |
9 | | -sigs = oqs.get_enabled_sig_mechanisms() |
10 | | -pprint(sigs, compact=True) |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | +logger.setLevel(logging.INFO) |
| 11 | +logger.addHandler(logging.StreamHandler(stdout)) |
| 12 | + |
| 13 | +logger.info("liboqs version: %s", oqs.oqs_version()) |
| 14 | +logger.info("liboqs-python version: %s", oqs.oqs_python_version()) |
| 15 | +logger.info( |
| 16 | + "Enabled signature mechanisms:\n%s", |
| 17 | + pformat(oqs.get_enabled_sig_mechanisms(), compact=True), |
| 18 | +) |
11 | 19 |
|
12 | | -message = "This is the message to sign".encode() |
| 20 | +message = b"This is the message to sign" |
13 | 21 |
|
14 | 22 | # Create signer and verifier with sample signature mechanisms |
15 | 23 | sigalg = "ML-DSA-44" |
16 | | -with oqs.Signature(sigalg) as signer: |
17 | | - with oqs.Signature(sigalg) as verifier: |
18 | | - print("\nSignature details:") |
19 | | - pprint(signer.details) |
| 24 | +with oqs.Signature(sigalg) as signer, oqs.Signature(sigalg) as verifier: |
| 25 | + logger.info("Signature details:\n%s", pformat(signer.details)) |
20 | 26 |
|
21 | | - # Signer generates its keypair |
22 | | - signer_public_key = signer.generate_keypair() |
23 | | - # Optionally, the secret key can be obtained by calling export_secret_key() |
24 | | - # and the signer can later be re-instantiated with the key pair: |
25 | | - # secret_key = signer.export_secret_key() |
| 27 | + # Signer generates its keypair |
| 28 | + signer_public_key = signer.generate_keypair() |
| 29 | + # Optionally, the secret key can be obtained by calling export_secret_key() |
| 30 | + # and the signer can later be re-instantiated with the key pair: |
| 31 | + # secret_key = signer.export_secret_key() |
26 | 32 |
|
27 | | - # Store key pair, wait... (session resumption): |
28 | | - # signer = oqs.Signature(sigalg, secret_key) |
| 33 | + # Store key pair, wait... (session resumption): |
| 34 | + # signer = oqs.Signature(sigalg, secret_key) |
29 | 35 |
|
30 | | - # Signer signs the message |
31 | | - signature = signer.sign(message) |
| 36 | + # Signer signs the message |
| 37 | + signature = signer.sign(message) |
32 | 38 |
|
33 | | - # Verifier verifies the signature |
34 | | - is_valid = verifier.verify(message, signature, signer_public_key) |
| 39 | + # Verifier verifies the signature |
| 40 | + is_valid = verifier.verify(message, signature, signer_public_key) |
35 | 41 |
|
36 | | - print("\nValid signature?", is_valid) |
| 42 | + logger.info("Valid signature? %s", is_valid) |
0 commit comments