1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-25 22:02:05 +01:00

Add EncryptionBuilder.discardOutput()

Also move NullOutputStream from pgpainless-sop to pgpainless-core
This commit is contained in:
Paul Schaub 2024-03-29 20:37:24 +01:00
parent 80cf1a7446
commit b96f22d0a9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 18 additions and 8 deletions

View file

@ -9,6 +9,7 @@ import org.pgpainless.PGPainless.Companion.getPolicy
import org.pgpainless.algorithm.CompressionAlgorithm import org.pgpainless.algorithm.CompressionAlgorithm
import org.pgpainless.algorithm.SymmetricKeyAlgorithm import org.pgpainless.algorithm.SymmetricKeyAlgorithm
import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator.Companion.byPopularity import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator.Companion.byPopularity
import org.pgpainless.util.NullOutputStream
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -19,6 +20,10 @@ class EncryptionBuilder : EncryptionBuilderInterface {
return WithOptionsImpl(outputStream) return WithOptionsImpl(outputStream)
} }
override fun discardOutput(): EncryptionBuilderInterface.WithOptions {
return onOutputStream(NullOutputStream())
}
class WithOptionsImpl(val outputStream: OutputStream) : EncryptionBuilderInterface.WithOptions { class WithOptionsImpl(val outputStream: OutputStream) : EncryptionBuilderInterface.WithOptions {
override fun withOptions(options: ProducerOptions): EncryptionStream { override fun withOptions(options: ProducerOptions): EncryptionStream {

View file

@ -8,7 +8,7 @@ import java.io.IOException
import java.io.OutputStream import java.io.OutputStream
import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPException
fun interface EncryptionBuilderInterface { interface EncryptionBuilderInterface {
/** /**
* Create a [EncryptionStream] wrapping an [OutputStream]. Data that is piped through the * Create a [EncryptionStream] wrapping an [OutputStream]. Data that is piped through the
@ -19,6 +19,16 @@ fun interface EncryptionBuilderInterface {
*/ */
fun onOutputStream(outputStream: OutputStream): WithOptions fun onOutputStream(outputStream: OutputStream): WithOptions
/**
* Create an [EncryptionStream] that discards the data after processing it. This is useful, e.g.
* for generating detached signatures, where the resulting signature is retrieved from the
* [EncryptionResult] once the operation is finished. In this case, the plaintext data does not
* need to be retained.
*
* @return api handle
*/
fun discardOutput(): WithOptions
fun interface WithOptions { fun interface WithOptions {
/** /**

View file

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package org.pgpainless.sop package org.pgpainless.util
import java.io.OutputStream import java.io.OutputStream

View file

@ -53,15 +53,10 @@ class DetachedSignImpl : DetachedSign {
} }
} }
// When creating a detached signature, the output of the signing stream is actually
// the unmodified plaintext data, so we can discard it.
// The detached signature will later be retrieved from the metadata object instead.
val sink = NullOutputStream()
try { try {
val signingStream = val signingStream =
PGPainless.encryptAndOrSign() PGPainless.encryptAndOrSign()
.onOutputStream(sink) .discardOutput()
.withOptions(ProducerOptions.sign(signingOptions).setAsciiArmor(armor)) .withOptions(ProducerOptions.sign(signingOptions).setAsciiArmor(armor))
return object : ReadyWithResult<SigningResult>() { return object : ReadyWithResult<SigningResult>() {