PGPEncryptAndSign.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @author tlwr [toby@toby.codes]
  3. * @copyright Crown Copyright 2017
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import kbpgp from "kbpgp";
  8. import { ASP, importPrivateKey, importPublicKey } from "../lib/PGP";
  9. import OperationError from "../errors/OperationError";
  10. import promisifyDefault from "es6-promisify";
  11. const promisify = promisifyDefault.promisify;
  12. /**
  13. * PGP Encrypt and Sign operation
  14. */
  15. class PGPEncryptAndSign extends Operation {
  16. /**
  17. * PGPEncryptAndSign constructor
  18. */
  19. constructor() {
  20. super();
  21. this.name = "PGP Encrypt and Sign";
  22. this.module = "PGP";
  23. this.description = "Input: the cleartext you want to sign.\n<br><br>\nArguments: the ASCII-armoured private key of the signer (plus the private key password if necessary)\nand the ASCII-armoured PGP public key of the recipient.\n<br><br>\nThis operation uses PGP to produce an encrypted digital signature.\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.";
  24. this.inputType = "string";
  25. this.outputType = "string";
  26. this.args = [
  27. {
  28. "name": "Private key of signer",
  29. "type": "text",
  30. "value": ""
  31. },
  32. {
  33. "name": "Private key passphrase",
  34. "type": "string",
  35. "value": ""
  36. },
  37. {
  38. "name": "Public key of recipient",
  39. "type": "text",
  40. "value": ""
  41. }
  42. ];
  43. }
  44. /**
  45. * @param {string} input
  46. * @param {Object[]} args
  47. * @returns {string}
  48. *
  49. * @throws {OperationError} if failure to sign message
  50. */
  51. async run(input, args) {
  52. const message = input,
  53. privateKey = args[0],
  54. passphrase = args[1],
  55. publicKey = args[2];
  56. let signedMessage;
  57. if (!privateKey) throw new OperationError("Enter the private key of the signer.");
  58. if (!publicKey) throw new OperationError("Enter the public key of the recipient.");
  59. const privKey = await importPrivateKey(privateKey, passphrase);
  60. const pubKey = await importPublicKey(publicKey);
  61. try {
  62. signedMessage = await promisify(kbpgp.box)({
  63. "msg": message,
  64. "encrypt_for": pubKey,
  65. "sign_with": privKey,
  66. "asp": ASP
  67. });
  68. } catch (err) {
  69. throw new OperationError(`Couldn't sign message: ${err}`);
  70. }
  71. return signedMessage;
  72. }
  73. }
  74. export default PGPEncryptAndSign;