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:
parent
7bfa358bb3
commit
20f32926bb
2 changed files with 27 additions and 0 deletions
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue