mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-10 18:59:39 +02:00
Kotlin conversion: EncryptionBuilder + Interface
This commit is contained in:
parent
bbd956dbb7
commit
e9caa4af1f
4 changed files with 95 additions and 115 deletions
|
@ -0,0 +1,59 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.encryption_signing
|
||||
|
||||
import org.pgpainless.PGPainless.Companion.getPolicy
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||
import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator.Companion.byPopularity
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.OutputStream
|
||||
|
||||
class EncryptionBuilder : EncryptionBuilderInterface {
|
||||
override fun onOutputStream(outputStream: OutputStream): EncryptionBuilderInterface.WithOptions {
|
||||
return WithOptionsImpl(outputStream)
|
||||
}
|
||||
|
||||
class WithOptionsImpl(val outputStream: OutputStream) : EncryptionBuilderInterface.WithOptions {
|
||||
|
||||
override fun withOptions(options: ProducerOptions): EncryptionStream {
|
||||
return EncryptionStream(outputStream, options)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
val LOGGER: Logger = LoggerFactory.getLogger(EncryptionBuilder::class.java)
|
||||
|
||||
/**
|
||||
* Negotiate the [SymmetricKeyAlgorithm] used for message encryption.
|
||||
*
|
||||
* @param encryptionOptions encryption options
|
||||
* @return negotiated symmetric key algorithm
|
||||
*/
|
||||
@JvmStatic
|
||||
fun negotiateSymmetricEncryptionAlgorithm(encryptionOptions: EncryptionOptions): SymmetricKeyAlgorithm {
|
||||
val preferences = encryptionOptions.keyViews.values
|
||||
.map { it.preferredSymmetricKeyAlgorithms }
|
||||
.toList()
|
||||
val algorithm = byPopularity().negotiate(
|
||||
getPolicy().symmetricKeyEncryptionAlgorithmPolicy,
|
||||
encryptionOptions.encryptionAlgorithmOverride,
|
||||
preferences)
|
||||
LOGGER.debug("Negotiation resulted in {} being the symmetric encryption algorithm of choice.", algorithm)
|
||||
return algorithm
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun negotiateCompressionAlgorithm(producerOptions: ProducerOptions): CompressionAlgorithm {
|
||||
val compressionAlgorithmOverride = producerOptions.compressionAlgorithmOverride
|
||||
return compressionAlgorithmOverride ?: getPolicy().compressionAlgorithmPolicy.defaultCompressionAlgorithm()
|
||||
|
||||
// TODO: Negotiation
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.encryption_signing
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
|
||||
fun interface EncryptionBuilderInterface {
|
||||
|
||||
/**
|
||||
* Create a [EncryptionStream] wrapping an [OutputStream]. Data that is piped through the
|
||||
* [EncryptionStream] will be encrypted and/or signed.
|
||||
*
|
||||
* @param outputStream output stream which receives the encrypted / signed data.
|
||||
* @return api handle
|
||||
*/
|
||||
fun onOutputStream(outputStream: OutputStream): WithOptions
|
||||
|
||||
fun interface WithOptions {
|
||||
|
||||
/**
|
||||
* Create an [EncryptionStream] with the given options (recipients, signers, algorithms...).
|
||||
*
|
||||
* @param options options
|
||||
* @return encryption stream
|
||||
*
|
||||
* @throws PGPException if something goes wrong during encryption stream preparation
|
||||
* @throws IOException if something goes wrong during encryption stream preparation (writing headers)
|
||||
*/
|
||||
@Throws(PGPException::class, IOException::class)
|
||||
fun withOptions(options: ProducerOptions): EncryptionStream
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue