Skip to content

Instantly share code, notes, and snippets.

@ygotthilf
Last active February 10, 2026 20:17
Show Gist options
  • Select an option

  • Save ygotthilf/baa58da5c3dd1f69fae9 to your computer and use it in GitHub Desktop.

Select an option

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
@energister
Copy link

energister commented Jun 1, 2019

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

@abuzarhamza
Copy link

thanks :)

@ruaanvds
Copy link

Straight to the point - thanks for this.

@MonsterTi
Copy link

thanks

@jcramalho
Copy link

This piece of short code/information should be include in JWT packages documentation...
I was fighting against ssh keys formats and this completely solved the problem.
Thanks

@sikderg
Copy link

sikderg commented Dec 27, 2019

Thank you 👍

@propatodiya
Copy link

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

Thanks, it's work for me.

@sophiagatliff
Copy link

Works perfect! Thank you!!!!!

@70nyIT
Copy link

70nyIT commented Jul 6, 2020

Thanks!

@techie-subhadeep
Copy link

How to convert multi-line public and private key to single-line
so that it can be put in an environment variable

@IseeUDenis
Copy link

Thanks!

@sat0yu
Copy link

sat0yu commented Oct 17, 2020

Thanks🙏

@psych0panda
Copy link

Thanks!

@charlygame
Copy link

Cool! Thanks.

@JorgeSivil
Copy link

Hello, can JWT work with a passphrased key?

@kimisme9386
Copy link

Thanks a lot.

@calebpitan
Copy link

Thanks a lot

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

@tag6071
Copy link

tag6071 commented May 28, 2021

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!

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