Skip to content

Instantly share code, notes, and snippets.

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

abuzarhamza commented Jun 26, 2019

thanks :)

@ruaanvds
Copy link

ruaanvds commented Oct 10, 2019

Straight to the point - thanks for this.

@MonsterTi
Copy link

MonsterTi commented Nov 4, 2019

thanks

@jcramalho
Copy link

jcramalho commented Dec 9, 2019

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

propatodiya commented Apr 11, 2020

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

Thanks, it's work for me.

@sophiagatliff
Copy link

sophiagatliff commented May 31, 2020

Works perfect! Thank you!!!!!

@70nyIT
Copy link

70nyIT commented Jul 6, 2020

Thanks!

@subhadeepdas91
Copy link

subhadeepdas91 commented Jul 21, 2020

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

IseeUDenis commented Sep 10, 2020

Thanks!

@sat0yu
Copy link

sat0yu commented Oct 17, 2020

Thanks🙏

@psych0panda
Copy link

psych0panda commented Nov 24, 2020

Thanks!

@charlygame
Copy link

charlygame commented Dec 17, 2020

Cool! Thanks.

@JorgeSivil
Copy link

JorgeSivil commented Dec 29, 2020

Hello, can JWT work with a passphrased key?

@kimisme9386
Copy link

kimisme9386 commented Dec 30, 2020

Thanks a lot.

@calebpitan
Copy link

calebpitan commented Jan 6, 2021

Thanks a lot

@profoundpanda
Copy link

profoundpanda commented Jan 20, 2021

Thanks a lot. Super helpful.!

@jokermt235
Copy link

jokermt235 commented Jan 21, 2021

like

@mssoylu
Copy link

mssoylu commented Jan 29, 2021

thank you for this. you saved me.

@Ruborcalor
Copy link

Ruborcalor commented Feb 19, 2021

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

hermandinho commented Mar 7, 2021

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

anand-dhage-sp commented Apr 27, 2021

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

@ekundayo-ab
Copy link

ekundayo-ab 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

bentocin 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?

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

yayen-lin commented Jul 11, 2021

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

joe-gasewicz commented Aug 12, 2021

Cool! 😸

@osaavedrafsl
Copy link

osaavedrafsl commented Aug 12, 2021

thank you!

@FlavioBernaski
Copy link

FlavioBernaski commented Aug 26, 2021

Thanks for this! Works nicely!

@jonathanvanschenck
Copy link

jonathanvanschenck commented Aug 27, 2021

Perfect, Thanks!

@JenniferOH
Copy link

JenniferOH commented Sep 11, 2021

Thank you!!

@hatunaa
Copy link

hatunaa commented Sep 26, 2021

thank you :))

@GersomR-afk
Copy link

GersomR-afk commented Sep 26, 2021

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

thanks so much!!

@KenjiFukudome
Copy link

KenjiFukudome commented Nov 22, 2021

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

adamwdennis commented Nov 30, 2021

Worked for me! Thank you 🙏

@Ezriral
Copy link

Ezriral commented Dec 3, 2021

@David5050
Copy link

David5050 commented Dec 8, 2021

Thank you

@yantoiswanto
Copy link

yantoiswanto commented Jan 23, 2022

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

vizvasrj commented May 12, 2022

Thanks

@Bumsque
Copy link

Bumsque commented Jun 16, 2022

Thanks

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