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

@ -4,26 +4,28 @@
* 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,
e: exp || 0x10001
}).privateKey;
var jwk = { var 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 { return {
private: jwk private: jwk,
, public: { public: {
kty: jwk.kty kty: jwk.kty,
, n: jwk.n n: jwk.n,
, e: jwk.e e: jwk.e
} }
}; };
}; };
@ -37,11 +39,11 @@ function _toUrlBase64(fbn) {
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) {

View File

@ -4,15 +4,13 @@
* 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() }; var result = { privateKeyPem: keypair.privateKey.trim() };
return result; return result;
}; };

View File

@ -4,15 +4,20 @@
* 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 = {
privateKeyPem: keypair
.toPrivatePem()
.toString('ascii')
.trim()
};
return result; return result;
}; };

View File

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