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

Encrypt to all capable subkeys by default

This commit is contained in:
Paul Schaub 2021-08-04 16:38:17 +02:00
parent 7490eb925f
commit 9a485a3354
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 20 additions and 3 deletions

View file

@ -24,6 +24,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
@ -70,7 +72,7 @@ public class EncryptionOptions {
private final Set<SubkeyIdentifier> encryptionKeys = new LinkedHashSet<>();
private final Map<SubkeyIdentifier, KeyRingInfo> keyRingInfo = new HashMap<>();
private final Map<SubkeyIdentifier, KeyAccessor> keyViews = new HashMap<>();
private final EncryptionKeySelector encryptionKeySelector = encryptToFirstSubkey();
private final EncryptionKeySelector encryptionKeySelector = encryptToAllCapableSubkeys();
private SymmetricKeyAlgorithm encryptionAlgorithmOverride = null;
@ -119,6 +121,21 @@ public class EncryptionOptions {
return this;
}
/**
* Add all key rings in the provided key ring collection as recipients.
* Per key ring, the selector is applied to select one or more encryption subkeys.
*
* @param keys keys
* @param selector encryption key selector
* @return this
*/
public EncryptionOptions addRecipients(@Nonnull PGPPublicKeyRingCollection keys, @Nonnull EncryptionKeySelector selector) {
for (PGPPublicKeyRing key : keys) {
addRecipient(key, selector);
}
return this;
}
/**
* Add a recipient by providing a key and recipient user-id.
* The user-id is used to determine the recipients preferences (algorithms etc.).

View file

@ -179,14 +179,14 @@ public class EncryptionOptionsTest {
Arrays.asList(publicKeys, secondKeyRing));
EncryptionOptions options = new EncryptionOptions();
options.addRecipients(collection);
options.addRecipients(collection, EncryptionOptions.encryptToFirstSubkey());
assertEquals(2, options.getEncryptionKeyIdentifiers().size());
}
@Test
public void testAddRecipient_withValidUserId() {
EncryptionOptions options = new EncryptionOptions();
options.addRecipient(publicKeys, "test@pgpainless.org");
options.addRecipient(publicKeys, "test@pgpainless.org", EncryptionOptions.encryptToFirstSubkey());
assertEquals(1, options.getEncryptionMethods().size());
}