From 8e2763ecd618c745d6c2c39c73697bb7ac347dd0 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 8 Oct 2019 13:02:43 -0600 Subject: [PATCH] make Prettier --- fixtures/account.jwk.json | 30 +++---- fixtures/account.registration.json | 22 ++--- lib/node/generate-privkey-forge.js | 82 +++++++++--------- lib/node/generate-privkey-node.js | 24 +++--- lib/node/generate-privkey-ursa.js | 29 ++++--- lib/node/generate-privkey.js | 128 +++++++++++++++++------------ 6 files changed, 173 insertions(+), 142 deletions(-) diff --git a/fixtures/account.jwk.json b/fixtures/account.jwk.json index ba37dee..507063c 100644 --- a/fixtures/account.jwk.json +++ b/fixtures/account.jwk.json @@ -1,17 +1,17 @@ { - "private": { - "kty": "EC", - "crv": "P-256", - "d": "HB1OvdHfLnIy2mYYO9cLU4BqP36CeyS8OsDf3OnYP-M", - "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", - "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", - "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" - }, - "public": { - "kty": "EC", - "crv": "P-256", - "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", - "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", - "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" - } + "private": { + "kty": "EC", + "crv": "P-256", + "d": "HB1OvdHfLnIy2mYYO9cLU4BqP36CeyS8OsDf3OnYP-M", + "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", + "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", + "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" + }, + "public": { + "kty": "EC", + "crv": "P-256", + "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", + "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", + "kid": "UuuZa_56jCM2douUq1riGyRphPtRvCPkxtkg0bP-pNs" + } } diff --git a/fixtures/account.registration.json b/fixtures/account.registration.json index e1da38e..7736000 100644 --- a/fixtures/account.registration.json +++ b/fixtures/account.registration.json @@ -1,13 +1,13 @@ { - "key": { - "kty": "EC", - "crv": "P-256", - "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", - "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", - "kid": "https://acme-staging-v02.api.letsencrypt.org/acme/acct/11265299" - }, - "contact": [], - "initialIp": "66.219.236.169", - "createdAt": "2019-10-04T22:54:28.569489074Z", - "status": "valid" + "key": { + "kty": "EC", + "crv": "P-256", + "x": "uLh0RLpAmKyyHCf2zOaF18IIuBiJEiZ8Mu3xPZ7ZxN8", + "y": "vVl_cCXK0_GlCaCT5Yg750LUd8eRU6tySEdQFLM62NQ", + "kid": "https://acme-staging-v02.api.letsencrypt.org/acme/acct/11265299" + }, + "contact": [], + "initialIp": "66.219.236.169", + "createdAt": "2019-10-04T22:54:28.569489074Z", + "status": "valid" } diff --git a/lib/node/generate-privkey-forge.js b/lib/node/generate-privkey-forge.js index 55bd2e2..1ae7b26 100644 --- a/lib/node/generate-privkey-forge.js +++ b/lib/node/generate-privkey-forge.js @@ -4,50 +4,52 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 'use strict'; -module.exports = function (bitlen, exp) { - var k = require('node-forge').pki.rsa - .generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey; - 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 { - private: jwk - , public: { - kty: jwk.kty - , n: jwk.n - , e: jwk.e - } - }; +module.exports = function(bitlen, exp) { + var k = require('node-forge').pki.rsa.generateKeyPair({ + bits: bitlen || 2048, + e: exp || 0x10001 + }).privateKey; + 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 { + private: jwk, + public: { + kty: jwk.kty, + n: jwk.n, + e: jwk.e + } + }; }; function _toUrlBase64(fbn) { - var hex = fbn.toRadix(16); - if (hex.length % 2) { - // Invalid hex string - hex = '0' + hex; - } - while ('00' === hex.slice(0, 2)) { - hex = hex.slice(2); - } - return Buffer.from(hex, 'hex').toString('base64') - .replace(/\+/g, "-") - .replace(/\//g, "_") - .replace(/=/g,"") - ; + var hex = fbn.toRadix(16); + if (hex.length % 2) { + // Invalid hex string + hex = '0' + hex; + } + while ('00' === hex.slice(0, 2)) { + hex = hex.slice(2); + } + return Buffer.from(hex, 'hex') + .toString('base64') + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=/g, ''); } if (require.main === module) { - var keypair = module.exports(2048, 0x10001); - console.info(keypair.private); - console.warn(keypair.public); - //console.info(keypair.privateKeyJwk); - //console.warn(keypair.publicKeyJwk); + var keypair = module.exports(2048, 0x10001); + console.info(keypair.private); + console.warn(keypair.public); + //console.info(keypair.privateKeyJwk); + //console.warn(keypair.publicKeyJwk); } diff --git a/lib/node/generate-privkey-node.js b/lib/node/generate-privkey-node.js index c730d23..99271fd 100644 --- a/lib/node/generate-privkey-node.js +++ b/lib/node/generate-privkey-node.js @@ -4,20 +4,18 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 'use strict'; -module.exports = function (bitlen, exp) { - var keypair = require('crypto').generateKeyPairSync( - 'rsa' - , { modulusLength: bitlen - , publicExponent: exp - , privateKeyEncoding: { type: 'pkcs1', format: 'pem' } - , publicKeyEncoding: { type: 'pkcs1', format: 'pem' } - } - ); - var result = { privateKeyPem: keypair.privateKey.trim() }; - return result; +module.exports = function(bitlen, exp) { + var keypair = require('crypto').generateKeyPairSync('rsa', { + modulusLength: bitlen, + publicExponent: exp, + privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, + publicKeyEncoding: { type: 'pkcs1', format: 'pem' } + }); + var result = { privateKeyPem: keypair.privateKey.trim() }; + return result; }; if (require.main === module) { - var keypair = module.exports(2048, 0x10001); - console.info(keypair.privateKeyPem); + var keypair = module.exports(2048, 0x10001); + console.info(keypair.privateKeyPem); } diff --git a/lib/node/generate-privkey-ursa.js b/lib/node/generate-privkey-ursa.js index 5a60a04..5c9694e 100644 --- a/lib/node/generate-privkey-ursa.js +++ b/lib/node/generate-privkey-ursa.js @@ -4,19 +4,24 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 'use strict'; -module.exports = function (bitlen, exp) { - var ursa; - try { - ursa = require('ursa'); - } catch(e) { - ursa = require('ursa-optional'); - } - var keypair = ursa.generatePrivateKey(bitlen, exp); - var result = { privateKeyPem: keypair.toPrivatePem().toString('ascii').trim() }; - return result; +module.exports = function(bitlen, exp) { + var ursa; + try { + ursa = require('ursa'); + } catch (e) { + ursa = require('ursa-optional'); + } + var keypair = ursa.generatePrivateKey(bitlen, exp); + var result = { + privateKeyPem: keypair + .toPrivatePem() + .toString('ascii') + .trim() + }; + return result; }; if (require.main === module) { - var keypair = module.exports(2048, 0x10001); - console.info(keypair.privateKeyPem); + var keypair = module.exports(2048, 0x10001); + console.info(keypair.privateKeyPem); } diff --git a/lib/node/generate-privkey.js b/lib/node/generate-privkey.js index 772d2df..d82fa8c 100644 --- a/lib/node/generate-privkey.js +++ b/lib/node/generate-privkey.js @@ -6,59 +6,85 @@ var oldver = false; -module.exports = function (bitlen, exp) { - bitlen = parseInt(bitlen, 10) || 2048; - exp = parseInt(exp, 10) || 65537; +module.exports = function(bitlen, exp) { + bitlen = parseInt(bitlen, 10) || 2048; + exp = parseInt(exp, 10) || 65537; - try { - return require('./generate-privkey-node.js')(bitlen, exp); - } catch(e) { - if (!/generateKeyPairSync is not a function/.test(e.message)) { - throw e; - } - try { - return require('./generate-privkey-ursa.js')(bitlen, exp); - } catch(e) { - if (e.code !== 'MODULE_NOT_FOUND') { - console.error("[rsa-compat] Unexpected error when using 'ursa':"); - console.error(e); - } - if (!oldver) { - oldver = true; - console.warn("[WARN] rsa-compat: Your version of node does not have crypto.generateKeyPair()"); - console.warn("[WARN] rsa-compat: Please update to node >= v10.12 or 'npm install --save ursa node-forge'"); - console.warn("[WARN] rsa-compat: Using node-forge as a fallback may be unacceptably slow."); - if (/arm|mips/i.test(require('os').arch)) { - console.warn("================================================================"); - console.warn(" WARNING"); - console.warn("================================================================"); - console.warn(""); - console.warn("WARNING: You are generating an RSA key using pure JavaScript on"); - console.warn(" a VERY SLOW cpu. This could take DOZENS of minutes!"); - console.warn(""); - 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"); - } - } - } + try { + return require('./generate-privkey-node.js')(bitlen, exp); + } catch (e) { + if (!/generateKeyPairSync is not a function/.test(e.message)) { + throw e; + } + try { + return require('./generate-privkey-ursa.js')(bitlen, exp); + } catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + console.error( + "[rsa-compat] Unexpected error when using 'ursa':" + ); + console.error(e); + } + if (!oldver) { + oldver = true; + console.warn( + '[WARN] rsa-compat: Your version of node does not have crypto.generateKeyPair()' + ); + console.warn( + "[WARN] rsa-compat: Please update to node >= v10.12 or 'npm install --save ursa node-forge'" + ); + console.warn( + '[WARN] rsa-compat: Using node-forge as a fallback may be unacceptably slow.' + ); + if (/arm|mips/i.test(require('os').arch)) { + console.warn( + '================================================================' + ); + console.warn(' WARNING'); + console.warn( + '================================================================' + ); + console.warn(''); + console.warn( + 'WARNING: You are generating an RSA key using pure JavaScript on' + ); + console.warn( + ' a VERY SLOW cpu. This could take DOZENS of minutes!' + ); + console.warn(''); + 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) { - var keypair = module.exports(2048, 0x10001); - console.info(keypair.privateKeyPem); + var keypair = module.exports(2048, 0x10001); + console.info(keypair.privateKeyPem); }