2020-10-16 12:46:58 +02:00
|
|
|
package org.pgpainless.key.generation;
|
|
|
|
|
2020-10-16 13:18:07 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
2020-10-16 12:46:58 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2020-10-16 13:18:07 +02:00
|
|
|
import java.util.Iterator;
|
2020-10-16 12:46:58 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.pgpainless.PGPainless;
|
|
|
|
import org.pgpainless.key.collection.PGPKeyRing;
|
|
|
|
import org.pgpainless.key.generation.type.RSA_SIGN;
|
|
|
|
import org.pgpainless.key.generation.type.length.RsaLength;
|
|
|
|
|
|
|
|
public class GenerateKeyWithAdditionalUserIdTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
|
|
|
PGPKeyRing keyRing = PGPainless.generateKeyRing()
|
|
|
|
.withMasterKey(KeySpec.getBuilder(RSA_SIGN.withLength(RsaLength._3072))
|
|
|
|
.withDefaultKeyFlags()
|
|
|
|
.withDefaultAlgorithms())
|
|
|
|
.withPrimaryUserId("primary@user.id")
|
|
|
|
.withAdditionalUserId("additional@user.id")
|
2020-10-20 22:14:56 +02:00
|
|
|
.withAdditionalUserId("additional2@user.id")
|
2020-10-16 12:46:58 +02:00
|
|
|
.withoutPassphrase()
|
|
|
|
.build();
|
|
|
|
|
2020-10-16 13:18:07 +02:00
|
|
|
Iterator<String> userIds = keyRing.getPublicKeys().getPublicKey().getUserIDs();
|
|
|
|
assertEquals("primary@user.id", userIds.next());
|
|
|
|
assertEquals("additional@user.id", userIds.next());
|
2020-10-20 22:14:56 +02:00
|
|
|
assertEquals("additional2@user.id", userIds.next());
|
2020-10-16 13:18:07 +02:00
|
|
|
assertFalse(userIds.hasNext());
|
|
|
|
|
2020-10-16 12:46:58 +02:00
|
|
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
|
|
|
ArmoredOutputStream armor = new ArmoredOutputStream(byteOut);
|
|
|
|
keyRing.getSecretKeys().encode(armor);
|
|
|
|
armor.close();
|
|
|
|
|
2020-10-16 13:18:07 +02:00
|
|
|
// echo this | gpg --list-packets
|
2020-10-16 12:46:58 +02:00
|
|
|
System.out.println(byteOut.toString("UTF-8"));
|
|
|
|
}
|
|
|
|
}
|