1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

Add PGP api for changing a user's passphrase

This commit is contained in:
Tankred Hase 2014-03-31 20:15:09 +02:00
parent f0f0aa3d63
commit f3b3a4b496
2 changed files with 71 additions and 0 deletions

View File

@ -184,6 +184,55 @@ define(function(require) {
});
};
/**
* Change the passphrase of an ascii armored private key.
*/
PGP.prototype.changePassphrase = function(options, callback) {
var privKey, packets;
if (!options.privateKeyArmored ||
typeof options.oldPassphrase !== 'string' ||
typeof options.newPassphrase !== 'string') {
callback({
errMsg: 'Could not export keys!'
});
return;
}
// read armored key
try {
privKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
} catch (e) {
callback({
errMsg: 'Importing key failed. Parsing error!'
});
return;
}
// decrypt private key with passphrase
if (!privKey.decrypt(options.oldPassphrase)) {
callback({
errMsg: 'Old passphrase incorrect!'
});
return;
}
// encrypt key with new passphrase
try {
packets = privKey.getAllKeyPackets();
for (var i = 0; i < packets.length; i++) {
packets[i].encrypt(options.newPassphrase);
}
} catch (e) {
callback({
errMsg: 'Setting new passphrase failed!'
});
return;
}
callback(null, privKey.armor());
};
/**
* Encrypt and sign a pgp message for a list of receivers
*/

View File

@ -120,6 +120,28 @@ define(function(require) {
});
});
describe('Change passphrase of private key', function() {
it('should work', function(done) {
pgp.changePassphrase({
privateKeyArmored: privkey,
oldPassphrase: passphrase,
newPassphrase: 'yxcv'
}, function(err, reEncryptedKey) {
expect(err).to.not.exist;
expect(reEncryptedKey).to.exist;
pgp.importKeys({
passphrase: 'yxcv',
privateKeyArmored: reEncryptedKey,
publicKeyArmored: pubkey
}, function(err) {
expect(err).to.not.exist;
done();
});
});
});
});
describe('Encrypt/Sign/Decrypt/Verify', function() {
var message = 'asdfs\n\nThursday, Nov 21, 2013 7:38 PM asdf@example.com wrote:\n' +
'> asdf\n' +