1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-29 15:52:08 +01:00

Add Passphrase.fromPassword() and PasswordBasedSecretKeyRingProtector.forKey() factory methods

This commit is contained in:
Paul Schaub 2020-08-31 12:26:07 +02:00
parent 7bfa358bb3
commit 20f32926bb
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 27 additions and 0 deletions

View file

@ -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) {

View file

@ -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.
*/