Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Forked from Holger-Will/jwtRS256.sh
Created March 4, 2023 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonbosa/16ece55e7f496b2b569b263807afb3e5 to your computer and use it in GitHub Desktop.
Save andersonbosa/16ece55e7f496b2b569b263807afb3e5 to your computer and use it in GitHub Desktop.
generate public private key pair (RSA RS256) for use with koa-jwt jasonwebtoken etc.
# generate private key
openssl genrsa -out private.pem 2048
# extatract public key from it
openssl rsa -in private.pem -pubout > public.pem
const jwt2 = require('jsonwebtoken');
const fs = require("fs")
const public_key = fs.readFileSync('public.pem');
const private_key = fs.readFileSync('private.pem');
var token=jwt2.sign({"user":"me"},private_key, { algorithm: 'RS256'})
var res = jwt2.verify(token, public_key, { algorithm: 'RS256'})
console.log(res)
@andersonbosa
Copy link
Author

my version

#!/bin/bash

SCRIPT_PATH=$(dirname $0)

echo -e '\n# GENERATING PRIVATE KEY'
openssl genrsa -out $SCRIPT_PATH/private.pem 4096

echo -e '\n# EXTATRACTING PUBLIC KEY FROM IT'
openssl rsa -in private.pem -pubout >$SCRIPT_PATH/public.pem

echo -e '\n# MOVING KEYS TO APPLICATION DIRECTORY'
mv $SCRIPT_PATH/{private,public}.pem $SCRIPT_PATH/../.
ls -l $SCRIPT_PATH/../{private,public}.pem

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