From 70bfcd04bfa7f077f69e79b489cf323a62846cd7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 15 Dec 2018 23:16:27 -0700 Subject: [PATCH] export both Private and Public, and PEMs and JWKs --- lib/generate-privkey-forge.js | 38 +++++++++++++++++++++++------------ lib/generate-privkey-node.js | 19 +++++++++++++++--- lib/generate-privkey-ursa.js | 17 ++++++++++++++-- lib/generate-privkey.js | 4 ++-- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/lib/generate-privkey-forge.js b/lib/generate-privkey-forge.js index 686466f..890fbb6 100644 --- a/lib/generate-privkey-forge.js +++ b/lib/generate-privkey-forge.js @@ -5,19 +5,27 @@ var Rasha = require('rasha'); module.exports = function (bitlen, exp) { var k = require('node-forge').pki.rsa .generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey; - return Rasha.exportSync({ - jwk: { - kty: "RSA" - , n: _toUrlBase64(k.n) - , e: _toUrlBase64(k.e) - , d: _toUrlBase64(k.d) - , p: _toUrlBase64(k.p) - , q: _toUrlBase64(k.q) - , dp: _toUrlBase64(k.dP) - , dq: _toUrlBase64(k.dQ) - , qi: _toUrlBase64(k.qInv) + var jwk = { + kty: "RSA" + , n: _toUrlBase64(k.n) + , e: _toUrlBase64(k.e) + , d: _toUrlBase64(k.d) + , p: _toUrlBase64(k.p) + , q: _toUrlBase64(k.q) + , dp: _toUrlBase64(k.dP) + , dq: _toUrlBase64(k.dQ) + , 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) { @@ -37,5 +45,9 @@ function _toUrlBase64(fbn) { } 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); } diff --git a/lib/generate-privkey-node.js b/lib/generate-privkey-node.js index 0a2ff88..1502e38 100644 --- a/lib/generate-privkey-node.js +++ b/lib/generate-privkey-node.js @@ -1,7 +1,9 @@ 'use strict'; +var Rasha = require('rasha'); + module.exports = function (bitlen, exp) { - return require('crypto').generateKeyPairSync( + var keypair = require('crypto').generateKeyPairSync( 'rsa' , { modulusLength: bitlen , publicExponent: exp @@ -14,9 +16,20 @@ module.exports = function (bitlen, exp) { , 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) { - 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); } diff --git a/lib/generate-privkey-ursa.js b/lib/generate-privkey-ursa.js index feac7a5..2ad7979 100644 --- a/lib/generate-privkey-ursa.js +++ b/lib/generate-privkey-ursa.js @@ -1,5 +1,7 @@ 'use strict'; +var Rasha = require('rasha'); + module.exports = function (bitlen, exp) { var ursa; try { @@ -7,9 +9,20 @@ module.exports = function (bitlen, exp) { } catch(e) { 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) { - 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); } diff --git a/lib/generate-privkey.js b/lib/generate-privkey.js index 1d580d6..287bc1b 100644 --- a/lib/generate-privkey.js +++ b/lib/generate-privkey.js @@ -3,8 +3,8 @@ var oldver = false; module.exports = function (bitlen, exp) { - bitlen = parseInt(bitlen, 10); - exp = parseInt(exp, 10); + bitlen = parseInt(bitlen, 10) || 2048; + exp = parseInt(exp, 10) || 65537; try { return require('./generate-privkey-node')(bitlen, exp);