Skip to content

Commit 8b956f3

Browse files
committed
test: der key length
Signed-off-by: exploreriii <[email protected]> fix: ambiguous key signing but needs refining fix: solo key type ecdsa Signed-off-by: exploreriii <[email protected]> documentation: rough notes public_key file before new tests and refactor Signed-off-by: exploreriii <[email protected]> feat: restore generic from_bytes method with warnings and documentation Signed-off-by: exploreriii <[email protected]> feat: restore generic from_string method with warnings and documentation Signed-off-by: exploreriii <[email protected]> feat: to_bytes generic and documentation Signed-off-by: exploreriii <[email protected]> chore: cleanup to string methods Signed-off-by: exploreriii <[email protected]> chore: cleanup signatures Signed-off-by: exploreriii <[email protected]> fix: str not bytes for hex Signed-off-by: exploreriii <[email protected]> feat: examples public key uses Signed-off-by: exploreriii <[email protected]> chore: rough notes test Signed-off-by: exploreriii <[email protected]> chore: documentation for public key tests Signed-off-by: exploreriii <[email protected]> chore: cleanup public key tests Signed-off-by: exploreriii <[email protected]> chore: revert private key notes Signed-off-by: exploreriii <[email protected]> chore: tidy up private key Signed-off-by: exploreriii <[email protected]> chore: tidy up private key2 Signed-off-by: exploreriii <[email protected]> feat: private key examples Signed-off-by: exploreriii <[email protected]> feat: test_key_private Signed-off-by: exploreriii <[email protected]> feat: missing tests Signed-off-by: exploreriii <[email protected]> PK8 Signed-off-by: exploreriii <[email protected]> revert account create test Signed-off-by: exploreriii <[email protected]> revert topic update Signed-off-by: exploreriii <[email protected]> revert topic message Signed-off-by: exploreriii <[email protected]> revert topic delete Signed-off-by: exploreriii <[email protected]> revert topic create Signed-off-by: exploreriii <[email protected]> remove surplus key test Signed-off-by: exploreriii <[email protected]> surplus key file Signed-off-by: exploreriii <[email protected]> generate() typo Signed-off-by: exploreriii <[email protected]>
1 parent 391a62f commit 8b956f3

32 files changed

+2195
-134
lines changed

examples/account_create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def create_new_account():
1818
client = Client(network)
1919

2020
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
21-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
21+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
2222
client.set_operator(operator_id, operator_key)
2323

24-
new_account_private_key = PrivateKey.generate()
24+
new_account_private_key = PrivateKey.generate_ed25519()
2525
new_account_public_key = new_account_private_key.public_key()
2626

