make Prettier

This commit is contained in:
AJ ONeal 2019-10-08 13:02:43 -06:00
parent 76f98f7c7e
commit 8e2763ecd6
6 changed files with 173 additions and 142 deletions

View File

@ -1,17 +1,17 @@
{ {
"private": { "private": {
"kty": "EC", "kty": "EC",
"crv": "P-256", "crv": "P-256",
"d": "HB1OvdHfLnIy2mYYO9cLU4BqP36CeyS8OsDf3OnYP-M", "d": "HB1OvdHfLnIy2mYYO9cLU4BqP36CeyS8OsDf3OnYP-M",
"x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8",
"y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ",
"kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs"
}, },
"public": { "public": {
"kty": "EC", "kty": "EC",
"crv": "P-256", "crv": "P-256",
"x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8",
"y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ",
"kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs"
} }
} }

View File

@ -1,13 +1,13 @@
{ {
"key": { "key": {
"kty": "EC", "kty": "EC",
"crv": "P-256", "crv": "P-256",
"x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8",
"y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ",
"kid": "https://acme-staging-v02.api.letsencrypt.org/acme/acct/11265299" "kid": "https://acme-staging-v02.api.letsencrypt.org/acme/acct/11265299"
}, },
"contact": [], "contact": [],
"initialIp": "66.219.236.169", "initialIp": "66.219.236.169",
"createdAt": "2019-10-04T22:54:28.569489074Z", "createdAt": "2019-10-04T22:54:28.569489074Z",
"status": "valid" "status": "valid"
} }

View File

@ -4,50 +4,52 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'; 'use strict';
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({
.generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey; bits: bitlen || 2048,
var jwk = { e: exp || 0x10001
kty: "RSA" }).privateKey;
, n: _toUrlBase64(k.n) var jwk = {
, e: _toUrlBase64(k.e) kty: 'RSA',
, d: _toUrlBase64(k.d) n: _toUrlBase64(k.n),
, p: _toUrlBase64(k.p) e: _toUrlBase64(k.e),
, q: _toUrlBase64(k.q) d: _toUrlBase64(k.d),
, dp: _toUrlBase64(k.dP) p: _toUrlBase64(k.p),
, dq: _toUrlBase64(k.dQ) q: _toUrlBase64(k.q),
, qi: _toUrlBase64(k.qInv) dp: _toUrlBase64(k.dP),
}; dq: _toUrlBase64(k.dQ),
return { qi: _toUrlBase64(k.qInv)
private: jwk };
, public: { return {
kty: jwk.kty private: jwk,
, n: jwk.n public: {
, e: jwk.e kty: jwk.kty,
} n: jwk.n,
}; e: jwk.e
}
};
}; };
function _toUrlBase64(fbn) { function _toUrlBase64(fbn) {
var hex = fbn.toRadix(16); var hex = fbn.toRadix(16);
if (hex.length % 2) { if (hex.length % 2) {
// Invalid hex string // Invalid hex string
hex = '0' + hex; hex = '0' + hex;
} }
while ('00' === hex.slice(0, 2)) { while ('00' === hex.slice(0, 2)) {
hex = hex.slice(2); hex = hex.slice(2);
} }
return Buffer.from(hex, 'hex').toString('base64') return Buffer.from(hex, 'hex')
.replace(/\+/g, "-") .toString('base64')
.replace(/\//g, "_") .replace(/\+/g, '-')
.replace(/=/g,"") .replace(/\//g, '_')
; .replace(/=/g, '');
} }
if (require.main === module) { if (require.main === module) {
var keypair = module.exports(2048, 0x10001); var keypair = module.exports(2048, 0x10001);
console.info(keypair.private); console.info(keypair.private);
console.warn(keypair.public); console.warn(keypair.public);
//console.info(keypair.privateKeyJwk); //console.info(keypair.privateKeyJwk);
//console.warn(keypair.publicKeyJwk); //console.warn(keypair.publicKeyJwk);
} }

View File

@ -4,20 +4,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'; 'use strict';
module.exports = function (bitlen, exp) { module.exports = function(bitlen, exp) {
var keypair = require('crypto').generateKeyPairSync( var keypair = require('crypto').generateKeyPairSync('rsa', {
'rsa' modulusLength: bitlen,
, { modulusLength: bitlen publicExponent: exp,
, publicExponent: exp privateKeyEncoding: { type: 'pkcs1', format: 'pem' },
, privateKeyEncoding: { type: 'pkcs1', format: 'pem' } publicKeyEncoding: { type: 'pkcs1', format: 'pem' }
, publicKeyEncoding: { type: 'pkcs1', format: 'pem' } });
} var result = { privateKeyPem: keypair.privateKey.trim() };
); return result;
var result = { privateKeyPem: keypair.privateKey.trim() };
return result;
}; };
if (require.main === module) { if (require.main === module) {
var keypair = module.exports(2048, 0x10001); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem); console.info(keypair.privateKeyPem);
} }

View File

@ -4,19 +4,24 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'; 'use strict';
module.exports = function (bitlen, exp) { module.exports = function(bitlen, exp) {
var ursa; var ursa;
try { try {
ursa = require('ursa'); ursa = require('ursa');
} catch(e) { } catch (e) {
ursa = require('ursa-optional'); ursa = require('ursa-optional');
} }
var keypair = ursa.generatePrivateKey(bitlen, exp); var keypair = ursa.generatePrivateKey(bitlen, exp);
var result = { privateKeyPem: keypair.toPrivatePem().toString('ascii').trim() }; var result = {
return result; privateKeyPem: keypair
.toPrivatePem()
.toString('ascii')
.trim()
};
return result;
}; };
if (require.main === module) { if (require.main === module) {
var keypair = module.exports(2048, 0x10001); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem); console.info(keypair.privateKeyPem);
} }

