Allow either privateKey, session key or password for message.decrypt()
This commit is contained in:
parent
83fcbaa633
commit
31df9c07da
|
@ -86,11 +86,13 @@ Message.prototype.getSigningKeyIds = function() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt the message
|
* Decrypt the message
|
||||||
* @param {module:key~Key|String} privateKey private key with decrypted secret data, password or session key
|
* @param {module:key~Key} privateKey private key with decrypted secret data
|
||||||
|
* @param {String} sessionKey session key as a binary string
|
||||||
|
* @param {String} password password used to decrypt
|
||||||
* @return {Array<module:message~Message>} new message with decrypted content
|
* @return {Array<module:message~Message>} new message with decrypted content
|
||||||
*/
|
*/
|
||||||
Message.prototype.decrypt = function(privateKey) {
|
Message.prototype.decrypt = function(privateKey, sessionKey, password) {
|
||||||
var keyObj = this.decryptSessionKey(privateKey);
|
var keyObj = this.decryptSessionKey(privateKey, sessionKey, password);
|
||||||
if (!keyObj) {
|
if (!keyObj) {
|
||||||
// nothing to decrypt return unmodified message
|
// nothing to decrypt return unmodified message
|
||||||
return this;
|
return this;
|
||||||
|
@ -108,18 +110,20 @@ Message.prototype.decrypt = function(privateKey) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt session key
|
* Decrypt session key
|
||||||
* @param {module:key~Key|String} privateKey private key with decrypted secret data or password
|
* @param {module:key~Key} privateKey private key with decrypted secret data
|
||||||
|
* @param {String} sessionKey session key as a binary string
|
||||||
|
* @param {String} password password used to decrypt
|
||||||
* @return {Object} object with sessionKey, algo
|
* @return {Object} object with sessionKey, algo
|
||||||
*/
|
*/
|
||||||
Message.prototype.decryptSessionKey = function(privateKey) {
|
Message.prototype.decryptSessionKey = function(privateKey, sessionKey, password) {
|
||||||
var keyPacket;
|
var keyPacket;
|
||||||
if (String.prototype.isPrototypeOf(privateKey) || typeof privateKey === 'string') {
|
if (sessionKey || password) {
|
||||||
var symEncryptedSessionKeyPacketlist = this.packets.filterByTag(enums.packet.symEncryptedSessionKey);
|
var symEncryptedSessionKeyPacketlist = this.packets.filterByTag(enums.packet.symEncryptedSessionKey);
|
||||||
var symLength = symEncryptedSessionKeyPacketlist.length;
|
var symLength = symEncryptedSessionKeyPacketlist.length;
|
||||||
for (var i = 0; i < symLength; i++) {
|
for (var i = 0; i < symLength; i++) {
|
||||||
keyPacket = symEncryptedSessionKeyPacketlist[i];
|
keyPacket = symEncryptedSessionKeyPacketlist[i];
|
||||||
try {
|
try {
|
||||||
keyPacket.decrypt(privateKey);
|
keyPacket.decrypt(sessionKey || password);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
|
@ -132,7 +136,7 @@ Message.prototype.decryptSessionKey = function(privateKey) {
|
||||||
throw new Error('No symmetrically encrypted session key packet found.');
|
throw new Error('No symmetrically encrypted session key packet found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else if (privateKey) {
|
||||||
var encryptionKeyIds = this.getEncryptionKeyIds();
|
var encryptionKeyIds = this.getEncryptionKeyIds();
|
||||||
if (!encryptionKeyIds.length) {
|
if (!encryptionKeyIds.length) {
|
||||||
// nothing to decrypt
|
// nothing to decrypt
|
||||||
|
@ -150,6 +154,9 @@ Message.prototype.decryptSessionKey = function(privateKey) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new Error('No key or password specified.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keyPacket) {
|
if (keyPacket) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user