2018-05-15 11:15:31 +02:00
|
|
|
/**
|
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
* @copyright Crown Copyright 2017
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2018-05-15 17:04:57 +02:00
|
|
|
import kbpgp from "kbpgp";
|
2019-07-09 13:23:59 +02:00
|
|
|
import { ASP, importPublicKey } from "../lib/PGP.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2018-05-29 18:00:24 +02:00
|
|
|
import * as es6promisify from "es6-promisify";
|
|
|
|
const promisify = es6promisify.default ? es6promisify.default.promisify : es6promisify.promisify;
|
2018-05-15 19:01:04 +02:00
|
|
|
|
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";
|
2018-05-16 11:17:49 +02:00
|
|
|
this.description = [
|
|
|
|
"Input: the message you want to encrypt.",
|
|
|
|
"<br><br>",
|
|
|
|
"Arguments: the ASCII-armoured PGP public key of the recipient.",
|
|
|
|
"<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");
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Pretty_Good_Privacy";
|
2018-05-15 11:15:31 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Public key of recipient",
|
|
|
|
"type": "text",
|
|
|
|
"value": ""
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
2018-05-15 19:01:04 +02:00
|
|
|
*
|
|
|
|
* @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];
|
2018-05-29 18:00:24 +02:00
|
|
|
let encryptedMessage;
|
2018-05-15 11:15:31 +02:00
|
|
|
|
2018-05-15 19:01:04 +02:00
|
|
|
if (!plainPubKey) throw new OperationError("Enter the public key of the recipient.");
|
2018-05-15 11:15:31 +02:00
|
|
|
|
2018-05-29 18:00:24 +02:00
|
|
|
const key = await importPublicKey(plainPubKey);
|
2018-05-15 11:15:31 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
encryptedMessage = await promisify(kbpgp.box)({
|
|
|
|
"msg": plaintextMessage,
|
|
|
|
"encrypt_for": key,
|
|
|
|
"asp": ASP
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2018-05-15 19:01:04 +02:00
|
|
|
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;
|