pgpainless/pgpainless-core/src/main/java/org/pgpainless/implementation/ImplementationFactory.java

107 lines
5.0 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2020-12-27 01:56:18 +01:00
package org.pgpainless.implementation;
2021-01-03 15:52:33 +01:00
import java.security.KeyPair;
import java.util.Date;
2020-12-27 01:56:18 +01:00
2021-01-03 15:52:33 +01:00
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.util.Passphrase;
2020-12-27 01:56:18 +01:00
2021-01-03 15:52:33 +01:00
public abstract class ImplementationFactory {
private static ImplementationFactory FACTORY_IMPLEMENTATION;
2021-01-03 15:52:33 +01:00
public static void setFactoryImplementation(ImplementationFactory implementation) {
2020-12-27 01:56:18 +01:00
FACTORY_IMPLEMENTATION = implementation;
}
2021-01-03 15:52:33 +01:00
public static ImplementationFactory getInstance() {
if (FACTORY_IMPLEMENTATION == null) {
FACTORY_IMPLEMENTATION = new BcImplementationFactory();
}
2020-12-27 01:56:18 +01:00
return FACTORY_IMPLEMENTATION;
}
2021-01-03 15:52:33 +01:00
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
Passphrase passphrase)
throws PGPException {
return getPBESecretKeyEncryptor(symmetricKeyAlgorithm,
getPGPDigestCalculator(HashAlgorithm.SHA1), passphrase);
}
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException;
2021-01-03 15:52:33 +01:00
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
PGPDigestCalculator digestCalculator,
Passphrase passphrase);
2021-01-03 15:52:33 +01:00
public abstract PBESecretKeyDecryptor getPBESecretKeyDecryptor(Passphrase passphrase) throws PGPException;
2021-01-03 15:52:33 +01:00
public PGPDigestCalculator getPGPDigestCalculator(HashAlgorithm algorithm) throws PGPException {
return getPGPDigestCalculator(algorithm.getAlgorithmId());
}
public PGPDigestCalculator getPGPDigestCalculator(int algorithm) throws PGPException {
return getPGPDigestCalculatorProvider().get(algorithm);
}
public abstract PGPDigestCalculatorProvider getPGPDigestCalculatorProvider() throws PGPException;
2021-01-03 15:52:33 +01:00
public abstract PGPContentVerifierBuilderProvider getPGPContentVerifierBuilderProvider();
2021-01-03 15:52:33 +01:00
public PGPContentSignerBuilder getPGPContentSignerBuilder(PublicKeyAlgorithm keyAlgorithm, HashAlgorithm hashAlgorithm) {
return getPGPContentSignerBuilder(keyAlgorithm.getAlgorithmId(), hashAlgorithm.getAlgorithmId());
}
public abstract PGPContentSignerBuilder getPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm);
2021-01-03 15:52:33 +01:00
public abstract KeyFingerPrintCalculator getKeyFingerprintCalculator();
2021-01-03 15:52:33 +01:00
public abstract PBEDataDecryptorFactory getPBEDataDecryptorFactory(Passphrase passphrase) throws PGPException;
2021-01-03 15:52:33 +01:00
public abstract PublicKeyDataDecryptorFactory getPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey);
2021-01-03 15:52:33 +01:00
public abstract PublicKeyKeyEncryptionMethodGenerator getPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key);
2021-01-03 15:52:33 +01:00
public abstract PBEKeyEncryptionMethodGenerator getPBEKeyEncryptionMethodGenerator(Passphrase passphrase);
2021-01-03 15:52:33 +01:00
public PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(SymmetricKeyAlgorithm symmetricKeyAlgorithm) {
return getPGPDataEncryptorBuilder(symmetricKeyAlgorithm.getAlgorithmId());
}
public abstract PGPDataEncryptorBuilder getPGPDataEncryptorBuilder(int symmetricKeyAlgorithm);
2021-01-03 15:52:33 +01:00
public abstract PGPKeyPair getPGPKeyPair(PublicKeyAlgorithm algorithm, KeyPair keyPair, Date creationDate) throws PGPException;
2021-01-03 15:52:33 +01:00
public abstract PBESecretKeyEncryptor getPBESecretKeyEncryptor(SymmetricKeyAlgorithm encryptionAlgorithm,
HashAlgorithm hashAlgorithm, int s2kCount,
Passphrase passphrase) throws PGPException;
@Override
public String toString() {
return getClass().getSimpleName();
}
2020-12-27 01:56:18 +01:00
}