From 31e758ca45667125feb52707762ead91d7969a08 Mon Sep 17 00:00:00 2001 From: Matt C Date: Fri, 31 Aug 2018 11:25:05 +0100 Subject: [PATCH] Attempt to make RSA key generation functional --- package-lock.json | 6 ++++++ package.json | 1 + src/core/operations/GenerateRSAKeyPair.mjs | 19 ++++++++++++------- src/core/operations/RSASign.mjs | 4 ++-- webpack.config.js | 4 ++++ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index d9814093..e7ce7f62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8573,6 +8573,12 @@ } } }, + "raw-loader": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/raw-loader/-/raw-loader-0.5.1.tgz", + "integrity": "sha1-DD0L6u2KAclm2Xh793goElKpeao=", + "dev": true + }, "rc": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", diff --git a/package.json b/package.json index 5ff508f6..6805c522 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "postcss-import": "^12.0.0", "postcss-loader": "^2.1.6", "prompt": "^1.0.0", + "raw-loader": "^0.5.1", "sass-loader": "^7.1.0", "sitemap": "^1.13.0", "style-loader": "^0.21.0", diff --git a/src/core/operations/GenerateRSAKeyPair.mjs b/src/core/operations/GenerateRSAKeyPair.mjs index 9d7a88d8..64ff29d2 100644 --- a/src/core/operations/GenerateRSAKeyPair.mjs +++ b/src/core/operations/GenerateRSAKeyPair.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation"; import forge from "node-forge/dist/forge.min.js"; +import PrimeWorker from "node-forge/dist/prime.worker.min.js"; /** * Generate RSA Key Pair operation @@ -52,15 +53,19 @@ class GenerateRSAKeyPair extends Operation { * @returns {string} */ async run(input, args) { - const [keyLength, outputFormat] = args + const [keyLength, outputFormat] = args; + let workerScript; return new Promise((resolve, reject) => { - forge.pki.rsa.generateKeyPair({ bits: Number(keyLength), workers: 2 }, (err, keypair) => { - if (err) return reject(err) + if (ENVIRONMENT_IS_WORKER || window) { + workerScript = ENVIRONMENT_IS_WORKER() ? self.URL.createObjectURL(new Blob([PrimeWorker])) : window.URL.createObjectURL(new Blob([PrimeWorker])); + } + forge.pki.rsa.generateKeyPair({ bits: Number(keyLength), workers: 2, workerScript}, (err, keypair) => { + if (err) return reject(err); let result; - switch(outputFormat) { + switch (outputFormat) { case "PEM": result = forge.pki.publicKeyToPem(keypair.publicKey) + "\n" + forge.pki.privateKeyToPem(keypair.privateKey); break; @@ -70,11 +75,11 @@ class GenerateRSAKeyPair extends Operation { case "DER": result = forge.asn1.toDer(forge.pki.privateKeyToAsn1(keypair.privateKey)).getBytes(); break; - }; + } resolve(result); - }) - }) + }); + }); } } diff --git a/src/core/operations/RSASign.mjs b/src/core/operations/RSASign.mjs index fb3e4bbd..c0b4ccdf 100644 --- a/src/core/operations/RSASign.mjs +++ b/src/core/operations/RSASign.mjs @@ -49,10 +49,10 @@ class RSASign extends Operation { const privateKey = forge.pki.decryptRsaPrivateKey(key, password); const md = forge.md.sha1.create(); - md.update(input, 'utf8'); + md.update(input, "utf8"); const signature = privateKey.sign(md); - return signature.split('').map(char => char.charCodeAt()); + return signature.split("").map(char => char.charCodeAt()); } } diff --git a/webpack.config.js b/webpack.config.js index 89a36f69..1a4f5a2d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -61,6 +61,10 @@ module.exports = { test: /forge.min.js$/, loader: "imports-loader?jQuery=>null" }, + { + test: /prime.worker.min.js$/, + use: "raw-loader" + }, { test: /bootstrap-material-design/, loader: "imports-loader?Popper=popper.js/dist/umd/popper.js"