armor based on text vs. bytes

This commit is contained in:
Sanjana Rajan 2017-07-04 16:47:23 -07:00
parent 28e5f5d3f4
commit a9bb3f1839
2 changed files with 22 additions and 9 deletions

View File

@ -37,7 +37,7 @@ import * as sigModule from './signature.js';
* @class * @class
* @classdesc Class that represents an OpenPGP cleartext signed message. * @classdesc Class that represents an OpenPGP cleartext signed message.
* See {@link http://tools.ietf.org/html/rfc4880#section-7} * See {@link http://tools.ietf.org/html/rfc4880#section-7}
* @param {String} text The cleartext of the signed message * @param {String | Uint8Array} data The cleartext of the signed message
* @param {module:signature} signature The detached signature or an empty signature if message not yet signed * @param {module:signature} signature The detached signature or an empty signature if message not yet signed
*/ */
@ -162,8 +162,12 @@ CleartextMessage.prototype.verifyDetached = function(signature, keys) {
* @return {String} cleartext of message * @return {String} cleartext of message
*/ */
CleartextMessage.prototype.getText = function() { CleartextMessage.prototype.getText = function() {
// normalize end of line to \n if (this.text) {
return this.text.replace(/\r\n/g,"\n"); // normalize end of line to \n
return this.text.replace(/\r\n/g,"\n");
} else {
return util.Uint8Array2str(this.bytes);
}
}; };
/** /**
@ -171,6 +175,11 @@ CleartextMessage.prototype.getText = function() {
* @returns {Uint8Array} A sequence of bytes * @returns {Uint8Array} A sequence of bytes
*/ */
CleartextMessage.prototype.getBytes = function() { CleartextMessage.prototype.getBytes = function() {
if (this.bytes) {
return this.bytes;
} else {
return util.str2Uint8Array(this.text.replace(/\r\n/g,"\n"));
}
return this.bytes; return this.bytes;
}; };
@ -200,7 +209,7 @@ CleartextMessage.prototype.armor = function() {
* @return {module:cleartext~CleartextMessage} new cleartext message object * @return {module:cleartext~CleartextMessage} new cleartext message object
* @static * @static
*/ */
export function readArmored(armoredText, isText=true) { export function readArmored(armoredText, bytes=false) {
var input = armor.decode(armoredText); var input = armor.decode(armoredText);
if (input.type !== enums.armor.signed) { if (input.type !== enums.armor.signed) {
throw new Error('No cleartext signed message.'); throw new Error('No cleartext signed message.');
@ -210,10 +219,10 @@ export function readArmored(armoredText, isText=true) {
verifyHeaders(input.headers, packetlist); verifyHeaders(input.headers, packetlist);
var signature = new sigModule.Signature(packetlist); var signature = new sigModule.Signature(packetlist);
var cleartext; var cleartext;
if (isText) { if (bytes) {
cleartext = input.cleartext.replace(/\n$/, '').replace(/\n/g, "\r\n");
} else {
cleartext = base64.decode(input.cleartext); cleartext = base64.decode(input.cleartext);
} else {
cleartext = input.cleartext.replace(/\n$/, '').replace(/\n/g, "\r\n");
} }
var newMessage = new CleartextMessage(cleartext, signature); var newMessage = new CleartextMessage(cleartext, signature);
return newMessage; return newMessage;

View File

@ -300,7 +300,7 @@ function dearmor(text) {
var sig_sum = splitChecksum(sig.body); var sig_sum = splitChecksum(sig.body);
result = { result = {
text: msg.body.replace(/\n$/, '').replace(/\n/g, "\r\n"), cleartext: msg.body,
data: base64.decode(sig_sum.body), data: base64.decode(sig_sum.body),
headers: msg.headers, headers: msg.headers,
type: type type: type
@ -353,7 +353,11 @@ function armor(messagetype, body, partindex, parttotal) {
case enums.armor.signed: case enums.armor.signed:
result.push("\r\n-----BEGIN PGP SIGNED MESSAGE-----\r\n"); result.push("\r\n-----BEGIN PGP SIGNED MESSAGE-----\r\n");
result.push("Hash: " + body.hash + "\r\n\r\n"); result.push("Hash: " + body.hash + "\r\n\r\n");
result.push(body.text.replace(/\n-/g, "\n- -")); if (body.text) {
result.push(body.text.replace(/\n-/g, "\n- -"));
} else {
result.push(base64.encode(body.bytes));
}
result.push("\r\n-----BEGIN PGP SIGNATURE-----\r\n"); result.push("\r\n-----BEGIN PGP SIGNATURE-----\r\n");
result.push(addheader()); result.push(addheader());
result.push(base64.encode(body.data)); result.push(base64.encode(body.data));