1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 12:52:07 +01:00

Use passphrase in PGP key generation

This commit is contained in:
Paul Schaub 2018-07-12 23:21:09 +02:00
parent d46671e37e
commit 7272027ef1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 13 additions and 12 deletions

View file

@ -52,6 +52,7 @@ import org.pgpainless.pgpainless.key.generation.type.KeyType;
import org.pgpainless.pgpainless.key.generation.type.RSA_GENERAL; import org.pgpainless.pgpainless.key.generation.type.RSA_GENERAL;
import org.pgpainless.pgpainless.key.generation.type.curve.EllipticCurve; import org.pgpainless.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.pgpainless.util.Passphrase;
public class KeyRingBuilder implements KeyRingBuilderInterface { public class KeyRingBuilder implements KeyRingBuilderInterface {
@ -59,7 +60,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
private List<KeySpec> keySpecs = new ArrayList<>(); private List<KeySpec> keySpecs = new ArrayList<>();
private String userId; private String userId;
private char[] passphrase; private Passphrase passphrase;
/** /**
* Creates a simple RSA KeyPair of length {@code length} with user-id {@code userId}. * Creates a simple RSA KeyPair of length {@code length} with user-id {@code userId}.
@ -143,12 +144,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
class WithPassphraseImpl implements WithPassphrase { class WithPassphraseImpl implements WithPassphrase {
@Override @Override
public Build withPassphrase(String passphrase) { public Build withPassphrase(Passphrase passphrase) {
return withPassphrase(passphrase.toCharArray());
}
@Override
public Build withPassphrase(char[] passphrase) {
KeyRingBuilder.this.passphrase = passphrase; KeyRingBuilder.this.passphrase = passphrase;
return new BuildImpl(); return new BuildImpl();
} }
@ -176,7 +172,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
null : // unencrypted key pair, otherwise AES-256 encrypted null : // unencrypted key pair, otherwise AES-256 encrypted
new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator) new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
.setProvider(BouncyCastleProvider.PROVIDER_NAME) .setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build(passphrase); .build(passphrase != null ? passphrase.getChars() : null);
if (passphrase != null) {
passphrase.clear();
}
// First key is the Master Key // First key is the Master Key
KeySpec certKeySpec = keySpecs.get(0); KeySpec certKeySpec = keySpecs.get(0);

View file

@ -21,6 +21,7 @@ import java.security.NoSuchProviderException;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.pgpainless.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.pgpainless.util.Passphrase;
public interface KeyRingBuilderInterface { public interface KeyRingBuilderInterface {
@ -38,9 +39,7 @@ public interface KeyRingBuilderInterface {
interface WithPassphrase { interface WithPassphrase {
Build withPassphrase(String passphrase); Build withPassphrase(Passphrase passphrase);
Build withPassphrase(char[] passphrase);
Build withoutPassphrase(); Build withoutPassphrase();
} }

View file

@ -39,6 +39,8 @@ public class Passphrase {
} }
public char[] getChars() { public char[] getChars() {
return chars; char[] copy = new char[chars.length];
System.arraycopy(chars, 0, copy, 0, chars.length);
return copy;
} }
} }