Last active
          May 24, 2024 14:28 
        
      - 
      
- 
        Save mirhatx/6f52b82f96adee3c9bfdc32b43d09532 to your computer and use it in GitHub Desktop. 
    sign jwt using a public key via HS256
  
        
  
    
      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 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