Last active
August 24, 2017 18:46
-
-
Save fabianobizarro/6968e581bb245ab8c4650326253a06ce to your computer and use it in GitHub Desktop.
Asymmetric JWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require('jsonwebtoken'); | |
const fs = require('fs'); | |
const PRIVATE_KEY = fs.readFileSync('./cert/private.key'); | |
const PUBLIC_KEY = fs.readFileSync('./cert/cert.pub'); | |
const userInfo = { | |
id: 1, | |
name: 'example' | |
}; | |
console.log('===================================='); | |
console.log('Initial Payload:'); | |
console.log(userInfo); | |
console.log('===================================='); | |
console.log(); | |
let token = jwt.sign(userInfo, PRIVATE_KEY, { algorithm: 'RS256' }); | |
console.log('===================================='); | |
console.log('token:'); | |
console.log(token); | |
console.log('===================================='); | |
console.log(); | |
let payload = jwt.verify(token, PUBLIC_KEY, (err, payload) => { | |
if (err) | |
console.log(err) | |
else { | |
console.log('===================================='); | |
console.log('decoded:'); | |
console.log(payload); | |
console.log('===================================='); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openssl genrsa -out /path/to/key/private.key 4096 | |
openssl rsa -in /path/to/key/private.key -pubout -out cert/cert.pub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment