Skip to content

Instantly share code, notes, and snippets.

@ygotthilf
Last active May 29, 2023 10:10
Embed
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 jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub
@yayen-lin
Copy link

yayen-lin commented Jul 10, 2021

Thanks so much!
I'm sorry I'm still new to this but does the private key go to .gitignore or both the public and private key go to .gitignore?

@bentocin
Copy link

Thanks so much!
I'm sorry I'm still new to this but does the private key go to .gitignore or both the public and private key go to .gitignore?

To my best knowledge:

Where you put them depends on your secrets management. We don't put either of it in the repo. This information is rather specific to your deployment and can be considered as part of the environment. It is not specific to your application and thus should be treated accordingly.

Depending on your project setup some of the following solutions might be suitable for you:

Another option might be to create them ad-hoc during your deployment.

@yayen-lin
Copy link

Hi @bentocin,

Thanks again for the detailed answer, I looked up a few posts on the intenet and you answer makes sense to me now.
I decided to not put either of it in the repo.
Really appreciate your help and solutions!

@joe-gasewicz
Copy link

Cool! 😸

@osaavedrafsl
Copy link

thank you!

@FlavioBernaski
Copy link

Thanks for this! Works nicely!

@jonathanvanschenck
Copy link

Perfect, Thanks!

@JenniferOH
Copy link

Thank you!!

@hatunaa
Copy link

hatunaa commented Sep 26, 2021

thank you :))

@GersomR-afk
Copy link

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key

thanks so much!!

@KenjiFukudome
Copy link

Thank you so much!

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub

@adamwdennis
Copy link

Worked for me! Thank you 🙏

@Ezriral
Copy link

Ezriral commented Dec 3, 2021

@David5050
Copy link

Thank you

@yantoiswanto
Copy link

Thanks you

@mludi
Copy link

mludi commented Feb 23, 2022

Thanks! :)

@rvirani1
Copy link

rvirani1 commented Mar 1, 2022

Thanks so much!

@vizvasrj
Copy link

Thanks

@Bumsque
Copy link

Bumsque commented Jun 16, 2022

Thanks

@navghanzed
Copy link

thanks

@ritavdasthou
Copy link

Thanks a lot !

@HansKim6
Copy link

thanks

@TWebSrDev
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!

@MJENDIDS
Copy link

MJENDIDS commented Dec 5, 2022

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

@canhlinh
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;}'  jwtRS256.key.pub

@XDanny322
Copy link

Thank you indeed!

@jdboris
Copy link

jdboris commented Feb 16, 2023

Thanks

@tgirotto
Copy link

tgirotto commented Mar 7, 2023

thanks

@itaow
Copy link

itaow commented Mar 9, 2023

Thanks!

@Jehovanie
Copy link

I think this link resolve also this problem.
https://symfony.com/bundles/LexikJWTAuthenticationBundle/current/index.html
thank you.

@francesco-plt
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?

@meltingrock
Copy link

Thanks

@nikelborm
Copy link

Thanks!
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