Implement feature #443

This commit is contained in:
evilaliv3 2016-04-25 00:54:16 +02:00
parent 528b5f2fe6
commit afc2c95238
2 changed files with 40 additions and 0 deletions

View File

@ -365,6 +365,27 @@ Key.prototype.getEncryptionKeyPacket = function() {
return null;
};
/**
* Encrypts all secret key and subkey packets
* @param {String} passphrase
* @return {Boolean} true if all key and subkey packets encrypted successfully
*/
Key.prototype.encrypt = function(passphrase) {
if (this.isPrivate()) {
var keys = this.getAllKeyPackets();
for (var i = 0; i < keys.length; i++) {
try {
keys[i].encrypt(passphrase);
} catch (e) {
return false;
}
}
} else {
throw new Error("Nothing to encrypt in a public key");
}
return true;
};
/**
* Decrypts all secret key and subkey packets
* @param {String} passphrase

View File

@ -706,5 +706,24 @@ var pgp_desktop_priv =
}).catch(done);
});
it('Encrypt key with new passphrase', function(done) {
var userId = 'test <a@b.com>';
var opt = {numBits: 512, userIds: userId, passphrase: 'passphrase'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKey(opt).then(function(key) {
key = key.key;
var armor1 = key.armor();
var armor2 = key.armor();
expect(armor1).to.equal(armor2);
expect(key.decrypt('passphrase')).to.be.true;
expect(key.encrypt('new_passphrase')).to.be.true;
expect(key.decrypt('passphrase')).to.be.false;
expect(key.decrypt('new_passphrase')).to.be.true;
var armor3 = key.armor();
expect(armor3).to.not.equal(armor1);
done();
}).catch(done);
});
});