From 1dfdfb62cb3505732f546057a88e719bd765e7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Wed, 26 Feb 2014 11:45:03 +0100 Subject: [PATCH] Keyring: simplify API, accept 16 char hex or fingerprint as keyid. --- src/keyring/keyring.js | 96 ++++++++++++----------------------------- test/general/keyring.js | 50 ++++++++++----------- 2 files changed, 53 insertions(+), 93 deletions(-) diff --git a/src/keyring/keyring.js b/src/keyring/keyring.js index fd923c9b..c991a763 100644 --- a/src/keyring/keyring.js +++ b/src/keyring/keyring.js @@ -59,7 +59,8 @@ Keyring.prototype.clear = function() { /** * Searches the keyring for keys having the specified key id - * @param {String} keyId provided as string of hex number (lowercase) + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) * @param {Boolean} deep if true search also in subkeys * @return {Array|null} keys found or null */ @@ -72,7 +73,8 @@ Keyring.prototype.getKeysForId = function (keyId, deep) { /** * Removes keys having the specified key id from the keyring - * @param {String} keyId provided as string of hex number (lowercase) + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) * @return {Array|null} keys found or null */ Keyring.prototype.removeKeysForId = function (keyId) { @@ -82,31 +84,6 @@ Keyring.prototype.removeKeysForId = function (keyId) { return result.length ? result : null; }; -/** - * Searches the keyring for keys having the specified long key id (fingerprint) - * @param {String} longKeyId fingerprint in lowercase hex - * @param {Boolean} deep if true search also in subkeys - * @return {Array|null} keys found or null - */ -Keyring.prototype.getKeysForLongId = function (longKeyId, deep) { - var result = []; - result = result.concat(this.publicKeys.getForLongId(longKeyId, deep) || []); - result = result.concat(this.privateKeys.getForLongId(longKeyId, deep) || []); - return result.length ? result : null; -}; - -/** - * Removes keys having the specified long key id (fingerprint) from the keyring - * @param {String} longKeyId fingerprint in lowercase hex - * @return {Array|null} keys found or null - */ -Keyring.prototype.removeKeysForLongId = function (longKeyId) { - var result = []; - result = result.concat(this.publicKeys.removeForLongId(longKeyId) || []); - result = result.concat(this.privateKeys.removeForLongId(longKeyId) || []); - return result.length ? result : null; -}; - /** * Get all public and private keys * @return {Array} all keys @@ -140,6 +117,7 @@ KeyArray.prototype.getForAddress = function(email) { /** * Checks a key to see if it matches the specified email address + * @private * @param {String} email email address to search for * @param {module:key~Key} key The key to be checked. * @return {Boolean} True if the email address is defined in the specified key @@ -157,42 +135,37 @@ function emailCheck(email, key) { return false; } +/** + * Checks a key to see if it matches the specified keyid + * @private + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) + * @param {module:packet/secret_key|public_key|public_subkey|secret_subkey} keypacket The keypacket to be checked + * @return {Boolean} True if keypacket has the specified keyid + */ +function keyIdCheck(keyId, keypacket) { + if (keyId.length === 16) { + return keyId === keypacket.getKeyId().toHex(); + } else { + return keyId === keypacket.getFingerprint(); + } +} + /** * Searches the KeyArray for a key having the specified key id - * @param {String} keyId provided as string of hex number (lowercase) + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) * @param {Boolean} deep if true search also in subkeys * @return {module:key~Key|null} key found or null */ KeyArray.prototype.getForId = function (keyId, deep) { for (var i = 0; i < this.keys.length; i++) { - if (this.keys[i].primaryKey.getKeyId().toHex() === keyId) { + if (keyIdCheck(keyId, this.keys[i].primaryKey)) { return this.keys[i]; } if (deep && this.keys[i].subKeys) { for (var j = 0; j < this.keys[i].subKeys.length; j++) { - if (this.keys[i].subKeys[j].subKey.getKeyId().toHex() === keyId) { - return this.keys[i]; - } - } - } - } - return null; -}; - -/** - * Searches the keyring for a key having the specified long key id (fingerprint) - * @param {String} longKeyId fingerprint in lowercase hex - * @param {Boolean} deep if true search also in subkeys - * @return {module:key~Key|null} key found or null - */ -KeyArray.prototype.getForLongId = function(longKeyId, deep) { - for (var i = 0; i < this.keys.length; i++) { - if (this.keys[i].primaryKey.getFingerprint() === longKeyId) { - return this.keys[i]; - } - if (deep && this.keys[i].subKeys) { - for (var j = 0; j < this.keys[i].subKeys.length; j++) { - if (this.keys[i].subKeys[j].subKey.getFingerprint() === longKeyId) { + if (keyIdCheck(keyId, this.keys[i].subKeys[j].subKey)) { return this.keys[i]; } } @@ -233,26 +206,13 @@ KeyArray.prototype.push = function (key) { /** * Removes a key with the specified keyid from the keyring - * @param {String} keyId provided as string of hex number (lowercase) + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) * @return {module:key~Key|null} The key object which has been removed or null */ KeyArray.prototype.removeForId = function (keyId) { for (var i = 0; i < this.keys.length; i++) { - if (this.keys[i].primaryKey.getKeyId().toHex() === keyId) { - return this.keys.splice(i, 1)[0]; - } - } - return null; -}; - -/** - * Removes a key with the specified long key id (fingerprint) from the keyring - * @param {String} longKeyId fingerprint in lowercase hex - * @return {module:key~Key|null} The key object which has been removed or null - */ -KeyArray.prototype.removeForLongId = function (longKeyId) { - for (var i = 0; i < this.keys.length; i++) { - if (this.keys[i].primaryKey.getFingerprint() === longKeyId) { + if (keyIdCheck(keyId, this.keys[i].primaryKey)) { return this.keys.splice(i, 1)[0]; } } diff --git a/test/general/keyring.js b/test/general/keyring.js index 2dcff4ec..4e77dcaf 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -94,7 +94,7 @@ describe("Keyring", function() { }); it('getKeysForId() - unknown id', function() { - var keys = keyring.getKeysForId('000102030405060708'); + var keys = keyring.getKeysForId('01234567890123456'); expect(keys).to.be.null; }); @@ -106,7 +106,7 @@ describe("Keyring", function() { }); it('publicKeys.getForId() - unknown id', function() { - var key = keyring.publicKeys.getForId('000102030405060708'); + var key = keyring.publicKeys.getForId('01234567890123456'); expect(key).to.be.null; }); @@ -117,7 +117,7 @@ describe("Keyring", function() { }); it('privateKeys.getForId() - unknown id', function() { - var key = keyring.privateKeys.getForId('000102030405060708'); + var key = keyring.privateKeys.getForId('01234567890123456'); expect(key).to.be.null; }); @@ -138,35 +138,35 @@ describe("Keyring", function() { expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); }); - it('getKeysForLongId() - unknown long id', function() { - var keys = keyring.getKeysForLongId('000102030405060708'); + it('getKeysForId() - unknown fingerprint', function() { + var keys = keyring.getKeysForId('71130e8383bef9526e062600d5e9f93acbbc7275'); expect(keys).to.be.null; }); - it('getKeysForLongId() - valid long id', function() { - var keys = keyring.getKeysForLongId(keyFingerP2); + it('getKeysForId() - valid fingerprint', function() { + var keys = keyring.getKeysForId(keyFingerP2); expect(keys).to.exist.and.have.length(1); expect(keys[0].primaryKey.getKeyId().toHex()).equals(keyId2); }); - it('publicKeys.getForLongId() - unknown long id', function() { - var key = keyring.publicKeys.getForLongId('000102030405060708'); + it('publicKeys.getForId() - unknown fingerprint', function() { + var key = keyring.publicKeys.getForId('71130e8383bef9526e062600d5e9f93acbbc7275'); expect(key).to.be.null; }); - it('publicKeys.getForLongId() - valid long id', function() { - var key = keyring.publicKeys.getForLongId(keyFingerP2); + it('publicKeys.getForId() - valid fingerprint', function() { + var key = keyring.publicKeys.getForId(keyFingerP2); expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); }); - it('publicKeys.getForLongId() - subkey long id', function() { - var key = keyring.publicKeys.getForLongId(subkeyFingerP2); + it('publicKeys.getForId() - subkey fingerprint', function() { + var key = keyring.publicKeys.getForId(subkeyFingerP2); expect(key).to.be.null; }); - it('publicKeys.getForLongId() - deep, including subkeys - subkey long id', function() { - var key = keyring.publicKeys.getForLongId(subkeyFingerP2, true); + it('publicKeys.getForId() - deep, including subkeys - subkey fingerprint', function() { + var key = keyring.publicKeys.getForId(subkeyFingerP2, true); expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); }); @@ -203,7 +203,7 @@ describe("Keyring", function() { }); it('publicKeys.removeForId() - unknown id', function() { - var key = keyring.publicKeys.removeForId('000102030405060708'); + var key = keyring.publicKeys.removeForId('01234567890123456'); expect(key).to.be.null; }); @@ -214,14 +214,14 @@ describe("Keyring", function() { expect(keyring.publicKeys.keys).to.exist.and.have.length(1); }); - it('publicKeys.removeForLongId() - unknown id', function() { - var key = keyring.publicKeys.removeForLongId('000102030405060708'); + it('publicKeys.removeForId() - unknown fingerprint', function() { + var key = keyring.publicKeys.removeForId('71130e8383bef9526e062600d5e9f93acbbc7275'); expect(key).to.be.null; expect(keyring.publicKeys.keys).to.exist.and.have.length(1); }); - it('publicKeys.removeForLongId() - valid id', function() { - var key = keyring.publicKeys.removeForLongId(keyFingerP2); + it('publicKeys.removeForId() - valid fingerprint', function() { + var key = keyring.publicKeys.removeForId(keyFingerP2); expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); expect(keyring.publicKeys.keys).to.be.empty; @@ -244,7 +244,7 @@ describe("Keyring", function() { keyring.privateKeys.importKey(privkey); expect(keyring.publicKeys.keys).to.have.length(2); expect(keyring.privateKeys.keys).to.have.length(1); - var keys = keyring.removeKeysForId('000102030405060708'); + var keys = keyring.removeKeysForId('01234567890123456'); expect(keys).to.be.null; expect(keyring.publicKeys.keys).to.have.length(2); expect(keyring.privateKeys.keys).to.have.length(1); @@ -257,20 +257,20 @@ describe("Keyring", function() { expect(keyring.privateKeys.keys).to.have.length(0); }); - it('removeKeysForLongId() - unknown id', function() { + it('removeKeysForId() - unknown fingerprint', function() { keyring.publicKeys.importKey(pubkey); keyring.publicKeys.importKey(pubkey2); keyring.privateKeys.importKey(privkey); expect(keyring.publicKeys.keys).to.have.length(2); expect(keyring.privateKeys.keys).to.have.length(1); - var keys = keyring.removeKeysForLongId('000102030405060708'); + var keys = keyring.removeKeysForId('71130e8383bef9526e062600d5e9f93acbbc7275'); expect(keys).to.be.null; expect(keyring.publicKeys.keys).to.have.length(2); expect(keyring.privateKeys.keys).to.have.length(1); }); - it('removeKeysForLongId() - valid id', function() { - var keys = keyring.removeKeysForLongId(keyFingerP); + it('removeKeysForId() - valid fingerprint', function() { + var keys = keyring.removeKeysForId(keyFingerP); expect(keys).to.have.length(2); expect(keyring.publicKeys.keys).to.have.length(1); expect(keyring.privateKeys.keys).to.have.length(0);