js: add decrypt result options

--

* As a decrypt result cannot be known beforehand, the decrypt operation
  may add an 'expect' property, taking either 'uint8' or 'base64',
  which will return the decrypted data in the appropiate formats.
  the return property 'format' will give a feedback on which option
  was taken.
  A test was added to reflect these changes.
This commit is contained in:
Maximilian Krambach 2018-08-22 19:07:05 +02:00
parent f0409bbdaf
commit 24a0005865
4 changed files with 44 additions and 12 deletions

View File

@ -144,6 +144,30 @@ describe('Encryption and Decryption', function (){
});
}).timeout(3000);
it('Random data, original data is and should stay base64 encoded',
function (done) {
let data = bigBoringString(0.001);
let b64data = btoa(data);
context.encrypt(
{ data: b64data, publicKeys: good_fpr })
.then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
'END PGP MESSAGE');
context.decrypt({
data:answer.data, expect: 'base64' })
.then(function (result) {
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(b64data);
done();
});
});
}).timeout(3000);
for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
(j + 1) + '/'

View File

@ -266,13 +266,16 @@ class Answer{
if (_decodedResponse.base64 === true
&& poa.data[key] === 'string'
) {
if (this.expected === 'binary'){
if (this.expected === 'uint8'){
_response[key] = atobArray(_decodedResponse[key]);
_response.binary = true;
_response.format = 'uint8';
} else if (this.expected === 'base64'){
_response[key] = _decodedResponse[key];
_response.format = 'base64';
} else {
_response[key] = Utf8ArrayToStr(
atobArray(_decodedResponse[key]));
_response.binary = false;
_response.format = 'string';
}
} else {
_response[key] = decode(_decodedResponse[key]);

View File

@ -64,7 +64,7 @@ export class GPGME_Message {
}
set expected (value){
if (value === 'binary'){
if (value === 'uint8' || value === 'base64'){
this._expected = value;
}
}

View File

@ -31,7 +31,11 @@ import { createSignature } from './Signature';
/**
* @typedef {Object} decrypt_result
* @property {String|Uint8Array} data The decrypted data
* @property {Boolean} binary indicating whether data is an Uint8Array.
* @property {String} format Indicating how the data was converted after being
* received from gpgme.
* 'string': Data was decoded into an utf-8 string,
* 'base64': Data was not processed and is a base64 string
* 'uint8': data was turned into a Uint8Array
* @property {Boolean} is_mime (optional) the data claims to be a MIME
* object.
* @property {String} file_name (optional) the original file name
@ -51,8 +55,8 @@ import { createSignature } from './Signature';
/**
* @typedef {Object} encrypt_result The result of an encrypt operation
* @property {String} data The encrypted message
* @property {Boolean} binary Indicating whether returning payload data is an
* Uint8Array.
* @property {Boolean} base64 Indicating whether returning payload data is
* base64 encoded
*/
/**
@ -187,8 +191,9 @@ export class GpgME {
* Strings and Objects with a getText method
* @param {Boolean} options.base64 (optional) false if the data is an
* armored block, true if it is base64 encoded binary data
* @param {Boolean} options.binary (optional) if true, treat the decoded
* data as binary, and return the data as Uint8Array
* @param {String} options.expect (optional) can be set to 'uint8' or
* 'base64'. Does no extra decoding on the data, and returns the decoded
* data as either Uint8Array or unprocessed(base64 encoded) string.
* @returns {Promise<decrypt_result>} Decrypted Message and information
* @async
*/
@ -207,14 +212,14 @@ export class GpgME {
if (options.base64 === true){
msg.setParameter('base64', true);
}
if (options.binary === true){
msg.expected = 'binary';
if (options.expect === 'base64' || options.expect === 'uint8'){
msg.expected = options.expect;
}
putData(msg, options.data);
return new Promise(function (resolve, reject){
msg.post().then(function (result){
let _result = { data: result.data };
_result.binary = result.binary ? true: false;
_result.format = result.format ? result.format : null;
if (result.hasOwnProperty('dec_info')){
_result.is_mime = result.dec_info.is_mime ? true: false;
if (result.dec_info.file_name) {