Skip to content

Instantly share code, notes, and snippets.

@ygotthilf
Last active April 17, 2024 04:10
Show Gist options
  • Save ygotthilf/baa58da5c3dd1f69fae9 to your computer and use it in GitHub Desktop.
Save ygotthilf/baa58da5c3dd1f69fae9 to your computer and use it in GitHub Desktop.
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
@profoundpanda
Copy link

Thanks a lot. Super helpful.!

@jokermt235
Copy link

like

@mssoylu
Copy link

mssoylu commented Jan 29, 2021

thank you for this. you saved me.

@Ruborcalor
Copy link

Thanks for this! If you want to replace all the new lines with literal \n characters you can use the following awk command: awk -v ORS='\\n' '1' jwtRS256.key. It leaves an extra \n at the end of the line though.
https://stackoverflow.com/questions/38672680/replace-newlines-with-literal-n/38674872

@ratio91
Copy link

ratio91 commented Mar 2, 2021

It is also possible to just do it with ssh-keygen:

ssh-keygen -t rsa -P "" -b 4096 -m PEM -f jwtRS256.key
ssh-keygen -e -m PEM -f jwtRS256.key > jwtRS256.key.pub

thanks @bentocin, works like a charm!

@hermandinho
Copy link

Many Thanks

@prufrock
Copy link

prufrock commented Apr 1, 2021

It is also possible to just do it with ssh-keygen:

ssh-keygen -t rsa -P "" -b 4096 -m PEM -f jwtRS256.key
ssh-keygen -e -m PEM -f jwtRS256.key > jwtRS256.key.pub

Works great for me! Thanks!

@anand-dhage-sp
Copy link

How can i use RS384 algorithm in above command to create rsa public and private key.

@ekundayo-ab
Copy link

It's also possible to generate keys using openssl only:

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem

👍

@jhaoheng
Copy link

jhaoheng commented Jul 8, 2021

very thanks

@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 :))

@gersomonline
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

nikelborm commented Apr 22, 2023

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

@xiaoyuze88
Copy link

awesome!!! Thx

@mtwsec
Copy link

mtwsec commented Jun 13, 2023

Thanks! Nice and easy!

@ziyaDev
Copy link

ziyaDev commented Jun 22, 2023

Awesome, thank youuu

@gokhan-uysal
Copy link

many thanks

@Aaron2963
Copy link

save my day, thanks

@NNHungLong
Copy link

Thanks

@AColocho
Copy link

Now, it doesn't work because newer version of ssh-keygen no longer uses PEM format as default key format. It have changed to RFC4716 for default key format. Thus, I have to set PEM format explicitly :

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

This will work 👍

In case anyone is getting an error from openssl, try this.

@solairerove
Copy link

many thanks

@iamanikeev
Copy link

thank you good sir!

@muhammadfazeel1
Copy link

Thanks :)

@honne23
Copy link

honne23 commented Jan 31, 2024

nice

@mthienpont
Copy link

oh hell yeah

@farzadafi
Copy link

concise and useful :)

@peterDitrih
Copy link

thx

@IHsuanLu
Copy link

thanks!

@bmewj
Copy link

bmewj commented Mar 24, 2024

Thanks

@danwonghk01
Copy link

Thanks!! Amazing!

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