1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 12:52:07 +01:00

Move selection strategies to util package and implement UserIdSelectionStrategy

This commit is contained in:
Paul Schaub 2021-02-03 16:05:21 +01:00
parent 747776d8cc
commit eaee5a27fc
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
39 changed files with 200 additions and 90 deletions

View file

@ -43,14 +43,14 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.exception.SecretKeyNotFoundException; import org.pgpainless.exception.SecretKeyNotFoundException;
import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
import org.pgpainless.key.selection.key.impl.EncryptionKeySelectionStrategy; import org.pgpainless.util.selection.key.impl.EncryptionKeySelectionStrategy;
import org.pgpainless.key.selection.key.impl.NoRevocation; import org.pgpainless.util.selection.key.impl.NoRevocation;
import org.pgpainless.key.selection.key.impl.SignatureKeySelectionStrategy; import org.pgpainless.util.selection.key.impl.SignatureKeySelectionStrategy;
import org.pgpainless.key.selection.key.util.And; import org.pgpainless.util.selection.key.impl.And;
import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
import org.pgpainless.util.MultiMap; import org.pgpainless.util.MultiMap;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;

View file

@ -33,8 +33,8 @@ import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.exception.SecretKeyNotFoundException; import org.pgpainless.exception.SecretKeyNotFoundException;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
import org.pgpainless.util.MultiMap; import org.pgpainless.util.MultiMap;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
@ -218,7 +218,7 @@ public interface EncryptionBuilderInterface {
DocumentType signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings); DocumentType signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings);
/** /**
* Pass in a map of secret keys for signing, as well as a {@link org.pgpainless.key.selection.key.SecretKeySelectionStrategy} * Pass in a map of secret keys for signing, as well as a {@link org.pgpainless.util.selection.key.SecretKeySelectionStrategy}
* that is used to determine suitable secret keys. * that is used to determine suitable secret keys.
* If the keys are locked by a password, the provided {@link SecretKeyRingProtector} will be used to unlock the keys. * If the keys are locked by a password, the provided {@link SecretKeyRingProtector} will be used to unlock the keys.
* *

View file

@ -17,6 +17,7 @@ package org.pgpainless.key.info;
import static org.pgpainless.key.util.SignatureUtils.getLatestValidSignature; import static org.pgpainless.key.util.SignatureUtils.getLatestValidSignature;
import static org.pgpainless.key.util.SignatureUtils.sortByCreationTimeAscending; import static org.pgpainless.key.util.SignatureUtils.sortByCreationTimeAscending;
import static org.pgpainless.util.CollectionUtils.iteratorToList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -376,13 +377,4 @@ public class KeyRingInfo {
} }
return self.getCreationTime().after(binding.getCreationTime()) ? self : binding; return self.getCreationTime().after(binding.getCreationTime()) ? self : binding;
} }
private static <I> List<I> iteratorToList(Iterator<I> iterator) {
List<I> items = new ArrayList<>();
while (iterator.hasNext()) {
Object o = iterator.next();
items.add((I) o);
}
return items;
}
} }

View file

@ -40,10 +40,10 @@ import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.bouncycastle.openpgp.PGPUtil; import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.impl.NoRevocation; import org.pgpainless.util.selection.key.impl.NoRevocation;
import org.pgpainless.key.selection.key.impl.KeyBelongsToKeyRing; import org.pgpainless.util.selection.key.impl.KeyBelongsToKeyRing;
import org.pgpainless.key.selection.key.util.And; import org.pgpainless.util.selection.key.impl.And;
public class BCUtil { public class BCUtil {

View file

@ -0,0 +1,32 @@
/*
* Copyright 2021 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;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CollectionUtils {
public static <I> List<I> iteratorToList(Iterator<I> iterator) {
List<I> items = new ArrayList<>();
while (iterator.hasNext()) {
Object o = iterator.next();
items.add((I) o);
}
return items;
}
}

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.util; package org.pgpainless.util.selection.key.impl;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -22,8 +22,8 @@ import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
public class And { public class And {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -22,7 +22,7 @@ import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
/** /**
* Key Selection Strategy that only accepts {@link PGPPublicKey}s which are capable of encryption. * Key Selection Strategy that only accepts {@link PGPPublicKey}s which are capable of encryption.

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import java.util.Iterator; import java.util.Iterator;
@ -21,8 +21,8 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
/** /**
* Selection Strategy that accepts a key if it carries all of the specified key flags. * Selection Strategy that accepts a key if it carries all of the specified key flags.

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import java.util.Iterator; import java.util.Iterator;
@ -21,8 +21,8 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
/** /**
* Selection Strategies that accept a key if it carries at least one of the given key flags. * Selection Strategies that accept a key if it carries at least one of the given key flags.

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Arrays; import java.util.Arrays;
@ -25,7 +25,7 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
public class KeyBelongsToKeyRing { public class KeyBelongsToKeyRing {

View file

@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
/** /**
* Key Selection Strategies that do accept only keys, which have no revocation. * Key Selection Strategies that do accept only keys, which have no revocation.

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.util; package org.pgpainless.util.selection.key.impl;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -22,8 +22,8 @@ import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
public class Or { public class Or {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -22,7 +22,7 @@ import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
/** /**
* Key Selection Strategy that only accepts {@link PGPSecretKey}s which are capable of signing. * Key Selection Strategy that only accepts {@link PGPSecretKey}s which are capable of signing.

View file

@ -16,4 +16,4 @@
/** /**
* Implementations of Key Selection Strategies. * Implementations of Key Selection Strategies.
*/ */
package org.pgpainless.key.selection.key.impl; package org.pgpainless.util.selection.key.impl;

