Last active
May 26, 2023 02:27
-
-
Save ufxpri/2858a3134c63692aa7cd2bd42f14a5a5 to your computer and use it in GitHub Desktop.
pynacl reuse private_key for later use (Ed25519)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # based on this document (https://pynacl.readthedocs.io/en/latest/signing/) | |
| from nacl.signing import SigningKey | |
| from nacl.encoding import Base64Encoder | |
| # generate key pair | |
| signing_key = SigningKey.generate() | |
| signed_1 = signing_key.sign(b"test text toat") | |
| public_key_1 = signing_key.verify_key.encode(encoder=Base64Encoder) | |
| # get private key as base64 format | |
| private_key_base64 = signing_key.encode(encoder=Base64Encoder) | |
| # ... time passes ... | |
| # load key pair by entering | |
| signing_key = SigningKey(private_key_base64, encoder=Base64Encoder) | |
| signed_2 = signing_key.sign(b"test text toat") | |
| public_key_2 = signing_key.verify_key.encode(encoder=Base64Encoder) | |
| print(signed_1) | |
| print(signed_2) | |
| print(public_key_1) | |
| print(public_key_2) | |
| print(private_key_base64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from nacl.signing import SigningKey | |
| from nacl.encoding import HexEncoder | |
| # Generate a new random signing key | |
| signing_key = SigningKey.generate() | |
| # Serialize the signing key (private key) and save it for later use | |
| signing_key_hex = signing_key.encode(encoder=HexEncoder) | |
| # Write the private key to a file | |
| with open('private.key', 'w') as f: | |
| f.write(signing_key_hex.decode()) | |
| # Obtain the verify key (public key) for a given signing key | |
| verify_key = signing_key.verify_key | |
| # Serialize the verify key to send it to a third party | |
| verify_key_hex = verify_key.encode(encoder=HexEncoder) | |
| # Write the public key to a file | |
| with open('public.pub', 'w') as f: | |
| f.write(verify_key_hex.decode()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from nacl.signing import SigningKey, VerifyKey | |
| from nacl.encoding import HexEncoder | |
| # Load the private key from a file | |
| with open('private.key', 'r') as f: | |
| signing_key_hex = f.read() | |
| signing_key = SigningKey(signing_key_hex, encoder=HexEncoder) | |
| # Load the public key from a file | |
| with open('public.pub', 'r') as f: | |
| verify_key_hex = f.read() | |
| verify_key = VerifyKey(verify_key_hex, encoder=HexEncoder) | |
| # test sign and verify | |
| signed = signing_key.sign(b"test text toat", encoder=HexEncoder) | |
| print(verify_key.verify(signed, encoder=HexEncoder)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment