1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-29 07:42:06 +01:00

Simplify Key Generation API even more

This commit is contained in:
Paul Schaub 2018-06-04 19:44:47 +02:00
parent 4d1e5dc361
commit cbb3dd642a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 41 additions and 46 deletions

View file

@ -44,11 +44,21 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
private String userId; private String userId;
private char[] passphrase; private char[] passphrase;
/**
* Creates a simple RSA KeyPair of length {@code length} with user-id {@code userId}.
*
* @param userId user id.
* @param length length in bits.
* @return {@link PGPSecretKeyRing} containing the KeyPair.
* @throws PGPException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws InvalidAlgorithmParameterException
*/
public PGPSecretKeyRing simpleRsaKeyRing(String userId, RsaLength length) public PGPSecretKeyRing simpleRsaKeyRing(String userId, RsaLength length)
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
return withMasterKey( return withMasterKey(
KeySpec.getBuilder() KeySpec.getBuilder(RSA_GENERAL.withLength(length))
.ofType(RSA_GENERAL.withLength(length))
.withDefaultKeyFlags() .withDefaultKeyFlags()
.withDefaultAlgorithms()) .withDefaultAlgorithms())
.withPrimaryUserId(userId) .withPrimaryUserId(userId)
@ -59,13 +69,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
public PGPSecretKeyRing simpleEcKeyRing(String userId) public PGPSecretKeyRing simpleEcKeyRing(String userId)
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
return withSubKey( return withSubKey(
KeySpec.getBuilder() KeySpec.getBuilder(ECDH.fromCurve(EllipticCurve._P256))
.ofType(ECDH.fromCurve(EllipticCurve._P256))
.withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS) .withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)
.withDefaultAlgorithms()) .withDefaultAlgorithms())
.withMasterKey( .withMasterKey(
KeySpec.getBuilder() KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256))
.ofType(ECDSA.fromCurve(EllipticCurve._P256))
.withKeyFlags(KeyFlag.AUTHENTICATION, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA) .withKeyFlags(KeyFlag.AUTHENTICATION, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)
.withDefaultAlgorithms()) .withDefaultAlgorithms())
.withPrimaryUserId(userId) .withPrimaryUserId(userId)

View file

@ -30,7 +30,7 @@ public class KeySpec {
return inheritedSubPackets; return inheritedSubPackets;
} }
public static KeySpecBuilder getBuilder() { public static KeySpecBuilder getBuilder(KeyType type) {
return new KeySpecBuilder(); return new KeySpecBuilder(type);
} }
} }

View file

@ -1,7 +1,5 @@
package de.vanitasvitae.crypto.pgpainless.key.generation; package de.vanitasvitae.crypto.pgpainless.key.generation;
import java.util.Arrays;
import de.vanitasvitae.crypto.pgpainless.algorithm.AlgorithmSuite; import de.vanitasvitae.crypto.pgpainless.algorithm.AlgorithmSuite;
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm; import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.Feature; import de.vanitasvitae.crypto.pgpainless.algorithm.Feature;
@ -17,38 +15,33 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
private KeyType type; private KeyType type;
private PGPSignatureSubpacketGenerator hashedSubPackets = new PGPSignatureSubpacketGenerator(); private PGPSignatureSubpacketGenerator hashedSubPackets = new PGPSignatureSubpacketGenerator();
@Override KeySpecBuilder(KeyType type) {
public WithKeyFlags ofType(KeyType type) { this.type = type;
KeySpecBuilder.this.type = type;
return new WithKeyFlagsImpl();
} }
class WithKeyFlagsImpl implements WithKeyFlags { @Override
public WithDetailedConfiguration withKeyFlags(KeyFlag... flags) {
@Override int val = 0;
public WithDetailedConfiguration withKeyFlags(KeyFlag... flags) { for (KeyFlag f : flags) {
int val = 0; val |= f.getFlag();
for (KeyFlag f : flags) {
val |= f.getFlag();
}
KeySpecBuilder.this.hashedSubPackets.setKeyFlags(false, val);
return new WithDetailedConfigurationImpl();
} }
this.hashedSubPackets.setKeyFlags(false, val);
return new WithDetailedConfigurationImpl();
}
@Override @Override
public WithDetailedConfiguration withDefaultKeyFlags() { public WithDetailedConfiguration withDefaultKeyFlags() {
return withKeyFlags( return withKeyFlags(
KeyFlag.CERTIFY_OTHER, KeyFlag.CERTIFY_OTHER,
KeyFlag.SIGN_DATA, KeyFlag.SIGN_DATA,
KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_COMMS,
KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_STORAGE,
KeyFlag.AUTHENTICATION); KeyFlag.AUTHENTICATION);
} }
@Override @Override
public KeySpec withInheritedSubPackets() { public KeySpec withInheritedSubPackets() {
return new KeySpec(type, null, true); return new KeySpec(type, null, true);
}
} }
class WithDetailedConfigurationImpl implements WithDetailedConfiguration { class WithDetailedConfigurationImpl implements WithDetailedConfiguration {

View file

@ -5,20 +5,14 @@ import de.vanitasvitae.crypto.pgpainless.algorithm.Feature;
import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm; import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.KeyFlag; import de.vanitasvitae.crypto.pgpainless.algorithm.KeyFlag;
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm; import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
import de.vanitasvitae.crypto.pgpainless.key.generation.type.KeyType;
public interface KeySpecBuilderInterface { public interface KeySpecBuilderInterface {
WithKeyFlags ofType(KeyType type); WithDetailedConfiguration withKeyFlags(KeyFlag... flags);
interface WithKeyFlags { WithDetailedConfiguration withDefaultKeyFlags();
WithDetailedConfiguration withKeyFlags(KeyFlag... flags); KeySpec withInheritedSubPackets();
WithDetailedConfiguration withDefaultKeyFlags();
KeySpec withInheritedSubPackets();
}
interface WithDetailedConfiguration { interface WithDetailedConfiguration {