CyberChef/src/core/operations/PGPEncrypt.mjs

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-05-15 11:15:31 +02:00
/**
* @author tlwr [toby@toby.codes]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
import Operation from "../Operation";
2018-05-15 17:04:57 +02:00
import kbpgp from "kbpgp";
2018-05-15 11:15:31 +02:00
import { ASP } from "../lib/PGP";
import OperationError from "../errors/OperationError";
2018-05-15 17:04:57 +02:00
import promisifyDefault from "es6-promisify";
const promisify = promisifyDefault.promisify;
2018-05-15 11:15:31 +02:00
/**
* PGP Encrypt operation
*/
class PGPEncrypt extends Operation {
/**
* PGPEncrypt constructor
*/
constructor() {
super();
this.name = "PGP Encrypt";
this.module = "PGP";
this.description = "Input: the message you want to encrypt.\n<br><br>\nArguments: the ASCII-armoured PGP public key of the recipient.\n<br><br>\nPretty Good Privacy is an encryption standard (OpenPGP) used for encrypting, decrypting, and signing messages.\n<br><br>\nThis function uses the Keybase implementation of PGP.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Public key of recipient",
"type": "text",
"value": ""
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*
* @throws {OperationError} if failed private key import or failed encryption
2018-05-15 11:15:31 +02:00
*/
async run(input, args) {
const plaintextMessage = input,
plainPubKey = args[0];
let key,
encryptedMessage;
if (!plainPubKey) throw new OperationError("Enter the public key of the recipient.");
2018-05-15 11:15:31 +02:00
try {
key = await promisify(kbpgp.KeyManager.import_from_armored_pgp)({
armored: plainPubKey,
});
} catch (err) {
throw new OperationError(`Could not import public key: ${err}`);
2018-05-15 11:15:31 +02:00
}
try {
encryptedMessage = await promisify(kbpgp.box)({
"msg": plaintextMessage,
"encrypt_for": key,
"asp": ASP
});
} catch (err) {
throw new OperationError(`Couldn't encrypt message with provided public key: ${err}`);
2018-05-15 11:15:31 +02:00
}
return encryptedMessage.toString();
}
}
export default PGPEncrypt;