1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +01:00

Kotlin conversion: ProviderFactory

This commit is contained in:
Paul Schaub 2023-10-04 15:06:59 +02:00
parent 8351223614
commit 11c1c54111
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 63 additions and 92 deletions

View file

@ -1,23 +0,0 @@
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.provider;
import java.security.Provider;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* Implementation of {@link ProviderFactory} using a {@link BouncyCastleProvider}.
*/
public final class BouncyCastleProviderFactory extends ProviderFactory {
private static final Provider provider = new BouncyCastleProvider();
@Override
public Provider _getProvider() {
return provider;
}
}

View file

@ -1,46 +0,0 @@
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.provider;
import java.security.Provider;
/**
* Allow the use of different {@link Provider} implementations to provide cryptographic primitives by setting
* a {@link ProviderFactory} singleton.
* By default, the class is initialized with a {@link BouncyCastleProviderFactory}.
* <br>
* To make use of your own custom {@link Provider}, call {@link #setFactory(ProviderFactory)}, passing your
* own custom {@link ProviderFactory} instance.
*/
public abstract class ProviderFactory {
private static ProviderFactory FACTORY;
protected abstract Provider _getProvider();
protected String _getProviderName() {
return _getProvider().getName();
}
public static void setFactory(ProviderFactory factory) {
ProviderFactory.FACTORY = factory;
}
public static ProviderFactory getFactory() {
if (FACTORY == null) {
FACTORY = new BouncyCastleProviderFactory();
}
return FACTORY;
}
public static Provider getProvider() {
return ProviderFactory.getFactory()._getProvider();
}
public static String getProviderName() {
return ProviderFactory.getFactory()._getProviderName();
}
}

View file

@ -1,8 +0,0 @@
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Classes that allow setting a custom implementation of {@link java.security.Provider}.
*/
package org.pgpainless.provider;

View file

@ -31,20 +31,20 @@ import java.util.*
class JceImplementationFactory : ImplementationFactory() {
override val pgpDigestCalculatorProvider: PGPDigestCalculatorProvider =
JcaPGPDigestCalculatorProviderBuilder()
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build()
override val pgpContentVerifierBuilderProvider: PGPContentVerifierBuilderProvider =
JcaPGPContentVerifierBuilderProvider()
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override val keyFingerprintCalculator: KeyFingerPrintCalculator =
JcaKeyFingerprintCalculator()
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override fun getPBESecretKeyEncryptor(symmetricKeyAlgorithm: SymmetricKeyAlgorithm,
digestCalculator: PGPDigestCalculator,
passphrase: Passphrase): PBESecretKeyEncryptor =
JcePBESecretKeyEncryptorBuilder(symmetricKeyAlgorithm.algorithmId, digestCalculator)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(passphrase.getChars())
override fun getPBESecretKeyEncryptor(encryptionAlgorithm: SymmetricKeyAlgorithm,
@ -55,44 +55,44 @@ class JceImplementationFactory : ImplementationFactory() {
encryptionAlgorithm.algorithmId,
getPGPDigestCalculator(hashAlgorithm),
s2kCount)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(passphrase.getChars())
override fun getPBESecretKeyDecryptor(passphrase: Passphrase): PBESecretKeyDecryptor =
JcePBESecretKeyDecryptorBuilder(pgpDigestCalculatorProvider)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(passphrase.getChars())
override fun getPGPContentSignerBuilder(keyAlgorithm: Int, hashAlgorithm: Int): PGPContentSignerBuilder =
JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override fun getPBEDataDecryptorFactory(passphrase: Passphrase): PBEDataDecryptorFactory =
JcePBEDataDecryptorFactoryBuilder(pgpDigestCalculatorProvider)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(passphrase.getChars())
override fun getPublicKeyDataDecryptorFactory(privateKey: PGPPrivateKey): PublicKeyDataDecryptorFactory =
JcePublicKeyDataDecryptorFactoryBuilder()
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(privateKey)
override fun getSessionKeyDataDecryptorFactory(sessionKey: PGPSessionKey): SessionKeyDataDecryptorFactory =
JceSessionKeyDataDecryptorFactoryBuilder()
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
.build(sessionKey)
override fun getPublicKeyKeyEncryptionMethodGenerator(key: PGPPublicKey): PublicKeyKeyEncryptionMethodGenerator =
JcePublicKeyKeyEncryptionMethodGenerator(key)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override fun getPBEKeyEncryptionMethodGenerator(passphrase: Passphrase): PBEKeyEncryptionMethodGenerator =
JcePBEKeyEncryptionMethodGenerator(passphrase.getChars())
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override fun getPGPDataEncryptorBuilder(symmetricKeyAlgorithm: Int): PGPDataEncryptorBuilder =
JcePGPDataEncryptorBuilder(symmetricKeyAlgorithm)
.setProvider(ProviderFactory.getProvider())
.setProvider(ProviderFactory.provider)
override fun getPGPKeyPair(publicKeyAlgorithm: PublicKeyAlgorithm, keyPair: KeyPair, creationDate: Date): PGPKeyPair =
JcaPGPKeyPair(publicKeyAlgorithm.algorithmId, keyPair, creationDate)

View file

@ -222,7 +222,7 @@ class KeyRingBuilder : KeyRingBuilderInterface<KeyRingBuilder> {
fun generateKeyPair(spec: KeySpec): PGPKeyPair {
spec.keyType.let { type ->
// Create raw Key Pair
val keyPair = KeyPairGenerator.getInstance(type.name, ProviderFactory.getProvider())
val keyPair = KeyPairGenerator.getInstance(type.name, ProviderFactory.provider)
.also { it.initialize(type.algorithmSpec) }
.generateKeyPair()

View file

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.provider
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.Provider
class BouncyCastleProviderFactory : ProviderFactory() {
override val securityProvider: Provider = BouncyCastleProvider()
}

View file

@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.provider
import java.security.Provider
/**
* Allow the use of different [Provider] implementations to provide cryptographic primitives by setting
* a [ProviderFactory] singleton.
* By default, the class is initialized with a [BouncyCastleProviderFactory].
* To make use of your own custom [Provider], call [setFactory], passing your
* own custom [ProviderFactory] instance.
*/
abstract class ProviderFactory {
protected abstract val securityProvider: Provider
protected open val securityProviderName: String
get() = securityProvider.name
companion object {
// singleton instance
@JvmStatic
var factory: ProviderFactory = BouncyCastleProviderFactory()
@JvmStatic
val provider: Provider
@JvmName("getProvider")
get() = factory.securityProvider
@JvmStatic
val providerName: String
get() = factory.securityProviderName
}
}

View file

@ -21,7 +21,7 @@ public class ProviderFactoryTest {
};
@Override
protected Provider _getProvider() {
protected Provider getSecurityProvider() {
return provider;
}