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) {
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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);