export both Private and Public, and PEMs and JWKs

This commit is contained in:
AJ ONeal 2018-12-15 23:16:27 -07:00
parent 0a7734c6da
commit 70bfcd04bf
4 changed files with 58 additions and 20 deletions

View File

@ -5,19 +5,27 @@ var Rasha = require('rasha');
module.exports = function (bitlen, exp) { module.exports = function (bitlen, exp) {
var k = require('node-forge').pki.rsa var k = require('node-forge').pki.rsa
.generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey; .generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey;
return Rasha.exportSync({ var jwk = {
jwk: { kty: "RSA"
kty: "RSA" , n: _toUrlBase64(k.n)
, n: _toUrlBase64(k.n) , e: _toUrlBase64(k.e)
, e: _toUrlBase64(k.e) , d: _toUrlBase64(k.d)
, d: _toUrlBase64(k.d) , p: _toUrlBase64(k.p)
, p: _toUrlBase64(k.p) , q: _toUrlBase64(k.q)
, q: _toUrlBase64(k.q) , dp: _toUrlBase64(k.dP)
, dp: _toUrlBase64(k.dP) , dq: _toUrlBase64(k.dQ)
, dq: _toUrlBase64(k.dQ) , qi: _toUrlBase64(k.qInv)
, qi: _toUrlBase64(k.qInv) };
return {
publicKeyPem: Rasha.exportSync({ jwk: jwk, public: true })
, privateKeyPem: Rasha.exportSync({ jwk: jwk })
, privateKeyJwk: jwk
, publicKeyJwk: {
kty: jwk.kty
, n: jwk.n
, e: jwk.e
} }
}); };
}; };
function _toUrlBase64(fbn) { function _toUrlBase64(fbn) {
@ -37,5 +45,9 @@ function _toUrlBase64(fbn) {
} }
if (require.main === module) { if (require.main === module) {
console.log(module.exports(2048, 0x10001)); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem);
console.warn(keypair.publicKeyPem);
//console.info(keypair.privateKeyJwk);
//console.warn(keypair.publicKeyJwk);
} }

View File

@ -1,7 +1,9 @@
'use strict'; 'use strict';
var Rasha = require('rasha');
module.exports = function (bitlen, exp) { module.exports = function (bitlen, exp) {
return require('crypto').generateKeyPairSync( var keypair = require('crypto').generateKeyPairSync(
'rsa' 'rsa'
, { modulusLength: bitlen , { modulusLength: bitlen
, publicExponent: exp , publicExponent: exp
@ -14,9 +16,20 @@ module.exports = function (bitlen, exp) {
, format: 'pem' , format: 'pem'
} }
} }
).privateKey.trim(); );
var result = {
publicKeyPem: keypair.publicKey.trim()
, privateKeyPem: keypair.privateKey.trim()
};
result.publicKeyJwk = Rasha.importSync({ pem: result.publicKeyPem, public: true });
result.privateKeyJwk = Rasha.importSync({ pem: result.privateKeyPem });
return result;
}; };
if (require.main === module) { if (require.main === module) {
console.log(module.exports(2048, 0x10001)); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem);
console.warn(keypair.publicKeyPem);
//console.info(keypair.privateKeyJwk);
//console.warn(keypair.publicKeyJwk);
} }

View File

@ -1,5 +1,7 @@
'use strict'; 'use strict';
var Rasha = require('rasha');
module.exports = function (bitlen, exp) { module.exports = function (bitlen, exp) {
var ursa; var ursa;
try { try {
@ -7,9 +9,20 @@ module.exports = function (bitlen, exp) {
} catch(e) { } catch(e) {
ursa = require('ursa-optional'); ursa = require('ursa-optional');
} }
return ursa.generatePrivateKey(bitlen || 2048, exp || 65537).toPrivatePem().toString('ascii').trim(); var keypair = ursa.generatePrivateKey(bitlen, exp);
var result = {
publicKeyPem: keypair.toPublicPem().toString('ascii').trim()
, privateKeyPem: keypair.toPrivatePem().toString('ascii').trim()
};
result.publicKeyJwk = Rasha.importSync({ pem: result.publicKeyPem, public: true });
result.privateKeyJwk = Rasha.importSync({ pem: result.privateKeyPem });
return result;
}; };
if (require.main === module) { if (require.main === module) {
console.log(module.exports(2048, 0x10001)); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem);
console.warn(keypair.publicKeyPem);
//console.info(keypair.privateKeyJwk);
//console.warn(keypair.publicKeyJwk);
} }

View File

@ -3,8 +3,8 @@
var oldver = false; var oldver = false;
module.exports = function (bitlen, exp) { module.exports = function (bitlen, exp) {
bitlen = parseInt(bitlen, 10); bitlen = parseInt(bitlen, 10) || 2048;
exp = parseInt(exp, 10); exp = parseInt(exp, 10) || 65537;
try { try {
return require('./generate-privkey-node')(bitlen, exp); return require('./generate-privkey-node')(bitlen, exp);