Attempt to make RSA key generation functional

This commit is contained in:
Matt C 2018-08-31 11:25:05 +01:00
parent f81ca3ba60
commit 31e758ca45
5 changed files with 25 additions and 9 deletions

6
package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

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

View file

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

View file

@ -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"