From 8db94be8acbd3c0a40a92e14f0696f6ca1af957d Mon Sep 17 00:00:00 2001 From: toby Date: Wed, 15 Mar 2017 12:06:32 -0400 Subject: [PATCH] Add operation "Detach PGP Cleartext" This operation is like "Remove PGP ASCII Armor" but for detached signatures. It outputs files as HTML similar to "Sign PGP Detached". --- src/js/config/OperationConfig.js | 8 +++++ src/js/operations/PGP.js | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/js/config/OperationConfig.js b/src/js/config/OperationConfig.js index 8452dc20..91ab8629 100755 --- a/src/js/config/OperationConfig.js +++ b/src/js/config/OperationConfig.js @@ -3335,6 +3335,14 @@ var OperationConfig = { }, ] }, + "Detach PGP Cleartext": { + description: "", + run: PGP.runDetachClearsig, + inputType: "string", + outputType: "HTML", + args: [ + ], + }, "Add PGP ASCII Armor": { description: "", run: PGP.runAddArmor, diff --git a/src/js/operations/PGP.js b/src/js/operations/PGP.js index 624ecd02..f141d1ab 100755 --- a/src/js/operations/PGP.js +++ b/src/js/operations/PGP.js @@ -511,6 +511,62 @@ var PGP = { }, + /** + * Turns a PGP clearsigned message into a detached signature. + * + * @param {string} input + * @param {Object[]} args + * @returns {HTML} + */ + runDetachClearsig: function (input, args) { + return new Promise(function(resolve, reject) { + try { + var message = openpgp.cleartext.readArmored(input); + } catch (err) { + return reject("Could not read input message: " + err); + } + + var cleartext = message.getText(); + var clearbytes = openpgp.util.str2Uint8Array(cleartext); + + var signature = message.packets.filterByTag(openpgp.enums.packet.signature); + var rawSignatureBytes = signature.write(); + + var armoredMessage = openpgp.armor.encode( + openpgp.enums.armor.message, + rawSignatureBytes + ); + armoredMessage = armoredMessage.replace( + "-----BEGIN PGP MESSAGE-----\r\n", + "-----BEGIN PGP SIGNATURE-----\r\n" + ); + armoredMessage = armoredMessage.replace( + "-----END PGP MESSAGE-----\r\n", + "-----END PGP SIGNATURE-----\r\n" + ); + + var files = [{ + fileName: "msg", + size: cleartext.length, + contents: cleartext, + bytes: clearbytes, + }, { + fileName: "msg.asc", + size: armoredMessage.length, + contents: armoredMessage, + bytes: openpgp.util.str2Uint8Array(armoredMessage), + }, { + fileName: "msg.sig", + size: rawSignatureBytes.length, + contents: openpgp.util.Uint8Array2str(rawSignatureBytes), + bytes: rawSignatureBytes, + }]; + + resolve(Utils.displayFilesAsHTML(files)); + }); + }, + + /** * Turns raw PGP bytes into an ASCII armored string. *