Comment and add error handling to generate and sign

This commit is contained in:
Matt 2020-04-06 15:24:06 +01:00
parent e0f000b913
commit 2233b9a094
No known key found for this signature in database
GPG Key ID: 2DD462FE98BF38C2
2 changed files with 17 additions and 8 deletions

View File

@ -56,7 +56,7 @@ class GenerateRSAKeyPair extends Operation {
const [keyLength, outputFormat] = args;
return new Promise((resolve, reject) => {
forge.pki.rsa.generateKeyPair({ bits: Number(keyLength), workers: -1, workerScript: "./assets/forge/prime.worker.min.js"}, (err, keypair) => {
forge.pki.rsa.generateKeyPair({ bits: Number(keyLength), workers: -1, workerScript: "assets/forge/prime.worker.min.js"}, (err, keypair) => {
if (err) return reject(err);
let result;

View File

@ -6,6 +6,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import forge from "node-forge/dist/forge.min.js";
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
@ -52,13 +53,21 @@ class RSASign extends Operation {
*/
run(input, args) {
const [key, password, mdAlgo] = args;
const privateKey = forge.pki.decryptRsaPrivateKey(key, password);
const md = MD_ALGORITHMS[mdAlgo].create();
md.update(input, "utf8");
const signature = privateKey.sign(md);
return signature.split("").map(char => char.charCodeAt());
if (key.replace("-----BEGIN RSA PRIVATE KEY-----", "").length === 0) {
throw new OperationError("Please enter a private key.");
}
try {
const privateKey = forge.pki.decryptRsaPrivateKey(key, password);
// Generate message hash
const md = MD_ALGORITHMS[mdAlgo].create();
md.update(input, "utf8");
// Convert signature UTF-16 string to byteArray
const encoder = new TextEncoder();
const signature = encoder.encode(privateKey.sign(md));
return signature;
} catch (err) {
throw new OperationError(err);
}
}
}