mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-19 02:42:05 +01:00
Kotlin conversion: ProviderFactory
This commit is contained in:
parent
8351223614
commit
11c1c54111
8 changed files with 63 additions and 92 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ public class ProviderFactoryTest {
|
|||
};
|
||||
|
||||
@Override
|
||||
protected Provider _getProvider() {
|
||||
protected Provider getSecurityProvider() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue