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

Add SignatureUtils.getSignaturesForUserIdBy(key, userId, keyId)

This commit is contained in:
Paul Schaub 2022-05-17 18:31:10 +02:00
parent 77d010ec94
commit 1a37058c66

View file

@ -9,7 +9,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@ -40,6 +42,8 @@ import org.pgpainless.key.util.RevocationAttributes;
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
import org.pgpainless.util.ArmorUtils;
import javax.annotation.Nonnull;
/**
* Utility methods related to signatures.
*/
@ -333,4 +337,36 @@ public final class SignatureUtils {
return fp.equals(issuerFp);
}
/**
* Extract all signatures from the given <pre>key</pre> which were issued by <pre>issuerKeyId</pre>
* over <pre>userId</pre>.
*
* @param key public key
* @param userId user-id
* @param issuerKeyId issuer key-id
* @return (potentially empty) list of signatures
*/
public static @Nonnull List<PGPSignature> getSignaturesOverUserIdBy(
@Nonnull PGPPublicKey key,
@Nonnull String userId,
long issuerKeyId) {
List<PGPSignature> signaturesByKeyId = new ArrayList<>();
Iterator<PGPSignature> userIdSignatures = key.getSignaturesForID(userId);
// getSignaturesForID() is nullable -.-
if (userIdSignatures == null) {
return signaturesByKeyId;
}
// filter for signatures by key-id
while (userIdSignatures.hasNext()) {
PGPSignature signature = userIdSignatures.next();
if (signature.getKeyID() == issuerKeyId) {
signaturesByKeyId.add(signature);
}
}
return Collections.unmodifiableList(signaturesByKeyId);
}
}