mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-02 14:11:02 +01:00
Comment and add error handling to generate and sign
This commit is contained in:
parent
e0f000b913
commit
2233b9a094
@ -56,7 +56,7 @@ class GenerateRSAKeyPair extends Operation {
|
|||||||
const [keyLength, outputFormat] = args;
|
const [keyLength, outputFormat] = args;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
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);
|
if (err) return reject(err);
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import forge from "node-forge/dist/forge.min.js";
|
import forge from "node-forge/dist/forge.min.js";
|
||||||
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
||||||
|
|
||||||
@ -52,13 +53,21 @@ class RSASign extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const [key, password, mdAlgo] = args;
|
const [key, password, mdAlgo] = args;
|
||||||
|
if (key.replace("-----BEGIN RSA PRIVATE KEY-----", "").length === 0) {
|
||||||
const privateKey = forge.pki.decryptRsaPrivateKey(key, password);
|
throw new OperationError("Please enter a private key.");
|
||||||
const md = MD_ALGORITHMS[mdAlgo].create();
|
}
|
||||||
md.update(input, "utf8");
|
try {
|
||||||
const signature = privateKey.sign(md);
|
const privateKey = forge.pki.decryptRsaPrivateKey(key, password);
|
||||||
|
// Generate message hash
|
||||||
return signature.split("").map(char => char.charCodeAt());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user