diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java index e7d384ef..398b369d 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java @@ -24,11 +24,12 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Objects; +import java.util.Set; import javax.annotation.Nonnull; -import org.bouncycastle.asn1.ocsp.Signature; -import org.bouncycastle.bcpg.SignatureSubpacket; import org.bouncycastle.openpgp.PGPEncryptedData; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPKeyPair; @@ -45,7 +46,6 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.PGPDigestCalculator; -import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; @@ -53,7 +53,6 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.KeyFlag; -import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.generation.type.ECDH; import org.pgpainless.key.generation.type.ECDSA; @@ -70,7 +69,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { private List keySpecs = new ArrayList<>(); private String userId; - private List additionalUserIds = new ArrayList<>(); + private Set additionalUserIds = new LinkedHashSet<>(); private Passphrase passphrase; /** @@ -216,6 +215,9 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { @Override public WithAdditionalUserIdOrPassphrase withAdditionalUserId(@Nonnull String userId) { + if (Objects.equals(KeyRingBuilder.this.userId, userId)) { + throw new IllegalArgumentException("Additional user-id MUST NOT be equal to primary user-id."); + } KeyRingBuilder.this.additionalUserIds.add(userId); return this; } diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java index 97931065..425bc955 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java @@ -27,12 +27,14 @@ public class GenerateKeyWithAdditionalUserIdTest { .withDefaultAlgorithms()) .withPrimaryUserId("primary@user.id") .withAdditionalUserId("additional@user.id") + .withAdditionalUserId("additional2@user.id") .withoutPassphrase() .build(); Iterator userIds = keyRing.getPublicKeys().getPublicKey().getUserIDs(); assertEquals("primary@user.id", userIds.next()); assertEquals("additional@user.id", userIds.next()); + assertEquals("additional2@user.id", userIds.next()); assertFalse(userIds.hasNext()); ByteArrayOutputStream byteOut = new ByteArrayOutputStream();