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 d16f82bb..3e7ec4d3 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 @@ -54,10 +54,7 @@ import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.SignatureType; import org.pgpainless.key.collection.PGPKeyRing; -import org.pgpainless.key.generation.type.ECDH; -import org.pgpainless.key.generation.type.ECDSA; import org.pgpainless.key.generation.type.KeyType; -import org.pgpainless.key.generation.type.RSA; import org.pgpainless.key.generation.type.curve.EllipticCurve; import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.provider.ProviderFactory; @@ -108,7 +105,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { WithAdditionalUserIdOrPassphrase builder = this .withMasterKey( - KeySpec.getBuilder(RSA.withLength(length)) + KeySpec.getBuilder(KeyType.RSA(length)) .withDefaultKeyFlags() .withDefaultAlgorithms()) .withPrimaryUserId(userId); @@ -156,11 +153,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { WithAdditionalUserIdOrPassphrase builder = this .withSubKey( - KeySpec.getBuilder(ECDH.fromCurve(EllipticCurve._P256)) + KeySpec.getBuilder(KeyType.ECDH(EllipticCurve._P256)) .withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS) .withDefaultAlgorithms()) .withMasterKey( - KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256)) + KeySpec.getBuilder(KeyType.ECDSA(EllipticCurve._P256)) .withKeyFlags(KeyFlag.AUTHENTICATION, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA) .withDefaultAlgorithms()) .withPrimaryUserId(userId); diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java index 72f181da..e12506d3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java @@ -18,6 +18,8 @@ package org.pgpainless.key.generation.type; import java.security.spec.AlgorithmParameterSpec; import org.pgpainless.algorithm.PublicKeyAlgorithm; +import org.pgpainless.key.generation.type.curve.EllipticCurve; +import org.pgpainless.key.generation.type.length.RsaLength; public interface KeyType { @@ -26,4 +28,18 @@ public interface KeyType { PublicKeyAlgorithm getAlgorithm(); AlgorithmParameterSpec getAlgorithmSpec(); + + static KeyType RSA(RsaLength length) { + return RSA.withLength(length); + } + + static KeyType ECDH(EllipticCurve curve) { + return ECDH.fromCurve(curve); + } + + static KeyType ECDSA(EllipticCurve curve) { + return ECDSA.fromCurve(curve); + } + + // TODO: Decide, if we want to add ElGamal here as well? } diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java index 81d1ebc3..008dbe69 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java @@ -47,7 +47,7 @@ import org.pgpainless.key.TestKeys; import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.type.ElGamal_GENERAL; -import org.pgpainless.key.generation.type.RSA; +import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.length.ElGamalLength; import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.key.protection.SecretKeyRingProtector; @@ -74,7 +74,7 @@ public class EncryptDecryptTest { PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072); PGPKeyRing recipient = PGPainless.generateKeyRing() .withSubKey(KeySpec.getBuilder(ElGamal_GENERAL.withLength(ElGamalLength._3072)).withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS).withDefaultAlgorithms()) - .withMasterKey(KeySpec.getBuilder(RSA.withLength(RsaLength._4096)).withKeyFlags(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER).withDefaultAlgorithms()) + .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._4096)).withKeyFlags(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER).withDefaultAlgorithms()) .withPrimaryUserId("juliet@capulet.lit").withoutPassphrase().build(); encryptDecryptForSecretKeyRings(sender, recipient); 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 7a8a6e5b..0e53381d 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 @@ -29,7 +29,7 @@ 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; +import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.length.RsaLength; public class GenerateKeyWithAdditionalUserIdTest { @@ -37,7 +37,7 @@ public class GenerateKeyWithAdditionalUserIdTest { @Test public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { PGPKeyRing keyRing = PGPainless.generateKeyRing() - .withMasterKey(KeySpec.getBuilder(RSA.withLength(RsaLength._3072)) + .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)) .withDefaultKeyFlags() .withDefaultAlgorithms()) .withPrimaryUserId("primary@user.id") diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java index d31f27cf..ea57fb81 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java @@ -24,7 +24,7 @@ import java.security.NoSuchAlgorithmException; import org.bouncycastle.openpgp.PGPException; import org.junit.Test; import org.pgpainless.PGPainless; -import org.pgpainless.key.generation.type.RSA; +import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.util.Passphrase; @@ -40,7 +40,7 @@ public class GenerateWithEmptyPassphrase { @Test public void testGeneratingKeyWithEmptyPassphraseDoesNotThrow() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { assertNotNull(PGPainless.generateKeyRing() - .withMasterKey(KeySpec.getBuilder(RSA.withLength(RsaLength._3072)) + .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)) .withDefaultKeyFlags() .withDefaultAlgorithms()) .withPrimaryUserId("primary@user.id") diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java index 0fcb0b3b..6bc6f6ff 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java @@ -38,7 +38,7 @@ import org.junit.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.generation.KeySpec; -import org.pgpainless.key.generation.type.RSA; +import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.length.RsaLength; public class BCUtilTest { @@ -50,8 +50,8 @@ public class BCUtilTest { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException { PGPKeyRing ring = PGPainless.generateKeyRing() - .withSubKey(KeySpec.getBuilder(RSA.withLength(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) - .withMasterKey(KeySpec.getBuilder(RSA.withLength(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) + .withSubKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) + .withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms()) .withPrimaryUserId("donald@duck.tails").withoutPassphrase().build(); PGPSecretKeyRing sec = ring.getSecretKeys(); PGPPublicKeyRing pub = ring.getPublicKeys();