Last active
March 11, 2021 12:18
-
-
Save SuperCipher/2f05c4b618d1774779412ade3a2bcd7b to your computer and use it in GitHub Desktop.
JWT Ed25519 encode decode example (EdDSA)
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
import jwt | |
from cryptography.hazmat.primitives import serialization | |
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey | |
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption, PublicFormat | |
from nacl.public import PrivateKey | |
from cryptography.hazmat.backends import default_backend | |
key = PrivateKey.generate() | |
cryptography_key = Ed25519PrivateKey.from_private_bytes(bytes(key)) | |
private_key_byte = cryptography_key.private_bytes( | |
encoding=Encoding.PEM, format=PrivateFormat.PKCS8, encryption_algorithm=NoEncryption() | |
) | |
private_key_byte | |
# .decode("utf-8") | |
private_key = serialization.load_pem_private_key( | |
private_key_byte, password=None, backend=default_backend() | |
) | |
private_key | |
print(private_key) | |
public_key_byte = cryptography_key.public_key().public_bytes( | |
encoding=Encoding.OpenSSH, | |
format=PublicFormat.OpenSSH, | |
) | |
public_key_byte | |
# .decode("utf-8") | |
# -------------------------------------------------- | |
private_key_string = private_key_byte.decode("utf-8") | |
private_key_string | |
public_key_string = public_key_byte.decode("utf-8") | |
public_key_string | |
# -------------------------------------------------- | |
private_key_byte = str.encode(private_key_string) | |
public_key_byte = str.encode(public_key_string) | |
public_key = serialization.load_ssh_public_key( | |
public_key_byte, backend=default_backend() | |
) | |
encoded = jwt.encode({"some": "payload"}, private_key, algorithm="EdDSA") | |
encoded | |
decoded = jwt.decode(encoded, public_key, algorithms=["EdDSA"],backend=default_backend()) | |
decoded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment