From 20f32926bb1d72f0cc91181e904b5dbaede44dff Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 31 Aug 2020 12:26:07 +0200 Subject: [PATCH] Add Passphrase.fromPassword() and PasswordBasedSecretKeyRingProtector.forKey() factory methods --- .../PasswordBasedSecretKeyRingProtector.java | 17 +++++++++++++++++ .../java/org/pgpainless/util/Passphrase.java | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java index 56e01cd5..9232b1e4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java @@ -19,12 +19,14 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPKeyRing; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider; import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyEncryptorBuilder; import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; +import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.util.Passphrase; /** @@ -51,6 +53,21 @@ public class PasswordBasedSecretKeyRingProtector implements SecretKeyRingProtect this.passphraseProvider = passphraseProvider; } + public static PasswordBasedSecretKeyRingProtector forKey(PGPKeyRing keyRing, Passphrase passphrase) { + KeyRingProtectionSettings protectionSettings = new KeyRingProtectionSettings(SymmetricKeyAlgorithm.AES_256); + SecretKeyPassphraseProvider passphraseProvider = new SecretKeyPassphraseProvider() { + @Override + @Nullable + public Passphrase getPassphraseFor(Long keyId) { + if (keyRing.getPublicKey().getKeyID() == keyId) { + return passphrase; + } + return null; + } + }; + return new PasswordBasedSecretKeyRingProtector(protectionSettings, passphraseProvider); + } + @Override @Nullable public PBESecretKeyDecryptor getDecryptor(Long keyId) { diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java b/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java index 7d9e9411..480c584b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java @@ -34,6 +34,16 @@ public class Passphrase { this.chars = chars; } + /** + * Create a {@link Passphrase} from a {@link String}. + * + * @param password password + * @return passphrase + */ + public static Passphrase fromPassword(String password) { + return new Passphrase(password.toCharArray()); + } + /** * Overwrite the char array with spaces and mark the {@link Passphrase} as invalidated. */