GCHQDeveloper581 772f7866bf
Fix pgp tests (#2461)
Co-authored-by: C85297 <95289555+C85297@users.noreply.github.com> (fix for #2267)
2026-05-28 09:17:43 +01:00

84 lines
2.5 KiB
JavaScript

/**
* @author GCHQDeveloper581
* @copyright Crown Copyright 2026
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import kbpgp from "kbpgp";
import { ASP, importPrivateKey } from "../lib/PGP.mjs";
import OperationError from "../errors/OperationError.mjs";
import * as es6promisify from "es6-promisify";
const promisify = es6promisify.default ? es6promisify.default.promisify : es6promisify.promisify;
/**
* PGP Sign operation
*/
class PGPSign extends Operation {
/**
* PGPSign constructor
*/
constructor() {
super();
this.name = "PGP Sign";
this.module = "PGP";
this.description = [
"Input: the message you want to sign",
"<br><br>",
"Arguments: the ASCII-armoured PGP private key of the sender.",
"<br><br>",
"Pretty Good Privacy is an encryption standard (OpenPGP) used for encrypting, decrypting, and signing messages.",
"<br><br>",
"This function uses the Keybase implementation of PGP.",
].join("\n");
this.infoURL = "https://wikipedia.org/wiki/Pretty_Good_Privacy"; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Private key of signer",
"type": "text",
"value": ""
},
{
"name": "Private key passphrase (optional)",
"type": "string",
"value": ""
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*
* @throws {OperationError} if failed private key import or failed encryption
*/
async run(input, args) {
const message = input,
[privateKey, passphrase] = args;
let signedMessage;
if (!privateKey) throw new OperationError("Enter the private key of the signer.");
const privKey = await importPrivateKey(privateKey, passphrase);
try {
signedMessage = await promisify(kbpgp.box)({
"msg": message,
"sign_with": privKey,
"asp": ASP
});
} catch (err) {
throw new OperationError(`Couldn't sign message: ${err}`);
}
return signedMessage;
}
}
export default PGPSign;