View file

@ -16,4 +16,4 @@
/** /**
* Different Key Selection Strategies. * Different Key Selection Strategies.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import java.util.Set; import java.util.Set;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.HashSet; import java.util.HashSet;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.HashSet; import java.util.HashSet;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
public class Email { public class Email {

View file

@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
import java.util.Iterator; import java.util.Iterator;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
public class ExactUserId { public class ExactUserId {

View file

@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Iterator; import java.util.Iterator;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.util.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; import org.pgpainless.util.selection.key.SecretKeySelectionStrategy;
public class PartialUserId { public class PartialUserId {

View file

@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
import org.pgpainless.util.MultiMap; import org.pgpainless.util.MultiMap;
public class Whitelist { public class Whitelist {

View file

@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
public class Wildcard { public class Wildcard {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;

View file

@ -16,4 +16,4 @@
/** /**
* Implementations of Key Ring Selection Strategies. * Implementations of Key Ring Selection Strategies.
*/ */
package org.pgpainless.key.selection.keyring.impl; package org.pgpainless.util.selection.keyring.impl;

View file

@ -16,4 +16,4 @@
/** /**
* Different Key Ring Selection Strategies. * Different Key Ring Selection Strategies.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;

View file

@ -0,0 +1,86 @@
/*
* Copyright 2021 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.userid;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.pgpainless.PGPainless;
public abstract class UserIdSelectionStrategy {
protected abstract boolean accept(String userId);
public List<String> selectUserIds(PGPKeyRing keyRing) {
List<String> userIds = PGPainless.inspectKeyRing(keyRing).getValidUserIds();
return selectUserIds(userIds);
}
public List<String> selectUserIds(List<String> userIds) {
List<String> selected = new ArrayList<>();
for (String userId : userIds) {
if (accept(userId)) {
selected.add(userId);
}
}
return selected;
}
public String firstMatch(PGPKeyRing keyRing) {
return firstMatch(selectUserIds(keyRing));
}
public String firstMatch(List<String> userIds) {
for (String userId : userIds) {
if (accept(userId)) {
return userId;
}
}
return null;
}
public static UserIdSelectionStrategy containsSubstring(String query) {
return new UserIdSelectionStrategy() {
@Override
protected boolean accept(String userId) {
return userId.contains(query);
}
};
}
public static UserIdSelectionStrategy exactMatch(String query) {
return new UserIdSelectionStrategy() {
@Override
protected boolean accept(String userId) {
return userId.equals(query);
}
};
}
public static UserIdSelectionStrategy startsWith(String substring) {
return new UserIdSelectionStrategy() {
@Override
protected boolean accept(String userId) {
return userId.startsWith(substring);
}
};
}
public static UserIdSelectionStrategy containsEmailAddress(String email) {
return containsSubstring(email.matches("^<.+>$") ? email : '<' + email + '>');
}
}

View file

@ -14,6 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Implementations of utility Key Selection Strategies. * UserID selection strategies.
*/ */
package org.pgpainless.key.selection.key.util; package org.pgpainless.util.selection.userid;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -28,9 +28,9 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.key.impl.EncryptionKeySelectionStrategy; import org.pgpainless.util.selection.key.impl.EncryptionKeySelectionStrategy;
import org.pgpainless.key.selection.key.impl.HasAnyKeyFlagSelectionStrategy; import org.pgpainless.util.selection.key.impl.HasAnyKeyFlagSelectionStrategy;
import org.pgpainless.key.selection.key.util.Or; import org.pgpainless.util.selection.key.impl.Or;
public class AndOrSelectionStrategyTest { public class AndOrSelectionStrategyTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -29,7 +29,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.key.impl.KeyBelongsToKeyRing; import org.pgpainless.util.selection.key.impl.KeyBelongsToKeyRing;
public class KeyBelongsToKeyRingTest { public class KeyBelongsToKeyRingTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.key; package org.pgpainless.util.selection.key;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -39,8 +39,8 @@ import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.ecc.EllipticCurve; import org.pgpainless.key.generation.type.ecc.EllipticCurve;
import org.pgpainless.key.generation.type.eddsa.EdDSACurve; import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.key.generation.type.xdh.XDHCurve; import org.pgpainless.key.generation.type.xdh.XDHCurve;
import org.pgpainless.key.selection.key.impl.HasAllKeyFlagSelectionStrategy; import org.pgpainless.util.selection.key.impl.HasAllKeyFlagSelectionStrategy;
import org.pgpainless.key.selection.key.impl.HasAnyKeyFlagSelectionStrategy; import org.pgpainless.util.selection.key.impl.HasAnyKeyFlagSelectionStrategy;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
public class KeyFlagBasedSelectionStrategyTest { public class KeyFlagBasedSelectionStrategyTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -25,7 +25,7 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.Email; import org.pgpainless.util.selection.keyring.impl.Email;
public class EmailKeyRingSelectionStrategyTest { public class EmailKeyRingSelectionStrategyTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
@ -30,7 +30,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.ExactUserId; import org.pgpainless.util.selection.keyring.impl.ExactUserId;
import org.pgpainless.util.MultiMap; import org.pgpainless.util.MultiMap;
public class KeyRingsFromCollectionTest { public class KeyRingsFromCollectionTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -29,7 +29,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.Whitelist; import org.pgpainless.util.selection.keyring.impl.Whitelist;
public class WhitelistKeyRingSelectionStrategyTest { public class WhitelistKeyRingSelectionStrategyTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -24,7 +24,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.Wildcard; import org.pgpainless.util.selection.keyring.impl.Wildcard;
public class WildcardKeyRingSelectionStrategyTest { public class WildcardKeyRingSelectionStrategyTest {

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.pgpainless.key.selection.keyring; package org.pgpainless.util.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -25,7 +25,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys; import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.XMPP; import org.pgpainless.util.selection.keyring.impl.XMPP;
public class XmppKeyRingSelectionStrategyTest { public class XmppKeyRingSelectionStrategyTest {