returnSessionKey flag in encrypt function

This commit is contained in:
Sanjana Rajan 2017-12-02 11:04:29 -08:00
parent f0d65780ad
commit 6189cd4568
4 changed files with 37 additions and 7 deletions

View File

@ -253,7 +253,13 @@ Message.prototype.encrypt = function(keys, passwords, sessionKey) {
}).then(() => {
msg.packets.push(symEncryptedPacket);
symEncryptedPacket.packets = new packet.List(); // remove packets after encryption
return msg;
return {
message: msg,
sessionKey: {
data: sessionKey,
algorithm: enums.read(enums.symmetric, symAlgo)
}
};
});
};

View File

@ -187,11 +187,11 @@ 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 }) {
export function encrypt({ data, publicKeys, privateKeys, passwords, sessionKey, filename, armor=true, detached=false, signature=null, returnSessionKey=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
return asyncProxy.delegate('encrypt', { data, publicKeys, privateKeys, passwords, sessionKey, filename, armor, detached, signature });
return asyncProxy.delegate('encrypt', { data, publicKeys, privateKeys, passwords, sessionKey, filename, armor, detached, signature, returnSessionKey });
}
var result = {};
return Promise.resolve().then(() => {
@ -214,11 +214,14 @@ export function encrypt({ data, publicKeys, privateKeys, passwords, sessionKey,
}
return message.encrypt(publicKeys, passwords, sessionKey);
}).then(message => {
}).then(encrypted => {
if (armor) {
result.data = message.armor();
result.data = encrypted.message.armor();
} else {
result.message = message;
result.message = encrypted.message;
}
if (returnSessionKey) {
result.sessionKey = encrypted.sessionKey;
}
return result;
}).catch(onError.bind(null, 'Error encrypting message'));

View File

@ -931,7 +931,7 @@ describe('Key', function() {
key = newKey;
return openpgp.message.fromText('hello').encrypt([key.key]);
}).then(function(msg) {
return msg.decrypt(key.key);
return msg.message.decrypt(key.key);
}).catch(function(err) {
expect(err.message).to.equal('Private key is not decrypted.');
});

View File

@ -629,6 +629,27 @@ describe('OpenPGP.js public api tests', function() {
});
});
it('should encrypt then decrypt using returned session key', function() {
var encOpt = {
data: plaintext,
publicKeys: publicKey.keys,
returnSessionKey: true
};
return openpgp.encrypt(encOpt).then(function(encrypted) {
expect(encrypted.data).to.match(/^-----BEGIN PGP MESSAGE/);
var decOpt = {
sessionKey: encrypted.sessionKey,
message: openpgp.message.readArmored(encrypted.data)
};
return openpgp.decrypt(decOpt);
}).then(function(decrypted) {
expect(decrypted.data).to.equal(plaintext);
expect(decrypted.signatures).to.exist;
expect(decrypted.signatures.length).to.equal(0);
});
});
it('should encrypt using custom session key and decrypt using session key', function() {
var sessionKey = {
data: openpgp.crypto.generateSessionKey('aes256'),