Add compression support for the encrypt function

This commit is contained in:
mmso 2018-02-13 15:08:41 +01:00
parent 90337debfb
commit 9f7466ef45
No known key found for this signature in database
GPG Key ID: 954AFC83B3C56A54
5 changed files with 832 additions and 715 deletions

View File

@ -31,7 +31,7 @@ export default {
/** @property {Integer} encryption_cipher Default encryption cipher {@link module:enums.symmetric} */ /** @property {Integer} encryption_cipher Default encryption cipher {@link module:enums.symmetric} */
encryption_cipher: enums.symmetric.aes256, encryption_cipher: enums.symmetric.aes256,
/** @property {Integer} compression Default compression algorithm {@link module:enums.compression} */ /** @property {Integer} compression Default compression algorithm {@link module:enums.compression} */
compression: enums.compression.zip, compression: enums.compression.uncompressed,
/** /**
* Use Authenticated Encryption with Additional Data (AEAD) protection for symmetric encryption. * Use Authenticated Encryption with Additional Data (AEAD) protection for symmetric encryption.

View File

@ -451,6 +451,26 @@ Message.prototype.sign = async function(privateKeys=[], signature=null) {
return new Message(packetlist); return new Message(packetlist);
}; };
/**
* Compresses the message (the literal and -if signed- signature data packets of the message)
* @param {module:enums.compression} compression compression algorithm to be used
* @return {module:message~Message} new message with signed content
*/
Message.prototype.compress = function(compression) {
if (compression === enums.compression.uncompressed) {
return this;
}
const compressed = new packet.Compressed();
compressed.packets = this.packets;
compressed.algorithm = enums.read(enums.compression, compression);
const packetList = new packet.List();
packetList.push(compressed);
return new Message(packetList);
};
/** /**
* Create a detached signature for the message (the literal data packet of the message) * Create a detached signature for the message (the literal data packet of the message)
* @param {Array<module:key~Key>} privateKey private keys with decrypted secret key data for signing * @param {Array<module:key~Key>} privateKey private keys with decrypted secret key data for signing

View File

@ -192,6 +192,7 @@ export function decryptKey({ privateKey, passphrase }) {
* @param {String|Array<String>} passwords (optional) array of passwords or a single password to encrypt the message * @param {String|Array<String>} passwords (optional) array of passwords or a single password to encrypt the message
* @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String } * @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String }
* @param {String} filename (optional) a filename for the literal data packet * @param {String} filename (optional) a filename for the literal data packet
* @param {Boolean} compression (optional) which compression algorithm to compress the message with, defaults to what is specified in config
* @param {Boolean} armor (optional) if the return values should be ascii armored or the message/signature objects * @param {Boolean} armor (optional) if the return values should be ascii armored or the message/signature objects
* @param {Boolean} detached (optional) if the signature should be detached (if true, signature will be added to returned object) * @param {Boolean} detached (optional) if the signature should be detached (if true, signature will be added to returned object)
* @param {Signature} signature (optional) a detached signature to add to the encrypted message * @param {Signature} signature (optional) a detached signature to add to the encrypted message
@ -202,7 +203,7 @@ export function decryptKey({ privateKey, passphrase }) {
* message: full Message object if 'armor' is false, signature: detached signature if 'detached' is true} * message: full Message object if 'armor' is false, signature: detached signature if 'detached' is true}
* @static * @static
*/ */
export function encrypt({ data, publicKeys, privateKeys, passwords, sessionKey, filename, armor=true, detached=false, signature=null, returnSessionKey=false, wildcard=false}) { export function encrypt({ data, publicKeys, privateKeys, passwords, sessionKey, filename, compression=config.compression, armor=true, detached=false, signature=null, returnSessionKey=false, wildcard=false}) {
checkData(data); publicKeys = toArray(publicKeys); privateKeys = toArray(privateKeys); passwords = toArray(passwords); checkData(data); publicKeys = toArray(publicKeys); privateKeys = toArray(privateKeys); passwords = toArray(passwords);
if (!nativeAEAD() && asyncProxy) { // use web worker if web crypto apis are not supported if (!nativeAEAD() && asyncProxy) { // use web worker if web crypto apis are not supported
@ -223,6 +224,7 @@ export function encrypt({ data, publicKeys, privateKeys, passwords, sessionKey,
message = await message.sign(privateKeys, signature); message = await message.sign(privateKeys, signature);
} }
} }
message = message.compress(compression);
return message.encrypt(publicKeys, passwords, sessionKey, wildcard); return message.encrypt(publicKeys, passwords, sessionKey, wildcard);
}).then(encrypted => { }).then(encrypted => {

View File

@ -88,7 +88,7 @@ Compressed.prototype.write = function () {
this.compress(); this.compress();
} }
return util.concatUint8Array(new Uint8Array([enums.write(enums.compression, this.algorithm)]), this.compressed); return util.concatUint8Array([new Uint8Array([enums.write(enums.compression, this.algorithm)]), this.compressed]);
}; };

File diff suppressed because it is too large Load Diff