Skip to content

Instantly share code, notes, and snippets.

Last active May 29, 2023 10:10
What would you like to do?
How to generate JWT RS256 key
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out
cat jwtRS256.key
Copy link

I cannot believe the amount of time and frustration it took to FINALLY stumble upon this amazing post, and the equally amazing comments that provided additional clarity. IMO, while fully acknowledging that the actual generation of public/private keys using openssl/ssh-keygen is beyond scope for node-jsonwebtoken docs, still, it would be a great service to their user base, to if not actually include the few lines required to properly create compatible public/private keys, to at least maybe add references/links to CLEAR, helpful resources LIKE THIS!
Thanks ALOT to the OP, and everyone who contributed!

Copy link

MJENDIDS commented Dec 5, 2022

T. H. A. N. K. S. 🙏

Copy link

canhlinh commented Dec 7, 2022

Use this command to convert the keys to one line string

awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}'  jwtRS256.key
awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}'

Copy link

Thank you indeed!

Copy link

jdboris commented Feb 16, 2023


Copy link

tgirotto commented Mar 7, 2023


Copy link

itaow commented Mar 9, 2023


Copy link

I think this link resolve also this problem.
thank you.

Copy link

Thanks! What if I need to use the private key associated to a Metamask wallet to sign? Can I do it by manipulating the key or do I need another type of key altogether?

Copy link


Copy link

BTW @ygotthilf you can add -N '' to ssh keygen and will not even ask for passphrase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment