diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/KeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/KeySelectionStrategy.java deleted file mode 100644 index 68884e92..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/KeySelectionStrategy.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.key; - -import java.util.Set; -import javax.annotation.Nonnull; - - -/** - * Interface that describes a selection strategy for OpenPGP keys. - * @param Type of the Key - * @param Type of the PGPKeyRing - */ -public interface KeySelectionStrategy { - - boolean accept(K key); - - Set selectKeysFromKeyRing(@Nonnull R ring); -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/PublicKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/PublicKeySelectionStrategy.java deleted file mode 100644 index 5e0f22bb..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/PublicKeySelectionStrategy.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.key; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import javax.annotation.Nonnull; - -import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPPublicKeyRing; - -/** - * Key Selection Strategy which accepts {@link PGPPublicKey}s that are accepted by the abstract method - * {@link #accept(Object)}. - */ -public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy { - - @Override - public Set selectKeysFromKeyRing(@Nonnull PGPPublicKeyRing ring) { - Set keys = new HashSet<>(); - for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { - PGPPublicKey key = i.next(); - if (accept(key)) keys.add(key); - } - return keys; - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/SecretKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/SecretKeySelectionStrategy.java deleted file mode 100644 index 49e40a0d..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/SecretKeySelectionStrategy.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.key; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import javax.annotation.Nonnull; - -import org.bouncycastle.openpgp.PGPSecretKey; -import org.bouncycastle.openpgp.PGPSecretKeyRing; - -/** - * Key Selection Strategy which accepts {@link PGPSecretKey}s that are accepted by the abstract method - * {@link #accept(Object)}. - * - */ -public abstract class SecretKeySelectionStrategy implements KeySelectionStrategy { - - @Override - public Set selectKeysFromKeyRing(@Nonnull PGPSecretKeyRing ring) { - Set keys = new HashSet<>(); - for (Iterator i = ring.getSecretKeys(); i.hasNext(); ) { - PGPSecretKey key = i.next(); - if (accept(key)) keys.add(key); - } - return keys; - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/package-info.java deleted file mode 100644 index 971fa41c..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/key/package-info.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * Different Key Selection Strategies. - */ -package org.pgpainless.util.selection.key; diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/Email.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/Email.java deleted file mode 100644 index 059907df..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/Email.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.keyring.impl; - -public class Email { - - public static class PubRingSelectionStrategy extends PartialUserId.PubRingSelectionStrategy { - - public PubRingSelectionStrategy(String email) { - super(email.matches("^<.+>$") ? email : '<' + email + '>'); - } - } - - public static class SecRingSelectionStrategy extends PartialUserId.SecRingSelectionStrategy { - public SecRingSelectionStrategy(String email) { - super(email.matches("^<.+>$") ? email : '<' + email + '>'); - } - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/PartialUserId.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/PartialUserId.java deleted file mode 100644 index 816aca3a..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/keyring/impl/PartialUserId.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.keyring.impl; - -import javax.annotation.Nonnull; -import java.util.Iterator; - -import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPSecretKey; -import org.pgpainless.util.selection.key.PublicKeySelectionStrategy; -import org.pgpainless.util.selection.key.SecretKeySelectionStrategy; - -public class PartialUserId { - - public static class PubRingSelectionStrategy extends PublicKeySelectionStrategy { - - protected final String identifier; - - public PubRingSelectionStrategy(String identifier) { - this.identifier = identifier; - } - - @Override - public boolean accept(@Nonnull PGPPublicKey key) { - for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { - String userId = userIds.next(); - if (userId.contains(identifier)) { - return true; - } - } - return false; - } - } - - public static class SecRingSelectionStrategy extends SecretKeySelectionStrategy { - - protected final String partialUserId; - - public SecRingSelectionStrategy(String partialUserId) { - this.partialUserId = partialUserId; - } - - @Override - public boolean accept(@Nonnull PGPSecretKey key) { - for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { - String userId = userIds.next(); - if (userId.contains(partialUserId)) { - return true; - } - } - return false; - } - } -} diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/selection/keyring/EmailKeyRingSelectionStrategyTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/selection/keyring/EmailKeyRingSelectionStrategyTest.java deleted file mode 100644 index 655af7b8..00000000 --- a/pgpainless-core/src/test/java/org/pgpainless/util/selection/keyring/EmailKeyRingSelectionStrategyTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2020 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.util.selection.keyring; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; - -import org.bouncycastle.openpgp.PGPException; -import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPSecretKey; -import org.junit.jupiter.api.Test; -import org.pgpainless.key.TestKeys; -import org.pgpainless.util.selection.keyring.impl.Email; - -public class EmailKeyRingSelectionStrategyTest { - - @Test - public void testMatchingEmailUIDAcceptedOnPubKey() throws IOException { - String uid = ""; - PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - - Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid); - - assertTrue(pubKeySelectionStrategy.accept(key)); - } - - @Test - public void testAddressIsFormattedToMatchOnPubKey() throws IOException { - String uid = "emil@email.user"; - PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - - Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid); - - assertTrue(pubKeySelectionStrategy.accept(key)); - } - - @Test - public void testPubKeyWithDifferentUIDIsRejected() throws IOException { - String wrongUid = "emilia@email.user"; - PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - - Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(wrongUid); - - assertFalse(pubKeySelectionStrategy.accept(key)); - } - - @Test - public void testMatchingEmailUIDAcceptedOnSecKey() throws IOException, PGPException { - String uid = ""; - PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - - Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid); - - assertTrue(secKeySelectionStrategy.accept(key)); - } - - @Test - public void testAddressIsFormattedToMatchOnSecKey() throws IOException, PGPException { - String uid = "emil@email.user"; - PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - - Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid); - - assertTrue(secKeySelectionStrategy.accept(key)); - } - - @Test - public void testSecKeyWithDifferentUIDIsRejected() throws IOException, PGPException { - String wrongUid = "emilia@email.user"; - PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - - Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(wrongUid); - - assertFalse(secKeySelectionStrategy.accept(key)); - } -}