2727
transaction = (

examples/keys_private_der.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Example file: Demonstrating how to serialize a PrivateKey to DER (hex)
3+
and then load it back.
4+
*WARNING* DER seeds should not be printed or exposed in a real‑world scenario
5+
"""
6+
from cryptography.exceptions import InvalidSignature
7+
from hiero_sdk_python.crypto.private_key import PrivateKey
8+
9+
def example_serialize_ed25519_der() -> None:
10+
11+
print("=== Ed25519: Serialize to DER ===")
12+
privkey = PrivateKey.generate_ed25519()
13+
print("Generated Ed25519 key:", privkey)
14+
15+
# Convert to DER (TraditionalOpenSSL)
16+
der_bytes = privkey.to_bytes_der()
17+
print("DER bytes length =", len(der_bytes))
18+
19+
der_hex = der_bytes.hex()
20+
print("DER hex =", der_hex)
21+
22+
# Load it back
23+
privkey2 = PrivateKey.from_string_der(der_hex)
24+
print("Loaded back from DER =", privkey2)
25+
26+
# Sign test
27+
signature = privkey2.sign(b"test")
28+
privkey2.public_key().verify(signature, b"test")
29+
print("Ed25519 DER reload: Verified signature OK.\n")
30+
31+
def example_serialize_ecdsa_der() -> None:
32+
33+
print("=== ECDSA: Serialize to DER ===")
34+
privkey = PrivateKey.generate_ecdsa()
35+
print("Generated ECDSA key:", privkey)
36+
37+
# Convert to DER
38+
der_bytes = privkey.to_bytes_der()
39+
der_hex = der_bytes.hex()
40+
print("DER hex =", der_hex)
41+
42+
# Load it back
43+
privkey2 = PrivateKey.from_string_der(der_hex)
44+
print("Loaded back from DER =", privkey2)
45+
46+
# Sign test
47+
signature = privkey2.sign(b"hello ECDSA serialization")
48+
privkey2.public_key().verify(signature, b"hello ECDSA serialization")
49+
print("ECDSA DER reload: Verified signature OK.\n")
50+
51+
def main():
52+
example_serialize_ed25519_der()
53+
example_serialize_ecdsa_der()
54+
55+
if __name__ == "__main__":
56+
main()

examples/keys_private_ecdsa.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
Example file: Working with ECDSA (secp256k1) PrivateKey using the PrivateKey class
3+
*WARNING* ECDSA seeds should not be printed or exposed in a real‑world scenario
4+
"""
5+
from cryptography.exceptions import InvalidSignature
6+
from hiero_sdk_python.crypto.private_key import PrivateKey
7+
8+
def example_generate_ecdsa() -> None:
9+
"""
10+
Demonstrates generating a new ECDSA (secp256k1) PrivateKey,
11+
signing data, and verifying with the associated PublicKey.
12+
"""
13+
print("=== ECDSA: Generate & Sign ===")
14+
# 1) Generate ECDSA
15+
privkey = PrivateKey.generate("ecdsa")
16+
print("Generated ECDSA PrivateKey (hex) =", privkey)
17+
18+
# 2) Get the public key
19+
pubkey = privkey.public_key()
20+
print("Derived public key =", pubkey)
21+
22+
# 3) Sign data
23+
data = b"hello ECDSA!"
24+
signature = privkey.sign(data)
25+
print("Signature (hex) =", signature.hex())
26+
27+
# 4) Verify signature using the public key
28+
try:
29+
pubkey.verify(signature, data)
30+
print("Signature is VALID (ECDSA)!")
31+
except InvalidSignature:
32+
print("Signature is INVALID (ECDSA)!")
33+
print()
34+
35+
def example_load_ecdsa_raw() -> None:
36+
"""
37+
Demonstrates creating an ECDSA (secp256k1) PrivateKey from raw 32 bytes (a scalar).
38+
Then signs and verifies.
39+
"""
40+
print("=== ECDSA: Load from Raw ===")
41+
# 32 bytes. Example: 0xAB repeated.
42+
raw_scalar_hex = "ab" * 32
43+
raw_scalar = bytes.fromhex(raw_scalar_hex)
44+
45+
# 1) Generate ECDSA
46+
privkey = PrivateKey.from_bytes_ecdsa(raw_scalar)
47+
print("Loaded ECDSA PrivateKey from raw scalar =", privkey)
48+
49+
# 2) Get the public key
50+
pubkey = privkey.public_key()
51+
52+
# 3) Sign/Verify
53+
data = b"ECDSA from raw scalar"
54+
signature = privkey.sign(data)
55+
try:
56+
pubkey.verify(signature, data)
57+
print("ECDSA signature valid with raw scalar!")
58+
except InvalidSignature:
59+
print("Signature invalid?!")
60+
print()
61+
62+
def example_load_ecdsa_from_hex() -> None:
63+
"""
64+
Demonstrates creating an ECDSA (secp256k1) PrivateKey from a hex-encoded 32-byte scalar.
65+
"""
66+
print("=== ECDSA: Load from Hex ===")
67+
# 32-byte scalar in hex. Must not be zero; example:
68+
ecdsa_hex = "abcdef0000000000000000000000000000000000000000000000000000000001"
69+
70+
# 1) Generate ECDSA
71+
privkey = PrivateKey.from_string_ecdsa(ecdsa_hex)
72+
print("Loaded ECDSA PrivateKey from hex =", privkey)
73+
74+
# 2) Get the public key
75+
pubkey = privkey.public_key()
76+
77+
# 3) Quick sign/verify
78+
data = b"Testing ECDSA hex load"
79+
signature = privkey.sign(data)
80+
try:
81+
pubkey.verify(signature, data)
82+
print("ECDSA signature valid with hex-loaded key!")
83+
except InvalidSignature:
84+
print("Signature invalid?!")
85+
print()
86+
87+
def example_load_ecdsa_der() -> None:
88+
"""
89+
Demonstrates loading an ECDSA (secp256k1) private key from DER bytes.
90+
TraditionalOpenSSL in hex form for demonstration.
91+
"""
92+
print("=== ECDSA: Load from DER ===")
93+
# Example TraditionalOpenSSL DER-encoded key (for scalar=1).
94+
# (Truncated for demonstration)
95+
der_hex = (
96+
"304e02010104"
97+
"200100000000000000000000000000000000000000000000000000000000000000"
98+
"a00706052b8104000aa1440342000479be667ef9dcbbac55a06295ce870b07029bfcdb"
99+
"2dce28d959f2815b16f8179842a2f1423fc2440aeddc92f8cd3dfff65d61b2334fa1dafc519e6b9f3"
100+
)
101+
102+
# 1) Generate DER
103+
privkey = PrivateKey.from_string_der(der_hex)
104+
print("Loaded ECDSA PrivateKey from DER =", privkey)
105+
106+
# 2) Get the public key
107+
pubkey = privkey.public_key()
108+
109+
# 3) Sign/Verify
110+
data = b"DER-based ECDSA"
111+
signature = privkey.sign(data)
112+
try:
113+
pubkey.verify(signature, data)
114+
print("ECDSA signature valid using DER-loaded key!")
115+
except InvalidSignature:
116+
print("Signature invalid?!")
117+
print()
118+
119+
def main_ecdsa():
120+
example_generate_ecdsa()
121+
example_load_ecdsa_raw()
122+
example_load_ecdsa_from_hex()
123+
example_load_ecdsa_der()
124+
125+
if __name__ == "__main__":
126+
main_ecdsa()

examples/keys_private_ed25519.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
Example file: Working with Ed25519 PrivateKey using the PrivateKey class
3+
*WARNING* Ed25519 seeds should not be printed or exposed in a real‑world scenario
4+
"""
5+
from cryptography.exceptions import InvalidSignature
6+
from hiero_sdk_python.crypto.private_key import PrivateKey
7+
8+
def example_generate_ed25519() -> None:
9+
"""
10+
Demonstrates generating a brand new Ed25519 PrivateKey, signing data,
11+
and verifying the signature with its corresponding PublicKey.
12+
"""
13+
print("=== Ed25519: Generate & Sign ===")
14+
# 1) Generate Ed25519 by specifying in "'"
15+
privkey = PrivateKey.generate("ed25519")
16+
print("Generated Ed25519 PrivateKey (hex) =", privkey)
17+
18+
# 2) Get the public key for this ed25519 private key
19+
pubkey = privkey.public_key()
20+
print("Derived public key =", pubkey)
21+
22+
# 3) Sign data
23+
data = b"hello ed25519!"
24+
signature = privkey.sign(data)
25+
print("Signature (hex) =", signature.hex())
26+
# 4) Verify signature using the public key
27+
try:
28+
pubkey.verify(signature, data)
29+
print("Signature is VALID (Ed25519)!")
30+
except InvalidSignature:
31+
print("Signature is INVALID (Ed25519)!")
32+
print()
33+
34+
def example_load_ed25519_raw() -> None:
35+
"""
36+
Demonstrates creating a PrivateKey from a 32-byte raw Ed25519 seed.
37+
Then uses it for signing and verifying.
38+
"""
39+
print("=== Ed25519: Load from Raw ===")
40+
raw_seed_hex = "01" * 32 # Example 32 bytes
41+
42+
# Now this becomes a raw seed
43+
raw_seed = bytes.fromhex(raw_seed_hex)
44+
45+
# Create private ed25519 key from ed25519 seed.
46+
privkey = PrivateKey.from_bytes_ed25519(raw_seed)
47+
print("Loaded Ed25519 PrivateKey from raw seed =", privkey)
48+
49+
# Derive the public key
50+
pubkey = privkey.public_key()
51+
52+
# Sign & verify
53+
data = b"Ed25519 from raw"
54+
signature = privkey.sign(data)
55+
try:
56+
pubkey.verify(signature, data)
57+
print("Signature valid with Ed25519 from raw seed!")
58+
except InvalidSignature:
59+
print("Signature invalid?!")
60+
print()
61+
62+
def example_load_ed25519_from_hex() -> None:
63+
"""
64+
Demonstrates creating a PrivateKey from a hex-encoded string for Ed25519.
65+
Must be 32 bytes in total, i.e. 64 hex characters.
66+
"""
67+
print("=== Ed25519: Load from Hex ===")
68+
# A random 32-byte example:
69+
ed_hex = "a1" * 16 + "b2" * 16 # => 64 hex characters => 32 bytes
70+
71+
# Create private ed25519 key from hex.
72+
privkey = PrivateKey.from_string_ed25519(ed_hex)
73+
print("Loaded Ed25519 PrivateKey from hex =", privkey)
74+
75+
# Derive the public key
76+
pubkey = privkey.public_key()
77+
78+
# Sign & verify
79+
data = b"Test data"
80+
signature = privkey.sign(data)
81+
try:
82+
pubkey.verify(signature, data)
83+
print("Ed25519 signature valid with hex-loaded key!")
84+
except InvalidSignature:
85+
print("Signature invalid?!")
86+
print()
87+
88+
def example_load_ed25519_der() -> None:
89+
"""
90+
Demonstrates loading an Ed25519 private key from DER bytes (hex form).
91+
In actual usage, you might read the raw DER bytes from a file (binary),
92+
then call from_der(...) or from_string_der(...).
93+
"""
94+
print("=== Ed25519: Load from DER ===")
95+
# This DER encodes a small example Ed25519 key whose raw seed might be all 0x01.
96+
# 46 bytes in total; for demonstration only.
97+
der_hex = (
98+
"302e020100300506032b657004220420"
99+
"0101010101010101010101010101010101010101010101010101010101010101"
100+
)
101+
102+
# Create private and public key
103+
privkey = PrivateKey.from_string_der(der_hex)
104+
print("Loaded Ed25519 PrivateKey from DER =", privkey)
105+
pubkey = privkey.public_key()
106+
107+
# Sign/Verify
108+
data = b"DER-based Ed25519"
109+
signature = privkey.sign(data)
110+
try:
111+
pubkey.verify(signature, data)
112+
print("Ed25519 signature valid using DER-loaded key!")
113+
except InvalidSignature:
114+
print("Signature invalid?!")
115+
print()
116+
117+
def main_ed25519():
118+
example_generate_ed25519()
119+
example_load_ed25519_raw()
120+
example_load_ed25519_from_hex()
121+
example_load_ed25519_der()
122+
123+
if __name__ == "__main__":
124+
main_ed25519()

0 commit comments

Comments
 (0)