Node-jsonwebtoken: Example about RSA and public/private keys

Created on 8 Jun 2017  路  1Comment  路  Source: auth0/node-jsonwebtoken

Hello there.
I don't know if this is a really issue, but I'm a little confuse about generating and using RSA and public/private keys.
I've tried some examples but didn't get any success.
Is there any tutorial or example about changing the use of jwt.sign and jwt.verify methods to adopt RSA and public/private keys?

  • How to generate RSA and public/private keys (for RS256 algorithm)?
  • How to manipulate those files using jwt.sign and jwt.verify (this is easier for me).
    All the best,

Most helpful comment

I was able to develop an example of generating public and private keys for RSA in Node.js
I'll share the implementation to help others with the same question :)

var fs = require('fs');
var keypair = require('keypair');

var pair = keypair({
    bits: 2048, // size for the private key in bits. Default: 2048
    e: 65537 // public exponent to use. Default: 65537
});

fs.writeFile(__dirname + '/keys/private.key', pair.private, function(err) {
    if (err) {
        return console.error(err);
    }
    console.log('RSA private key file generated');
});

fs.writeFile(__dirname + '/keys/public.pem', pair.public, function(err) {
    if (err) {
        return console.error(err);
    }
    console.log('RSA public key file generated');
});

>All comments

I was able to develop an example of generating public and private keys for RSA in Node.js
I'll share the implementation to help others with the same question :)

var fs = require('fs');
var keypair = require('keypair');

var pair = keypair({
    bits: 2048, // size for the private key in bits. Default: 2048
    e: 65537 // public exponent to use. Default: 65537
});

fs.writeFile(__dirname + '/keys/private.key', pair.private, function(err) {
    if (err) {
        return console.error(err);
    }
    console.log('RSA private key file generated');
});

fs.writeFile(__dirname + '/keys/public.pem', pair.public, function(err) {
    if (err) {
        return console.error(err);
    }
    console.log('RSA public key file generated');
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

p-brighenti picture p-brighenti  路  4Comments

yvele picture yvele  路  4Comments

ngminhduong picture ngminhduong  路  3Comments

dwelle picture dwelle  路  3Comments

salali picture salali  路  5Comments