Add compression support for the encrypt function
This commit is contained in:
parent
90337debfb
commit
9f7466ef45
|
@ -31,7 +31,7 @@ export default {
|
|||
/** @property {Integer} encryption_cipher Default encryption cipher {@link module:enums.symmetric} */
|
||||
encryption_cipher: enums.symmetric.aes256,
|
||||
/** @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.
|
||||
|
|
|
@ -451,6 +451,26 @@ Message.prototype.sign = async function(privateKeys=[], signature=null) {
|
|||
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)
|
||||
* @param {Array<module:key~Key>} privateKey private keys with decrypted secret key data for signing
|
||||
|
|
|
@ -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 {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String }
|
||||
* @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} 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
|
||||
|
@ -202,7 +203,7 @@ export function decryptKey({ privateKey, passphrase }) {
|
|||
* message: full Message object if 'armor' is false, signature: detached signature if 'detached' is true}
|
||||
* @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);
|
||||
|
||||
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 = message.compress(compression);
|
||||
return message.encrypt(publicKeys, passwords, sessionKey, wildcard);
|
||||
|
||||
}).then(encrypted => {
|
||||
|
|
|
@ -88,7 +88,7 @@ Compressed.prototype.write = function () {
|
|||
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]);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -177,6 +177,55 @@ var twoPasswordGPGFail = ['-----BEGIN PGP MESSAGE-----',
|
|||
'=cHCV',
|
||||
'-----END PGP MESSAGE-----'].join('\n');
|
||||
|
||||
function withCompression(tests) {
|
||||
const compressionTypes = Object.keys(openpgp.enums.compression).map(k => openpgp.enums.compression[k]);
|
||||
|
||||
compressionTypes.forEach(function (compression) {
|
||||
const compressionName = openpgp.enums.read(openpgp.enums.compression, compression);
|
||||
const group = `compression - ${compressionName}`;
|
||||
|
||||
// Compression bzip2 [BZ2] is yet to be implemented.
|
||||
if (compression === openpgp.enums.compression.bzip2) {
|
||||
describe.skip(`${group} (not implemented --> skipping tests)`, tests);
|
||||
return;
|
||||
}
|
||||
|
||||
describe(group, function() {
|
||||
let compressSpy;
|
||||
let decompressSpy;
|
||||
|
||||
beforeEach(function () {
|
||||
compressSpy = sinon.spy(openpgp.packet.Compressed.prototype, 'compress');
|
||||
decompressSpy = sinon.spy(openpgp.packet.Compressed.prototype, 'decompress');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
compressSpy.restore();
|
||||
decompressSpy.restore();
|
||||
});
|
||||
|
||||
tests(
|
||||
function(options) {
|
||||
options.compression = compression;
|
||||
return options;
|
||||
},
|
||||
function() {
|
||||
if (compression === openpgp.enums.compression.uncompressed) {
|
||||
expect(compressSpy.called).to.be.false;
|
||||
expect(decompressSpy.called).to.be.false;
|
||||
return;
|
||||
}
|
||||
|
||||
expect(compressSpy.called).to.be.true;
|
||||
expect(compressSpy.thisValues[0].algorithm).to.equal(compressionName);
|
||||
expect(decompressSpy.called).to.be.true;
|
||||
expect(decompressSpy.thisValues[0].algorithm).to.equal(compressionName);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('OpenPGP.js public api tests', function() {
|
||||
|
||||
describe('initWorker, getWorker, destroyWorker - unit tests', function() {
|
||||
|
@ -564,12 +613,13 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
});
|
||||
|
||||
withCompression(function (modifyCompressionEncryptOptions, verifyCompressionDecrypted) {
|
||||
it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with pgp key pair', function () {
|
||||
var msgAsciiArmored;
|
||||
return openpgp.encrypt({
|
||||
return openpgp.encrypt(modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys
|
||||
}).then(function(encrypted) {
|
||||
})).then(function (encrypted) {
|
||||
msgAsciiArmored = encrypted.data;
|
||||
return openpgp.decryptSessionKeys({
|
||||
message: openpgp.message.readArmored(msgAsciiArmored),
|
||||
|
@ -577,22 +627,24 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
|
||||
}).then(function (decryptedSessionKeys) {
|
||||
const message = openpgp.message.readArmored(msgAsciiArmored);
|
||||
return openpgp.decrypt({
|
||||
sessionKeys: decryptedSessionKeys[0],
|
||||
message: openpgp.message.readArmored(msgAsciiArmored)
|
||||
message
|
||||
});
|
||||
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with password', function () {
|
||||
var msgAsciiArmored;
|
||||
return openpgp.encrypt({
|
||||
return openpgp.encrypt(modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: password1
|
||||
}).then(function(encrypted) {
|
||||
})).then(function (encrypted) {
|
||||
msgAsciiArmored = encrypted.data;
|
||||
return openpgp.decryptSessionKeys({
|
||||
message: openpgp.message.readArmored(msgAsciiArmored),
|
||||
|
@ -607,15 +659,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('roundtrip workflow: encrypt with multiple passwords, decryptSessionKeys, decrypt with multiple passwords', function () {
|
||||
var msgAsciiArmored;
|
||||
return openpgp.encrypt({
|
||||
return openpgp.encrypt(modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: [password1, password2]
|
||||
}).then(function(encrypted) {
|
||||
})).then(function (encrypted) {
|
||||
msgAsciiArmored = encrypted.data;
|
||||
return openpgp.decryptSessionKeys({
|
||||
message: openpgp.message.readArmored(msgAsciiArmored),
|
||||
|
@ -630,20 +683,31 @@ describe('OpenPGP.js public api tests', function() {
|
|||
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('roundtrip workflow: encrypt twice with one password, decryptSessionKeys, only one session key', function () {
|
||||
return openpgp.encrypt({
|
||||
var msgAsciiArmored;
|
||||
return openpgp.encrypt(modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: [password1, password1]
|
||||
}).then(function(encrypted) {
|
||||
})).then(function (encrypted) {
|
||||
msgAsciiArmored = encrypted.data;
|
||||
return openpgp.decryptSessionKeys({
|
||||
message: openpgp.message.readArmored(encrypted.data),
|
||||
message: openpgp.message.readArmored(msgAsciiArmored),
|
||||
passwords: password1
|
||||
});
|
||||
}).then(function (decryptedSessionKeys) {
|
||||
expect(decryptedSessionKeys.length).to.equal(1);
|
||||
return openpgp.decrypt({
|
||||
sessionKeys: decryptedSessionKeys,
|
||||
message: openpgp.message.readArmored(msgAsciiArmored)
|
||||
});
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -666,11 +730,12 @@ describe('OpenPGP.js public api tests', function() {
|
|||
privateKey.keys[0].verifyPrimaryUser().then(() => done());
|
||||
});
|
||||
|
||||
withCompression(function (modifyCompressionEncryptOptions, verifyCompressionDecrypted) {
|
||||
it('should encrypt then decrypt', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
|
@ -682,14 +747,15 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt then decrypt', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
|
@ -701,6 +767,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -708,10 +775,10 @@ describe('OpenPGP.js public api tests', function() {
|
|||
var privKeyDE = openpgp.key.readArmored(priv_key_de).keys[0];
|
||||
privKeyDE.decrypt(passphrase);
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: [privKeyDE, privateKey.keys[0]]
|
||||
};
|
||||
|
@ -723,15 +790,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt then decrypt with wildcard', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
wildcard: true
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
|
@ -743,6 +811,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -750,11 +819,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
var privKeyDE = openpgp.key.readArmored(priv_key_de).keys[0];
|
||||
privKeyDE.decrypt(passphrase);
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
wildcard: true
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: [privKeyDE, privateKey.keys[0]]
|
||||
};
|
||||
|
@ -766,15 +835,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt then decrypt using returned session key', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
returnSessionKey: true
|
||||
};
|
||||
});
|
||||
|
||||
return openpgp.encrypt(encOpt).then(function (encrypted) {
|
||||
expect(encrypted.data).to.match(/^-----BEGIN PGP MESSAGE/);
|
||||
|
@ -787,6 +857,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures).to.exist;
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -795,11 +866,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
data: openpgp.crypto.generateSessionKey('aes256'),
|
||||
algorithm: 'aes256'
|
||||
};
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
sessionKey: sessionKey,
|
||||
publicKeys: publicKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
sessionKeys: sessionKey
|
||||
};
|
||||
|
@ -809,6 +880,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
return openpgp.decrypt(decOpt);
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -817,11 +889,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
data: openpgp.crypto.generateSessionKey('aes128'),
|
||||
algorithm: 'aes128'
|
||||
};
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
sessionKeys: sessionKey,
|
||||
publicKeys: publicKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0]
|
||||
};
|
||||
|
@ -831,15 +903,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
return openpgp.decrypt(decOpt);
|
||||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt/sign and decrypt/verify', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: publicKey.keys
|
||||
|
@ -852,15 +925,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.true;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt/sign and decrypt/verify with null string input', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: '',
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: publicKey.keys
|
||||
|
@ -873,16 +947,17 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.true;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt/sign and decrypt/verify with detached signatures', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys,
|
||||
detached: true
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: publicKey.keys
|
||||
|
@ -896,6 +971,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.true;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -906,11 +982,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
detached: true
|
||||
};
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
detached: true
|
||||
};
|
||||
});
|
||||
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
|
@ -929,6 +1005,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.true;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -944,11 +1021,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
detached: true
|
||||
};
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys[0]
|
||||
};
|
||||
});
|
||||
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
|
@ -967,6 +1044,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
expect(decrypted.signatures[1].valid).to.be.true;
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
return privKeyDE.verifyPrimaryUser().then(() => {
|
||||
expect(decrypted.signatures[1].keyid.toHex()).to.equal(privKeyDE.getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[1].signature.packets.length).to.equal(1);
|
||||
|
@ -981,11 +1059,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
detached: true
|
||||
};
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
detached: true
|
||||
};
|
||||
});
|
||||
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
|
@ -1004,6 +1082,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1014,10 +1093,10 @@ describe('OpenPGP.js public api tests', function() {
|
|||
detached: true
|
||||
};
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys
|
||||
};
|
||||
});
|
||||
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
|
@ -1035,15 +1114,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail to verify decrypted data with wrong public pgp key', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: openpgp.key.readArmored(wrong_pubkey).keys
|
||||
|
@ -1056,15 +1136,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail to verify decrypted null string with wrong public pgp key', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: '',
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: openpgp.key.readArmored(wrong_pubkey).keys
|
||||
|
@ -1077,15 +1158,16 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should successfully decrypt signed message without public keys to verify', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
};
|
||||
|
@ -1097,16 +1179,17 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail to verify decrypted data with wrong public pgp key with detached signatures', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys,
|
||||
detached: true
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
publicKeys: openpgp.key.readArmored(wrong_pubkey).keys
|
||||
|
@ -1120,6 +1203,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].valid).to.be.null;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1129,11 +1213,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
|
||||
var pubKeyDE = openpgp.key.readArmored(pub_key_de).keys[0];
|
||||
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: [privateKey.keys[0], privKeyDE]
|
||||
};
|
||||
});
|
||||
|
||||
var decOpt = {
|
||||
privateKeys: privateKey.keys[0],
|
||||
|
@ -1149,6 +1233,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
expect(decrypted.signatures[1].valid).to.be.true;
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
return privKeyDE.verifyPrimaryUser().then(() => {
|
||||
expect(decrypted.signatures[1].keyid.toHex()).to.equal(privKeyDE.getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[1].signature.packets.length).to.equal(1);
|
||||
|
@ -1279,17 +1364,19 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ELG / DSA encrypt, decrypt, sign, verify', function() {
|
||||
withCompression(function (modifyCompressionEncryptOptions, verifyCompressionDecrypted) {
|
||||
it('round trip test', function () {
|
||||
var pubKeyDE = openpgp.key.readArmored(pub_key_de).keys[0];
|
||||
var privKeyDE = openpgp.key.readArmored(priv_key_de).keys[0];
|
||||
privKeyDE.decrypt(passphrase);
|
||||
return openpgp.encrypt({
|
||||
return openpgp.encrypt(modifyCompressionEncryptOptions({
|
||||
publicKeys: pubKeyDE,
|
||||
privateKeys: privKeyDE,
|
||||
data: plaintext
|
||||
}).then(function(encrypted) {
|
||||
})).then(function (encrypted) {
|
||||
return openpgp.decrypt({
|
||||
privateKeys: privKeyDE,
|
||||
publicKeys: pubKeyDE,
|
||||
|
@ -1299,6 +1386,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
expect(encrypted.data).to.exist;
|
||||
expect(encrypted.data).to.equal(plaintext);
|
||||
expect(encrypted.signatures[0].valid).to.be.true;
|
||||
verifyCompressionDecrypted(encrypted);
|
||||
return privKeyDE.verifyPrimaryUser().then(() => {
|
||||
expect(encrypted.signatures[0].keyid.toHex()).to.equal(privKeyDE.getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(encrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
|
@ -1306,6 +1394,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("3DES decrypt", function() {
|
||||
var pgp_msg =
|
||||
|
@ -1374,11 +1463,12 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
|
||||
describe('AES encrypt, decrypt', function() {
|
||||
withCompression(function (modifyCompressionEncryptOptions, verifyCompressionDecrypted) {
|
||||
it('should encrypt and decrypt with one password', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: password1
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
passwords: password1
|
||||
};
|
||||
|
@ -1388,14 +1478,15 @@ describe('OpenPGP.js public api tests', function() {
|
|||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt and decrypt with two passwords', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: [password1, password2]
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
passwords: password2
|
||||
};
|
||||
|
@ -1405,6 +1496,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1421,11 +1513,11 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
|
||||
it('should encrypt and decrypt with password and not ascii armor', function () {
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: plaintext,
|
||||
passwords: password1,
|
||||
armor: false
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
passwords: password1
|
||||
};
|
||||
|
@ -1435,16 +1527,17 @@ describe('OpenPGP.js public api tests', function() {
|
|||
}).then(function (decrypted) {
|
||||
expect(decrypted.data).to.equal(plaintext);
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt and decrypt with binary data and transferable objects', function () {
|
||||
openpgp.config.zero_copy = true; // activate transferable objects
|
||||
var encOpt = {
|
||||
var encOpt = modifyCompressionEncryptOptions({
|
||||
data: new Uint8Array([0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01]),
|
||||
passwords: password1,
|
||||
armor: false
|
||||
};
|
||||
});
|
||||
var decOpt = {
|
||||
passwords: password1,
|
||||
format: 'binary'
|
||||
|
@ -1458,6 +1551,8 @@ describe('OpenPGP.js public api tests', function() {
|
|||
}
|
||||
expect(decrypted.data).to.deep.equal(new Uint8Array([0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01]));
|
||||
expect(decrypted.signatures.length).to.equal(0);
|
||||
verifyCompressionDecrypted(decrypted);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user