1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-25 22:02:05 +01:00

Simplify key template methods by replacing String and UserID args with CharSequence

This commit is contained in:
Paul Schaub 2023-05-03 13:51:59 +02:00
parent 9c81137f48
commit 8869d9bd78
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -17,7 +17,6 @@ import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.eddsa.EdDSACurve; import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.generation.type.xdh.XDHSpec; import org.pgpainless.key.generation.type.xdh.XDHSpec;
import org.pgpainless.key.util.UserId;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
public final class KeyRingTemplates { public final class KeyRingTemplates {
@ -111,46 +110,20 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing simpleRsaKeyRing(@Nullable UserId userId, @Nonnull RsaLength length) public PGPSecretKeyRing simpleRsaKeyRing(@Nullable CharSequence userId, @Nonnull RsaLength length)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId == null ? null : userId.toString(), length);
}
/**
* Creates a simple, unencrypted RSA KeyPair of length {@code length} with user-id {@code userId}.
* The KeyPair consists of a single RSA master key which is used for signing, encryption and certification.
*
* @param userId user id.
* @param length length in bits.
*
* @return {@link PGPSecretKeyRing} containing the KeyPair.
*
* @throws InvalidAlgorithmParameterException in case of invalid key generation parameters
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error
*/
public PGPSecretKeyRing simpleRsaKeyRing(@Nullable String userId, @Nonnull RsaLength length)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId, length, Passphrase.emptyPassphrase()); return simpleRsaKeyRing(userId, length, Passphrase.emptyPassphrase());
} }
/** public PGPSecretKeyRing simpleRsaKeyRing(@Nullable CharSequence userId, @Nonnull RsaLength length, @Nonnull Passphrase passphrase)
* Creates a simple RSA KeyPair of length {@code length} with user-id {@code userId}. throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
* The KeyPair consists of a single RSA master key which is used for signing, encryption and certification. KeyRingBuilder builder = PGPainless.buildKeyRing()
* .setPrimaryKey(KeySpec.getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS))
* @param userId user id. .setPassphrase(passphrase);
* @param length length in bits. if (userId != null) {
* @param password Password of the key. Can be null for unencrypted keys. builder.addUserId(userId.toString());
* }
* @return {@link PGPSecretKeyRing} containing the KeyPair. return builder.build();
*
* @throws InvalidAlgorithmParameterException in case of invalid key generation parameters
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error
*/
public PGPSecretKeyRing simpleRsaKeyRing(@Nullable UserId userId, @Nonnull RsaLength length, @Nullable String password)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleRsaKeyRing(userId == null ? null : userId.toString(), length, password);
} }
/** /**
@ -167,7 +140,7 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing simpleRsaKeyRing(@Nullable String userId, @Nonnull RsaLength length, @Nullable String password) public PGPSecretKeyRing simpleRsaKeyRing(@Nullable CharSequence userId, @Nonnull RsaLength length, @Nullable String password)
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Passphrase passphrase = Passphrase.emptyPassphrase(); Passphrase passphrase = Passphrase.emptyPassphrase();
if (!isNullOrEmpty(password)) { if (!isNullOrEmpty(password)) {
@ -176,17 +149,6 @@ public final class KeyRingTemplates {
return simpleRsaKeyRing(userId, length, passphrase); return simpleRsaKeyRing(userId, length, passphrase);
} }
public PGPSecretKeyRing simpleRsaKeyRing(@Nullable String userId, @Nonnull RsaLength length, @Nonnull Passphrase passphrase)
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
KeyRingBuilder builder = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS))
.setPassphrase(passphrase);
if (userId != null) {
builder.addUserId(userId);
}
return builder.build();
}
/** /**
* Creates a key ring consisting of an ed25519 EdDSA primary key and a curve25519 XDH subkey. * Creates a key ring consisting of an ed25519 EdDSA primary key and a curve25519 XDH subkey.
* The EdDSA primary key is used for signing messages and certifying the sub key. * The EdDSA primary key is used for signing messages and certifying the sub key.
@ -200,48 +162,11 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing simpleEcKeyRing(@Nullable UserId userId) public PGPSecretKeyRing simpleEcKeyRing(@Nullable CharSequence userId)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId == null ? null : userId.toString());
}
/**
* Creates a key ring consisting of an ed25519 EdDSA primary key and a curve25519 XDH subkey.
* The EdDSA primary key is used for signing messages and certifying the sub key.
* The XDH subkey is used for encryption and decryption of messages.
*
* @param userId user-id
*
* @return {@link PGPSecretKeyRing} containing the key pairs.
*
* @throws InvalidAlgorithmParameterException in case of invalid key generation parameters
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error
*/
public PGPSecretKeyRing simpleEcKeyRing(@Nullable String userId)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId, Passphrase.emptyPassphrase()); return simpleEcKeyRing(userId, Passphrase.emptyPassphrase());
} }
/**
* Creates a key ring consisting of an ed25519 EdDSA primary key and a curve25519 XDH subkey.
* The EdDSA primary key is used for signing messages and certifying the sub key.
* The XDH subkey is used for encryption and decryption of messages.
*
* @param userId user-id
* @param password Password of the private key. Can be null for an unencrypted key.
*
* @return {@link PGPSecretKeyRing} containing the key pairs.
*
* @throws InvalidAlgorithmParameterException in case of invalid key generation parameters
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error
*/
public PGPSecretKeyRing simpleEcKeyRing(@Nullable UserId userId, String password)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
return simpleEcKeyRing(userId == null ? null : userId.toString(), password);
}
/** /**
* Creates a key ring consisting of an ed25519 EdDSA primary key and a X25519 XDH subkey. * Creates a key ring consisting of an ed25519 EdDSA primary key and a X25519 XDH subkey.
* The EdDSA primary key is used for signing messages and certifying the sub key. * The EdDSA primary key is used for signing messages and certifying the sub key.
@ -256,7 +181,7 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing simpleEcKeyRing(@Nullable String userId, String password) public PGPSecretKeyRing simpleEcKeyRing(@Nullable CharSequence userId, String password)
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Passphrase passphrase = Passphrase.emptyPassphrase(); Passphrase passphrase = Passphrase.emptyPassphrase();
if (!isNullOrEmpty(password)) { if (!isNullOrEmpty(password)) {
@ -265,14 +190,14 @@ public final class KeyRingTemplates {
return simpleEcKeyRing(userId, passphrase); return simpleEcKeyRing(userId, passphrase);
} }
public PGPSecretKeyRing simpleEcKeyRing(@Nullable String userId, @Nonnull Passphrase passphrase) public PGPSecretKeyRing simpleEcKeyRing(@Nullable CharSequence userId, @Nonnull Passphrase passphrase)
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
KeyRingBuilder builder = PGPainless.buildKeyRing() KeyRingBuilder builder = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)) .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
.addSubkey(KeySpec.getBuilder(KeyType.XDH(XDHSpec._X25519), KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)) .addSubkey(KeySpec.getBuilder(KeyType.XDH(XDHSpec._X25519), KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS))
.setPassphrase(passphrase); .setPassphrase(passphrase);
if (userId != null) { if (userId != null) {
builder.addUserId(userId); builder.addUserId(userId.toString());
} }
return builder.build(); return builder.build();
} }
@ -288,7 +213,7 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing modernKeyRing(@Nullable String userId) throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { public PGPSecretKeyRing modernKeyRing(@Nullable CharSequence userId) throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
return modernKeyRing(userId, Passphrase.emptyPassphrase()); return modernKeyRing(userId, Passphrase.emptyPassphrase());
} }
@ -304,13 +229,13 @@ public final class KeyRingTemplates {
* @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider * @throws NoSuchAlgorithmException in case of missing algorithm implementation in the crypto provider
* @throws PGPException in case of an OpenPGP related error * @throws PGPException in case of an OpenPGP related error
*/ */
public PGPSecretKeyRing modernKeyRing(@Nullable String userId, @Nullable String password) public PGPSecretKeyRing modernKeyRing(@Nullable CharSequence userId, @Nullable String password)
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
Passphrase passphrase = (password != null ? Passphrase.fromPassword(password) : Passphrase.emptyPassphrase()); Passphrase passphrase = (password != null ? Passphrase.fromPassword(password) : Passphrase.emptyPassphrase());
return modernKeyRing(userId, passphrase); return modernKeyRing(userId, passphrase);
} }
public PGPSecretKeyRing modernKeyRing(@Nullable String userId, @Nonnull Passphrase passphrase) public PGPSecretKeyRing modernKeyRing(@Nullable CharSequence userId, @Nonnull Passphrase passphrase)
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
KeyRingBuilder builder = PGPainless.buildKeyRing() KeyRingBuilder builder = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER)) .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.CERTIFY_OTHER))
@ -318,7 +243,7 @@ public final class KeyRingTemplates {
.addSubkey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.SIGN_DATA)) .addSubkey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519), KeyFlag.SIGN_DATA))
.setPassphrase(passphrase); .setPassphrase(passphrase);
if (userId != null) { if (userId != null) {
builder.addUserId(userId); builder.addUserId(userId.toString());
} }
return builder.build(); return builder.build();
} }