mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 12:58:02 +01:00
Annotate EncryptionOptions methods with @Nonnull
This commit is contained in:
parent
304350fe5c
commit
64c6d7a904
2 changed files with 23 additions and 17 deletions
|
@ -75,7 +75,7 @@ public class EncryptionOptions {
|
|||
this(EncryptionPurpose.ANY);
|
||||
}
|
||||
|
||||
public EncryptionOptions(EncryptionPurpose purpose) {
|
||||
public EncryptionOptions(@Nonnull EncryptionPurpose purpose) {
|
||||
this.purpose = purpose;
|
||||
}
|
||||
|
||||
|
@ -118,7 +118,7 @@ public class EncryptionOptions {
|
|||
* @param keys keys
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addRecipients(Iterable<PGPPublicKeyRing> keys) {
|
||||
public EncryptionOptions addRecipients(@Nonnull Iterable<PGPPublicKeyRing> keys) {
|
||||
if (!keys.iterator().hasNext()) {
|
||||
throw new IllegalArgumentException("Set of recipient keys cannot be empty.");
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ public class EncryptionOptions {
|
|||
* @param userId user id
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId) {
|
||||
public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key, @Nonnull CharSequence userId) {
|
||||
return addRecipient(key, userId, encryptionKeySelector);
|
||||
}
|
||||
|
||||
|
@ -167,11 +167,13 @@ public class EncryptionOptions {
|
|||
* @param encryptionKeySelectionStrategy strategy to select one or more encryption subkeys to encrypt to
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId, EncryptionKeySelector encryptionKeySelectionStrategy) {
|
||||
public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key,
|
||||
@Nonnull CharSequence userId,
|
||||
@Nonnull EncryptionKeySelector encryptionKeySelectionStrategy) {
|
||||
KeyRingInfo info = new KeyRingInfo(key, new Date());
|
||||
|
||||
List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy
|
||||
.selectEncryptionSubkeys(info.getEncryptionSubkeys(userId, purpose));
|
||||
.selectEncryptionSubkeys(info.getEncryptionSubkeys(userId.toString(), purpose));
|
||||
if (encryptionSubkeys.isEmpty()) {
|
||||
throw new KeyException.UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(key));
|
||||
}
|
||||
|
@ -179,7 +181,7 @@ public class EncryptionOptions {
|
|||
for (PGPPublicKey encryptionSubkey : encryptionSubkeys) {
|
||||
SubkeyIdentifier keyId = new SubkeyIdentifier(key, encryptionSubkey.getKeyID());
|
||||
keyRingInfo.put(keyId, info);
|
||||
keyViews.put(keyId, new KeyAccessor.ViaUserId(info, keyId, userId));
|
||||
keyViews.put(keyId, new KeyAccessor.ViaUserId(info, keyId, userId.toString()));
|
||||
addRecipientKey(key, encryptionSubkey);
|
||||
}
|
||||
|
||||
|
@ -192,7 +194,7 @@ public class EncryptionOptions {
|
|||
* @param key key ring
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addRecipient(PGPPublicKeyRing key) {
|
||||
public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key) {
|
||||
return addRecipient(key, encryptionKeySelector);
|
||||
}
|
||||
|
||||
|
@ -203,7 +205,8 @@ public class EncryptionOptions {
|
|||
* @param encryptionKeySelectionStrategy strategy used to select one or multiple encryption subkeys.
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addRecipient(PGPPublicKeyRing key, EncryptionKeySelector encryptionKeySelectionStrategy) {
|
||||
public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key,
|
||||
@Nonnull EncryptionKeySelector encryptionKeySelectionStrategy) {
|
||||
Date evaluationDate = new Date();
|
||||
KeyRingInfo info;
|
||||
info = new KeyRingInfo(key, evaluationDate);
|
||||
|
@ -234,7 +237,8 @@ public class EncryptionOptions {
|
|||
return this;
|
||||
}
|
||||
|
||||
private void addRecipientKey(PGPPublicKeyRing keyRing, PGPPublicKey key) {
|
||||
private void addRecipientKey(@Nonnull PGPPublicKeyRing keyRing,
|
||||
@Nonnull PGPPublicKey key) {
|
||||
encryptionKeys.add(new SubkeyIdentifier(keyRing, key.getKeyID()));
|
||||
PGPKeyEncryptionMethodGenerator encryptionMethod = ImplementationFactory
|
||||
.getInstance().getPublicKeyKeyEncryptionMethodGenerator(key);
|
||||
|
@ -247,7 +251,7 @@ public class EncryptionOptions {
|
|||
* @param passphrase passphrase
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addPassphrase(Passphrase passphrase) {
|
||||
public EncryptionOptions addPassphrase(@Nonnull Passphrase passphrase) {
|
||||
if (passphrase.isEmpty()) {
|
||||
throw new IllegalArgumentException("Passphrase must not be empty.");
|
||||
}
|
||||
|
@ -267,7 +271,7 @@ public class EncryptionOptions {
|
|||
* @param encryptionMethod encryption method
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions addEncryptionMethod(PGPKeyEncryptionMethodGenerator encryptionMethod) {
|
||||
public EncryptionOptions addEncryptionMethod(@Nonnull PGPKeyEncryptionMethodGenerator encryptionMethod) {
|
||||
encryptionMethods.add(encryptionMethod);
|
||||
return this;
|
||||
}
|
||||
|
@ -303,7 +307,7 @@ public class EncryptionOptions {
|
|||
* @param encryptionAlgorithm encryption algorithm override
|
||||
* @return this
|
||||
*/
|
||||
public EncryptionOptions overrideEncryptionAlgorithm(SymmetricKeyAlgorithm encryptionAlgorithm) {
|
||||
public EncryptionOptions overrideEncryptionAlgorithm(@Nonnull SymmetricKeyAlgorithm encryptionAlgorithm) {
|
||||
if (encryptionAlgorithm == SymmetricKeyAlgorithm.NULL) {
|
||||
throw new IllegalArgumentException("Plaintext encryption can only be used to denote unencrypted secret keys.");
|
||||
}
|
||||
|
@ -322,7 +326,7 @@ public class EncryptionOptions {
|
|||
}
|
||||
|
||||
public interface EncryptionKeySelector {
|
||||
List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys);
|
||||
List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -333,7 +337,7 @@ public class EncryptionOptions {
|
|||
public static EncryptionKeySelector encryptToFirstSubkey() {
|
||||
return new EncryptionKeySelector() {
|
||||
@Override
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
|
||||
return encryptionCapableKeys.isEmpty() ? Collections.emptyList() : Collections.singletonList(encryptionCapableKeys.get(0));
|
||||
}
|
||||
};
|
||||
|
@ -347,7 +351,7 @@ public class EncryptionOptions {
|
|||
public static EncryptionKeySelector encryptToAllCapableSubkeys() {
|
||||
return new EncryptionKeySelector() {
|
||||
@Override
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
|
||||
return encryptionCapableKeys;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -36,6 +36,8 @@ import org.pgpainless.key.generation.type.xdh.XDHSpec;
|
|||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class EncryptionOptionsTest {
|
||||
|
||||
private static PGPSecretKeyRing secretKeys;
|
||||
|
@ -149,7 +151,7 @@ public class EncryptionOptionsTest {
|
|||
assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
|
||||
() -> options.addRecipient(publicKeys, new EncryptionOptions.EncryptionKeySelector() {
|
||||
@Override
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}));
|
||||
|
@ -157,7 +159,7 @@ public class EncryptionOptionsTest {
|
|||
assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
|
||||
() -> options.addRecipient(publicKeys, "test@pgpainless.org", new EncryptionOptions.EncryptionKeySelector() {
|
||||
@Override
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
||||
public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}));
|
||||
|
|
Loading…
Reference in a new issue