|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +example_keys.py |
| 4 | +
|
| 5 | +Demonstrates how to generate Ed25519/ECDSA private keys, |
| 6 | +convert them to/from raw and DER encodings, and sign/verify messages |
| 7 | +using the new explicit loaders in the hiero_sdk_python.crypto module. |
| 8 | +""" |
| 9 | + |
| 10 | +from hiero_sdk_python.crypto.private_key import PrivateKey |
| 11 | +from hiero_sdk_python.crypto.public_key import PublicKey |
| 12 | + |
| 13 | + |
| 14 | +def example_ed25519_raw(): |
| 15 | + print("=== Ed25519 (Raw) ===") |
| 16 | + # Generate Ed25519 private key |
| 17 | + priv_ed = PrivateKey.generate_ed25519() |
| 18 | + print("Ed25519 Private Key (raw hex):", priv_ed.to_string_raw()) |
| 19 | + |
| 20 | + # Derive the public key |
| 21 | + pub_ed = priv_ed.public_key() |
| 22 | + print("Ed25519 Public Key (raw hex):", pub_ed.to_string_raw()) |
| 23 | + |
| 24 | + # Reload the private key from its raw hex |
| 25 | + priv_ed_loaded = PrivateKey.from_string_ed25519(priv_ed.to_string_raw()) |
| 26 | + # Reload the public key from its raw hex |
| 27 | + pub_ed_loaded = PublicKey.from_string_ed25519(pub_ed.to_string_raw()) |
| 28 | + |
| 29 | + # Sign and verify |
| 30 | + message = b"Hello, Ed25519!" |
| 31 | + signature = priv_ed.sign(message) |
| 32 | + try: |
| 33 | + pub_ed.verify(signature, message) |
| 34 | + print("Ed25519 signature verified successfully.") |
| 35 | + except Exception as e: |
| 36 | + print("Ed25519 signature verification failed:", e) |
| 37 | + |
| 38 | + # Sanity checks |
| 39 | + print("Private keys match?" , priv_ed_loaded.to_string_raw() == priv_ed.to_string_raw()) |
| 40 | + print("Public keys match?" , pub_ed_loaded.to_string_raw() == pub_ed.to_string_raw()) |
| 41 | + print() |
| 42 | + |
| 43 | + |
| 44 | +def example_ed25519_der(): |
| 45 | + print("=== Ed25519 (DER) ===") |
| 46 | + # Generate Ed25519 private key |
| 47 | + priv_ed = PrivateKey.generate_ed25519() |
| 48 | + print("Ed25519 Private Key (DER hex):", priv_ed.to_string_der()) |
| 49 | + |
| 50 | + # Derive the public key and show DER |
| 51 | + pub_ed = priv_ed.public_key() |
| 52 | + print("Ed25519 Public Key (DER hex):", pub_ed.to_string_der()) |
| 53 | + |
| 54 | + # Reload the private key from DER hex |
| 55 | + priv_ed_loaded = PrivateKey.from_string_der(priv_ed.to_string_der()) |
| 56 | + # Reload the public key from DER hex |
| 57 | + pub_ed_loaded = PublicKey.from_string_der(pub_ed.to_string_der()) |
| 58 | + |
| 59 | + # Sign and verify |
| 60 | + message = b"Hello, Ed25519 in DER!" |
| 61 | + signature = priv_ed.sign(message) |
| 62 | + try: |
| 63 | + pub_ed.verify(signature, message) |
| 64 | + print("DER-based Ed25519 signature verified successfully.") |
| 65 | + except Exception as e: |
| 66 | + print("DER-based Ed25519 signature verification failed:", e) |
| 67 | + |
| 68 | + # Sanity checks |
| 69 | + print("Private keys match?" , priv_ed_loaded.to_string_der() == priv_ed.to_string_der()) |
| 70 | + print("Public keys match?" , pub_ed_loaded.to_string_der() == pub_ed.to_string_der()) |
| 71 | + print() |
| 72 | + |
| 73 | + |
| 74 | +def example_ecdsa_raw(): |
| 75 | + print("=== ECDSA (Raw) ===") |
| 76 | + # Generate ECDSA private key |
| 77 | + priv_ecdsa = PrivateKey.generate_ecdsa() |
| 78 | + print("ECDSA Private Key (raw hex):", priv_ecdsa.to_string_raw()) |
| 79 | + |
| 80 | + # Derive the public key |
| 81 | + pub_ecdsa = priv_ecdsa.public_key() |
| 82 | + print("ECDSA Public Key (raw hex):", pub_ecdsa.to_string_raw()) |
| 83 | + |
| 84 | + # Reload the private key from raw hex |
| 85 | + priv_ecdsa_loaded = PrivateKey.from_string_ecdsa(priv_ecdsa.to_string_raw()) |
| 86 | + # Reload the public key from raw hex |
| 87 | + pub_ecdsa_loaded = PublicKey.from_string_ecdsa(pub_ecdsa.to_string_raw()) |
| 88 | + |
| 89 | + # Sign and verify |
| 90 | + message = b"Hello, ECDSA!" |
| 91 | + signature = priv_ecdsa.sign(message) |
| 92 | + try: |
| 93 | + pub_ecdsa.verify(signature, message) |
| 94 | + print("ECDSA signature verified successfully.") |
| 95 | + except Exception as e: |
| 96 | + print("ECDSA signature verification failed:", e) |
| 97 | + |
| 98 | + # Sanity checks |
| 99 | + print("Private keys match?" , priv_ecdsa_loaded.to_string_raw() == priv_ecdsa.to_string_raw()) |
| 100 | + print("Public keys match?" , pub_ecdsa_loaded.to_string_raw() == pub_ecdsa.to_string_raw()) |
| 101 | + print() |
| 102 | + |
| 103 | + |
| 104 | +def example_ecdsa_der(): |
| 105 | + print("=== ECDSA (DER) ===") |
| 106 | + # Generate ECDSA private key |
| 107 | + priv_ecdsa = PrivateKey.generate_ecdsa() |
| 108 | + print("ECDSA Private Key (DER hex):", priv_ecdsa.to_string_der()) |
| 109 | + |
| 110 | + # Derive the public key and show DER |
| 111 | + pub_ecdsa = priv_ecdsa.public_key() |
| 112 | + print("ECDSA Public Key (DER hex):", pub_ecdsa.to_string_der()) |
| 113 | + |
| 114 | + # Reload the private key from DER hex |
| 115 | + priv_ecdsa_loaded = PrivateKey.from_string_der(priv_ecdsa.to_string_der()) |
| 116 | + # Reload the public key from DER hex |
| 117 | + pub_ecdsa_loaded = PublicKey.from_string_der(pub_ecdsa.to_string_der()) |
| 118 | + |
| 119 | + # Sign and verify |
| 120 | + message = b"Hello, ECDSA in DER!" |
| 121 | + signature = priv_ecdsa.sign(message) |
| 122 | + try: |
| 123 | + pub_ecdsa.verify(signature, message) |
| 124 | + print("DER-based ECDSA signature verified successfully.") |
| 125 | + except Exception as e: |
| 126 | + print("DER-based ECDSA signature verification failed:", e) |
| 127 | + |
| 128 | + # Sanity checks |
| 129 | + print("Private keys match?" , priv_ecdsa_loaded.to_string_der() == priv_ecdsa.to_string_der()) |
| 130 | + print("Public keys match?" , pub_ecdsa_loaded.to_string_der() == pub_ecdsa.to_string_der()) |
| 131 | + print() |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + example_ed25519_raw() |
| 136 | + example_ed25519_der() |
| 137 | + example_ecdsa_raw() |
| 138 | + example_ecdsa_der() |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments