Last active
July 20, 2020 15:03
-
-
Save nileshevrywhr/f8246373e0261808db533ea636019ceb to your computer and use it in GitHub Desktop.
JWT using RS256
This file contains 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 base64 | |
import rsa | |
import hashlib | |
(pubkey, privkey) = rsa.newkeys(512) | |
with open('private.key','wb') as keyfile: | |
keyfile.write(privkey._save_pkcs1_pem()) | |
with open('public.key','wb') as keyfile: | |
keyfile.write(pubkey._save_pkcs1_pem()) | |
file = open('private.key') | |
key = file.read() | |
header = '{"typ": "JWT", "alg": "RS256"}' | |
payload = '{"iss":"joe", "exp":1300819380, "http://example.com/is_root":true}' | |
urlSafeEncodedBytes = base64.urlsafe_b64encode(header.encode("utf-8")) | |
urlSafeEncodedHeader = str(urlSafeEncodedBytes,"utf-8").split('=')[0] | |
urlSafeEncodedBytes = base64.urlsafe_b64encode(payload.encode("utf-8")) | |
urlSafeEncodedPayload = str(urlSafeEncodedBytes,"utf-8").split('=')[0] | |
token = urlSafeEncodedHeader + '.' + urlSafeEncodedPayload | |
sign = base64.urlsafe_b64encode(rsa.sign(token.encode("utf-8"), rsa.PrivateKey._load_pkcs1_pem(bytes(key,'utf-8')), 'SHA-256')).decode('utf-8').rstrip('=') | |
token = token + '.' + sign | |
print(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment