public boolean matchTag(AbstractKey other) { return info().matchTag(other.info()); }
/** * The smart keys search. First keys with matching tags and type, then all others with matching * type. * * @param keyInfo desired key information * @return Collection of matching keys, possibly empty. */ @Override @NonNull public Collection<AbstractKey> findKey(KeyInfo keyInfo) { final ArrayList<AbstractKey> result = new ArrayList<>(); for( AbstractKey k: keys) { final KeyInfo ki = k.info(); if( ki.matchType(keyInfo) ) { if( ki.matchTag(keyInfo) ) result.add(0, k); else result.add(k); } } return result; }
@Test public void matchTypeAndTag() throws Exception { // 2 different private keys AbstractKey k1 = TestKeys.privateKey(0); AbstractKey k2 = TestKeys.privateKey(1); assertTrue(k1.info().matchType(k2.info())); assertFalse(k1.info().matchTag(k2.info())); // public matches private, not vice versa AbstractKey k3 = k1.getPublicKey(); assertTrue(k1.info().matchType(k3.info())); assertFalse(k3.info().matchType(k1.info())); assertTrue(k1.info().matchTag(k3.info())); // public keys do not match each other! assertFalse(k3.info().matchType(k3.info())); assertFalse(k3.info().matchType(k2.getPublicKey().info())); // Check AES match algorythm and tag AbstractKey k4 = new SymmetricKey(); assertFalse(k2.matchType(k4)); assertFalse(k3.matchType(k4)); assertFalse(k4.matchType(k2)); assertFalse(k4.matchType(k3)); assertFalse(k4.matchTag(k2)); assertFalse(k4.matchTag(k3)); AbstractKey k5 = new SymmetricKey(); assertTrue(k4.matchType(k5)); assertTrue(k5.matchType(k4)); assertFalse(k4.matchTag(k5)); k4.setTag("Hello"); k5.setTag("Hello"); }