Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
PGP.prototype.importKeys = function(options, callback) {
var publicKey, privateKey;
// check passphrase
if (typeof options.passphrase !== 'string' || !options.privateKeyArmored || !options.publicKeyArmored) {
callback({
errMsg: 'Importing keys failed. Not all options set!'
});
return;
}
// clear any keypair already in the keychain
openpgp.keyring.init();
// unlock and import private key
if (!openpgp.keyring.importPrivateKey(options.privateKeyArmored, options.passphrase)) {
openpgp.keyring.init();
callback({
errMsg: 'Incorrect passphrase!'
});
return;
}
// import public key
openpgp.keyring.importPublicKey(options.publicKeyArmored);
// check if keys have the same id
privateKey = openpgp.keyring.exportPrivateKey(0);
publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
if (!privateKey || !privateKey.armored || !publicKey || !publicKey.armored || privateKey.keyId !== publicKey.keyId) {
// reset keyring
// clear any keypair already in the keychain
openpgp.keyring.init();
// unlock and import private key
if (!openpgp.keyring.importPrivateKey(options.privateKeyArmored, options.passphrase)) {
openpgp.keyring.init();
callback({
errMsg: 'Incorrect passphrase!'
});
return;
}
// import public key
openpgp.keyring.importPublicKey(options.publicKeyArmored);
// check if keys have the same id
privateKey = openpgp.keyring.exportPrivateKey(0);
publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
if (!privateKey || !privateKey.armored || !publicKey || !publicKey.armored || privateKey.keyId !== publicKey.keyId) {
// reset keyring
openpgp.keyring.init();
callback({
errMsg: 'Key IDs dont match!'
});
return;
}
callback();
};
PGP.prototype.decrypt = function(ciphertext, senderKey, callback) {
var privateKey = openpgp.keyring.exportPrivateKey(0).obj;
senderKey = openpgp.read_publicKey(senderKey)[0];
var msg = openpgp.read_message(ciphertext)[0];
var keymat = null;
var sesskey = null;
// Find the private (sub)key for the session key of the message
for (var i = 0; i < msg.sessionKeys.length; i++) {
if (privateKey.privateKeyPacket.publicKey.getKeyId() === msg.sessionKeys[i].keyId.bytes) {
keymat = {
key: privateKey,
keymaterial: privateKey.privateKeyPacket
};
sesskey = msg.sessionKeys[i];
break;
}
PGP.prototype.exportKeys = function(callback) {
var publicKey, privateKey;
privateKey = openpgp.keyring.exportPrivateKey(0);
if (privateKey && privateKey.keyId) {
publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
}
if (!privateKey || !privateKey.keyId || !privateKey.armored || !publicKey || !publicKey.armored) {
callback({
errMsg: 'Could not export keys!'
});
return;
}
callback(null, {
keyId: util.hexstrdump(privateKey.keyId).toUpperCase(),
privateKeyArmored: privateKey.armored,
publicKeyArmored: publicKey.armored
});