Skip to content

Instantly share code, notes, and snippets.

@mirhatx
Last active May 24, 2024 14:28
Show Gist options
  • Save mirhatx/6f52b82f96adee3c9bfdc32b43d09532 to your computer and use it in GitHub Desktop.
Save mirhatx/6f52b82f96adee3c9bfdc32b43d09532 to your computer and use it in GitHub Desktop.
sign jwt using a public key via HS256
const fs = require('fs');
const jwt = require('jsonwebtoken');
const publicKey = fs.readFileSync('/x/y/z/public.pem');
// jwt payload
const payload = {
login: "admin"
};
// public key?
// with RSA to sign and verify you need private key
// with HMAC you need same secret to sign and verify
const finalTok = jwt.sign(payload, publicKey, { algorithm: 'HS256' });
// changing the algorithm will result in calling HSA256(public_key, data) <-
console.log("JWT:", finalTok);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment