Skip to content

Instantly share code, notes, and snippets.

@berkorbay
Last active October 5, 2023 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkorbay/d7470d4385c021b2e2c63ba20046ed0f to your computer and use it in GitHub Desktop.
Save berkorbay/d7470d4385c021b2e2c63ba20046ed0f to your computer and use it in GitHub Desktop.
Working method for generating RS256 private/public JWT keys without problem in py-jose
from jose import jwt
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
#https://dev.to/aaronktberry/generating-encrypted-key-pairs-in-python-69b
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
unencrypted_pem_private_key = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
pem_public_key = private_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
private_key_file = open("rk_auth_private.pem", "w")
private_key_file.write(unencrypted_pem_private_key.decode())
private_key_file.close()
public_key_file = open("rk_auth_public.pub", "w")
public_key_file.write(pem_public_key.decode())
public_key_file.close()
private_key_from_file = open("rk_auth_private.pem", "r").read().encode()
## OPTIONAL: public key from private key
# from cryptography.hazmat.primitives.serialization import load_pem_private_key
# pem_public_key_from_file = load_pem_private_key(private_key_from_file, password=None)
token = jwt.encode({"a":"b"}, private_key_from_file, algorithm='RS256')
decoded_token = jwt.decode(token, public_key_from_file, algorithms=['RS256'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment