mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 12:58:02 +01:00
Simplify Key Generation API even more
This commit is contained in:
parent
4d1e5dc361
commit
cbb3dd642a
4 changed files with 41 additions and 46 deletions
|
@ -44,11 +44,21 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
|
|||
private String userId;
|
||||
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)
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||
return withMasterKey(
|
||||
KeySpec.getBuilder()
|
||||
.ofType(RSA_GENERAL.withLength(length))
|
||||
KeySpec.getBuilder(RSA_GENERAL.withLength(length))
|
||||
.withDefaultKeyFlags()
|
||||
.withDefaultAlgorithms())
|
||||
.withPrimaryUserId(userId)
|
||||
|
@ -59,13 +69,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
|
|||
public PGPSecretKeyRing simpleEcKeyRing(String userId)
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||
return withSubKey(
|
||||
KeySpec.getBuilder()
|
||||
.ofType(ECDH.fromCurve(EllipticCurve._P256))
|
||||
KeySpec.getBuilder(ECDH.fromCurve(EllipticCurve._P256))
|
||||
.withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)
|
||||
.withDefaultAlgorithms())
|
||||
.withMasterKey(
|
||||
KeySpec.getBuilder()
|
||||
.ofType(ECDSA.fromCurve(EllipticCurve._P256))
|
||||
KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256))
|
||||
.withKeyFlags(KeyFlag.AUTHENTICATION, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)
|
||||
.withDefaultAlgorithms())
|
||||
.withPrimaryUserId(userId)
|
||||
|
|
|
@ -30,7 +30,7 @@ public class KeySpec {
|
|||
return inheritedSubPackets;
|
||||
}
|
||||
|
||||
public static KeySpecBuilder getBuilder() {
|
||||
return new KeySpecBuilder();
|
||||
public static KeySpecBuilder getBuilder(KeyType type) {
|
||||
return new KeySpecBuilder(type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package de.vanitasvitae.crypto.pgpainless.key.generation;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.vanitasvitae.crypto.pgpainless.algorithm.AlgorithmSuite;
|
||||
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
|
||||
import de.vanitasvitae.crypto.pgpainless.algorithm.Feature;
|
||||
|
@ -17,38 +15,33 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
|
|||
private KeyType type;
|
||||
private PGPSignatureSubpacketGenerator hashedSubPackets = new PGPSignatureSubpacketGenerator();
|
||||
|
||||
@Override
|
||||
public WithKeyFlags ofType(KeyType type) {
|
||||
KeySpecBuilder.this.type = type;
|
||||
return new WithKeyFlagsImpl();
|
||||
KeySpecBuilder(KeyType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
class WithKeyFlagsImpl implements WithKeyFlags {
|
||||
|
||||
@Override
|
||||
public WithDetailedConfiguration withKeyFlags(KeyFlag... flags) {
|
||||
int val = 0;
|
||||
for (KeyFlag f : flags) {
|
||||
val |= f.getFlag();
|
||||
}
|
||||
KeySpecBuilder.this.hashedSubPackets.setKeyFlags(false, val);
|
||||
return new WithDetailedConfigurationImpl();
|
||||
@Override
|
||||
public WithDetailedConfiguration withKeyFlags(KeyFlag... flags) {
|
||||
int val = 0;
|
||||
for (KeyFlag f : flags) {
|
||||
val |= f.getFlag();
|
||||
}
|
||||
this.hashedSubPackets.setKeyFlags(false, val);
|
||||
return new WithDetailedConfigurationImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WithDetailedConfiguration withDefaultKeyFlags() {
|
||||
return withKeyFlags(
|
||||
KeyFlag.CERTIFY_OTHER,
|
||||
KeyFlag.SIGN_DATA,
|
||||
KeyFlag.ENCRYPT_COMMS,
|
||||
KeyFlag.ENCRYPT_STORAGE,
|
||||
KeyFlag.AUTHENTICATION);
|
||||
}
|
||||
@Override
|
||||
public WithDetailedConfiguration withDefaultKeyFlags() {
|
||||
return withKeyFlags(
|
||||
KeyFlag.CERTIFY_OTHER,
|
||||
KeyFlag.SIGN_DATA,
|
||||
KeyFlag.ENCRYPT_COMMS,
|
||||
KeyFlag.ENCRYPT_STORAGE,
|
||||
KeyFlag.AUTHENTICATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeySpec withInheritedSubPackets() {
|
||||
return new KeySpec(type, null, true);
|
||||
}
|
||||
@Override
|
||||
public KeySpec withInheritedSubPackets() {
|
||||
return new KeySpec(type, null, true);
|
||||
}
|
||||
|
||||
class WithDetailedConfigurationImpl implements WithDetailedConfiguration {
|
||||
|
|
|
@ -5,20 +5,14 @@ import de.vanitasvitae.crypto.pgpainless.algorithm.Feature;
|
|||
import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm;
|
||||
import de.vanitasvitae.crypto.pgpainless.algorithm.KeyFlag;
|
||||
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import de.vanitasvitae.crypto.pgpainless.key.generation.type.KeyType;
|
||||
|
||||
public interface KeySpecBuilderInterface {
|
||||
|
||||
WithKeyFlags ofType(KeyType type);
|
||||
WithDetailedConfiguration withKeyFlags(KeyFlag... flags);
|
||||
|
||||
interface WithKeyFlags {
|
||||
WithDetailedConfiguration withDefaultKeyFlags();
|
||||
|
||||
WithDetailedConfiguration withKeyFlags(KeyFlag... flags);
|
||||
|
||||
WithDetailedConfiguration withDefaultKeyFlags();
|
||||
|
||||
KeySpec withInheritedSubPackets();
|
||||
}
|
||||
KeySpec withInheritedSubPackets();
|
||||
|
||||
interface WithDetailedConfiguration {
|
||||
|
||||
|
|
Loading…
Reference in a new issue