1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-09 22:01:10 +01:00

Add HasKeyFlagsSelectionStrategy

This commit is contained in:
Paul Schaub 2021-01-09 20:16:13 +01:00
parent 83bd157a78
commit 8df752e995
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 181 additions and 1 deletions

View file

@ -18,6 +18,7 @@ package org.pgpainless.key.selection.key.impl;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy;
/**
@ -25,8 +26,11 @@ import org.pgpainless.key.selection.key.PublicKeySelectionStrategy;
*/
public class EncryptionKeySelectionStrategy extends PublicKeySelectionStrategy {
private static final HasKeyFlagSelectionStrategy.PublicKey HAS_ENCRYPT_COMMS_FLAG =
new HasKeyFlagSelectionStrategy.PublicKey(KeyFlag.ENCRYPT_COMMS);
@Override
public boolean accept(@Nonnull PGPPublicKey key) {
return key.isEncryptionKey();
return key.isEncryptionKey() && HAS_ENCRYPT_COMMS_FLAG.accept(key);
}
}

View file

@ -0,0 +1,68 @@
/*
* 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.key.selection.key.impl;
import java.util.Iterator;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.selection.key.PublicKeySelectionStrategy;
import org.pgpainless.key.selection.key.SecretKeySelectionStrategy;
public class HasKeyFlagSelectionStrategy {
public static class PublicKey extends PublicKeySelectionStrategy {
private final int keyFlagMask;
public PublicKey(KeyFlag... flags) {
this(KeyFlag.toBitmask(flags));
}
public PublicKey(int mask) {
this.keyFlagMask = mask;
}
@Override
public boolean accept(PGPPublicKey key) {
Iterator<PGPSignature> signatures = key.getSignatures();
int flags = signatures.next().getHashedSubPackets().getKeyFlags();
return (keyFlagMask & flags) == keyFlagMask;
}
}
public static class SecretKey extends SecretKeySelectionStrategy {
private final int keyFlagMask;
public SecretKey(KeyFlag... flags) {
this(KeyFlag.toBitmask(flags));
}
public SecretKey(int mask) {
this.keyFlagMask = mask;
}
@Override
public boolean accept(PGPSecretKey key) {
Iterator<PGPSignature> signatures = key.getPublicKey().getSignatures();
int flags = signatures.next().getHashedSubPackets().getKeyFlags();
return (keyFlagMask & flags) == keyFlagMask;
}
}
}