Merge pull request #637 from mmso/feat/compression

Feat/compression
This commit is contained in:
Bart Butler 2018-02-13 11:01:46 -08:00 committed by GitHub
commit a09066e5ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 878 additions and 729 deletions

View File

@ -154,6 +154,35 @@ openpgp.decrypt(options).then(function(plaintext) {
});
```
#### Encrypt with compression
By default, `encrypt` will not use any compression. It's possible to override that behavior in two ways:
Either set the `compression` parameter in the options object when calling `encrypt`.
```js
var options, encrypted;
options = {
data: new Uint8Array([0x01, 0x02, 0x03]), // input as Uint8Array (or String)
passwords: ['secret stuff'], // multiple passwords possible
compression: openpgp.enums.compression.zip // compress the data with zip
};
openpgp.encrypt(options).then(function(ciphertext) {
// use ciphertext
});
```
Or, override the config to enable compression:
```js
openpgp.config.compression = openpgp.enums.compression.zip
```
Where `compression` can take the value of `openpgp.enums.compression.zlib` or `openpgp.enums.compression.zip`.
#### Generate new key pair
RSA keys:

View File

@ -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.

View File

@ -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 compressed 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

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 {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String }
* @param {String} filename (optional) a filename for the literal data packet
* @param {module:enums.compression} 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 => {

View File

@ -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]);
};

View File

@ -177,6 +177,60 @@ 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() {
// Disable the call expectations when using the web worker because it's not possible to spy on what functions get called.
if (openpgp.getWorker()) {
return;
}
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 +618,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 +632,23 @@ 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 +663,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 +687,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 +734,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 +751,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 +771,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 +779,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 +794,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 +815,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 +823,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 +839,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 +861,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 +870,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 +884,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 +893,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 +907,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 +929,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 +951,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 +975,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 +986,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 +1009,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 +1025,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 +1048,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 +1063,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 +1086,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 +1097,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 +1118,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 +1140,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 +1162,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 +1183,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 +1207,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 +1217,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 +1237,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,29 +1368,32 @@ 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,
message: openpgp.message.readArmored(encrypted.data)
});
}).then(function(encrypted) {
expect(encrypted.data).to.exist;
expect(encrypted.data).to.equal(plaintext);
expect(encrypted.signatures[0].valid).to.be.true;
}).then(function (decrypted) {
expect(decrypted.data).to.exist;
expect(decrypted.data).to.equal(plaintext);
expect(decrypted.signatures[0].valid).to.be.true;
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);
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privKeyDE.getSigningKeyPacket().getKeyId().toHex());
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
});
});
});
});
@ -1374,11 +1466,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 +1481,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 +1499,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 +1516,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 +1530,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 +1554,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);
});
});
});
});