From 59a14c291829dffb3bc21ffcdb55492a3898c99a Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 23 Jan 2021 01:09:55 +0100 Subject: [PATCH] Make ImplementationFactory methods abstract --- .../implementation/ImplementationFactory.java | 65 ++++++------------- .../JceImplementationFactory.java | 3 +- .../key/generation/KeyRingBuilder.java | 3 +- 3 files changed, 23 insertions(+), 48 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java index 6a716b6c..9fdd3deb 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java @@ -63,17 +63,13 @@ public abstract class ImplementationFactory { getPGPDigestCalculator(HashAlgorithm.SHA1), passphrase); } - public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException { - return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(secretKey, passphrase); - } + public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException; - public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, PGPDigestCalculator digestCalculator, Passphrase passphrase) { - return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(symmetricKeyAlgorithm, digestCalculator, passphrase); - } + public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm, + PGPDigestCalculator digestCalculator, + Passphrase passphrase); - public PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException { - return FACTORY_IMPLEMENTATION.getPBESecretKeyDecryptor(passphrase); - } + public abstract PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException; public PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException { return getPGPDigestCalculator(algorithm.getAlgorithmId()); @@ -83,59 +79,38 @@ public abstract class ImplementationFactory { return getPGPDigestCalculatorProvider().get(algorithm); } - public PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException { - return FACTORY_IMPLEMENTATION.getPGPDigestCalculatorProvider(); - } + public abstract PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException; - public PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider() { - return FACTORY_IMPLEMENTATION.getPGPContentVerifierBuilderProvider(); - } + public abstract PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider(); public PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) { return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId()); } - public PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm) { - return FACTORY_IMPLEMENTATION.getPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm); - } + public abstract PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm); - public KeyFingerPrintCalculator getKeyFingerprintCalculator() { - return FACTORY_IMPLEMENTATION.getKeyFingerprintCalculator(); - } + public abstract KeyFingerPrintCalculator getKeyFingerprintCalculator(); - public PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException { - return FACTORY_IMPLEMENTATION.getPBEDataDecryptorFactory(passphrase); - } + public abstract PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException; - public PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey) { - return FACTORY_IMPLEMENTATION.getPublicKeyDataDecryptorFactory(privateKey); - } + public abstract PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey); - public PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key) { - return FACTORY_IMPLEMENTATION.getPublicKeyKeyEncryptionMethodGenerator(key); - } + public abstract PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key); - public PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase) { - return FACTORY_IMPLEMENTATION.getPBEKeyEncryptionMethodGenerator(passphrase); - } + public abstract PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase); public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) { return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId()); } - public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm) { - return FACTORY_IMPLEMENTATION.getPGPDataEncryptorBuilder(symmetricKeyAlgorithm); - } + public abstract PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm); - public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException { - return FACTORY_IMPLEMENTATION.getPGPKeyPair(algorithm, keyPair, creationDate); - } + public abstract PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException; - public PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, AsymmetricCipherKeyPair keyPair, Date creationDate) throws PGPException, NoSuchAlgorithmException, IOException, InvalidKeySpecException { - return FACTORY_IMPLEMENTATION.getPGPKeyPair(algorithm, keyPair, creationDate); - } + public abstract PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, AsymmetricCipherKeyPair keyPair, Date creationDate) + throws PGPException, NoSuchAlgorithmException, IOException, InvalidKeySpecException; - public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, HashAlgorithm hashAlgorithm, int s2kCount, Passphrase passphrase) throws PGPException { - return FACTORY_IMPLEMENTATION.getPBESecretKeyEncryptor(encryptionAlgorithm, hashAlgorithm, s2kCount, passphrase); - } + public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm, + HashAlgorithm hashAlgorithm, int s2kCount, + Passphrase passphrase) throws PGPException; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java b/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java index 59a2ccd4..f1f8be0a 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/implementation/JceImplementationFactory.java @@ -64,8 +64,7 @@ import org.pgpainless.util.Passphrase; public class JceImplementationFactory extends ImplementationFactory { - public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) - throws PGPException { + public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) { return new JcePBESecretKeyEncryptorBuilder(secretKey.getKeyEncryptionAlgorithm()) .setProvider(ProviderFactory.getProvider()) .build(passphrase.getChars()); diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java index a4f67bf4..d0a6841f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java @@ -357,7 +357,8 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { throws NoSuchAlgorithmException, PGPException, InvalidAlgorithmParameterException { KeyType type = spec.getKeyType(); - KeyPairGenerator certKeyGenerator = KeyPairGenerator.getInstance(type.getName(), ProviderFactory.getProvider()); + KeyPairGenerator certKeyGenerator = KeyPairGenerator.getInstance(type.getName(), + ProviderFactory.getProvider()); certKeyGenerator.initialize(type.getAlgorithmSpec()); // Create raw Key Pair