View File

@ -6,59 +6,85 @@
var oldver = false; var oldver = false;
module.exports = function (bitlen, exp) { module.exports = function(bitlen, exp) {
bitlen = parseInt(bitlen, 10) || 2048; bitlen = parseInt(bitlen, 10) || 2048;
exp = parseInt(exp, 10) || 65537; exp = parseInt(exp, 10) || 65537;
try { try {
return require('./generate-privkey-node.js')(bitlen, exp); return require('./generate-privkey-node.js')(bitlen, exp);
} catch(e) { } catch (e) {
if (!/generateKeyPairSync is not a function/.test(e.message)) { if (!/generateKeyPairSync is not a function/.test(e.message)) {
throw e; throw e;
} }
try { try {
return require('./generate-privkey-ursa.js')(bitlen, exp); return require('./generate-privkey-ursa.js')(bitlen, exp);
} catch(e) { } catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') { if (e.code !== 'MODULE_NOT_FOUND') {
console.error("[rsa-compat] Unexpected error when using 'ursa':"); console.error(
console.error(e); "[rsa-compat] Unexpected error when using 'ursa':"
} );
if (!oldver) { console.error(e);
oldver = true; }
console.warn("[WARN] rsa-compat: Your version of node does not have crypto.generateKeyPair()"); if (!oldver) {
console.warn("[WARN] rsa-compat: Please update to node >= v10.12 or 'npm install --save ursa node-forge'"); oldver = true;
console.warn("[WARN] rsa-compat: Using node-forge as a fallback may be unacceptably slow."); console.warn(
if (/arm|mips/i.test(require('os').arch)) { '[WARN] rsa-compat: Your version of node does not have crypto.generateKeyPair()'
console.warn("================================================================"); );
console.warn(" WARNING"); console.warn(
console.warn("================================================================"); "[WARN] rsa-compat: Please update to node >= v10.12 or 'npm install --save ursa node-forge'"
console.warn(""); );
console.warn("WARNING: You are generating an RSA key using pure JavaScript on"); console.warn(
console.warn(" a VERY SLOW cpu. This could take DOZENS of minutes!"); '[WARN] rsa-compat: Using node-forge as a fallback may be unacceptably slow.'
console.warn(""); );
console.warn(" We recommend installing node >= v10.12, or 'gcc' and 'ursa'"); if (/arm|mips/i.test(require('os').arch)) {
console.warn(""); console.warn(
console.warn("EXAMPLE:"); '================================================================'
console.warn(""); );
console.warn(" sudo apt-get install build-essential && npm install ursa"); console.warn(' WARNING');
console.warn(""); console.warn(
console.warn("================================================================"); '================================================================'
} );
} console.warn('');
try { console.warn(
return require('./generate-privkey-forge.js')(bitlen, exp); 'WARNING: You are generating an RSA key using pure JavaScript on'
} catch(e) { );
if (e.code !== 'MODULE_NOT_FOUND') { console.warn(
throw e; ' a VERY SLOW cpu. This could take DOZENS of minutes!'
} );
console.error("[ERROR] rsa-compat: could not generate a private key."); console.warn('');
console.error("None of crypto.generateKeyPair, ursa, nor node-forge are present"); console.warn(
} " We recommend installing node >= v10.12, or 'gcc' and 'ursa'"
} );
} console.warn('');
console.warn('EXAMPLE:');
console.warn('');
console.warn(
' sudo apt-get install build-essential && npm install ursa'
);
console.warn('');
console.warn(
'================================================================'
);
}
}
try {
return require('./generate-privkey-forge.js')(bitlen, exp);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
console.error(
'[ERROR] rsa-compat: could not generate a private key.'
);
console.error(
'None of crypto.generateKeyPair, ursa, nor node-forge are present'
);
}
}
}
}; };
if (require.main === module) { if (require.main === module) {
var keypair = module.exports(2048, 0x10001); var keypair = module.exports(2048, 0x10001);
console.info(keypair.privateKeyPem); console.info(keypair.privateKeyPem);
} }