mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Remove API instance parameter from ProducerOptions
This commit is contained in:
parent
41251296ce
commit
ca22446f1c
8 changed files with 33 additions and 45 deletions
|
@ -106,7 +106,7 @@ class EncryptionStream(
|
|||
|
||||
@Throws(IOException::class)
|
||||
private fun prepareCompression() {
|
||||
options.negotiateCompressionAlgorithm().let {
|
||||
options.negotiateCompressionAlgorithm(api.algorithmPolicy).let {
|
||||
resultBuilder.setCompressionAlgorithm(it)
|
||||
compressedDataGenerator = PGPCompressedDataGenerator(it.algorithmId)
|
||||
if (it == CompressionAlgorithm.UNCOMPRESSED) return
|
||||
|
|
|
@ -6,14 +6,13 @@ package org.pgpainless.encryption_signing
|
|||
|
||||
import java.util.*
|
||||
import org.bouncycastle.openpgp.PGPLiteralData
|
||||
import org.pgpainless.PGPainless
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm
|
||||
import org.pgpainless.algorithm.StreamEncoding
|
||||
import org.pgpainless.policy.Policy
|
||||
|
||||
class ProducerOptions(
|
||||
val encryptionOptions: EncryptionOptions?,
|
||||
val signingOptions: SigningOptions?,
|
||||
val api: PGPainless
|
||||
val signingOptions: SigningOptions?
|
||||
) {
|
||||
|
||||
private var _fileName: String = ""
|
||||
|
@ -24,8 +23,8 @@ class ProducerOptions(
|
|||
private var _hideArmorHeaders = false
|
||||
var isDisableAsciiArmorCRC = false
|
||||
|
||||
private var _compressionAlgorithmOverride: CompressionAlgorithm =
|
||||
api.algorithmPolicy.compressionAlgorithmPolicy.defaultCompressionAlgorithm
|
||||
private var _compressionAlgorithmOverride: CompressionAlgorithm? = null
|
||||
|
||||
private var asciiArmor = true
|
||||
private var _comment: String? = null
|
||||
private var _version: String? = null
|
||||
|
@ -219,7 +218,7 @@ class ProducerOptions(
|
|||
_compressionAlgorithmOverride = compressionAlgorithm
|
||||
}
|
||||
|
||||
val compressionAlgorithmOverride: CompressionAlgorithm
|
||||
val compressionAlgorithmOverride: CompressionAlgorithm?
|
||||
get() = _compressionAlgorithmOverride
|
||||
|
||||
val isHideArmorHeaders: Boolean
|
||||
|
@ -237,8 +236,9 @@ class ProducerOptions(
|
|||
_hideArmorHeaders = hideArmorHeaders
|
||||
}
|
||||
|
||||
internal fun negotiateCompressionAlgorithm(): CompressionAlgorithm {
|
||||
internal fun negotiateCompressionAlgorithm(policy: Policy): CompressionAlgorithm {
|
||||
return compressionAlgorithmOverride
|
||||
?: policy.compressionAlgorithmPolicy.defaultCompressionAlgorithm
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -249,13 +249,11 @@ class ProducerOptions(
|
|||
* @param signingOptions signing options
|
||||
* @return builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun signAndEncrypt(
|
||||
encryptionOptions: EncryptionOptions,
|
||||
signingOptions: SigningOptions,
|
||||
api: PGPainless = PGPainless.getInstance()
|
||||
): ProducerOptions = ProducerOptions(encryptionOptions, signingOptions, api)
|
||||
signingOptions: SigningOptions
|
||||
): ProducerOptions = ProducerOptions(encryptionOptions, signingOptions)
|
||||
|
||||
/**
|
||||
* Sign some data without encryption.
|
||||
|
@ -263,12 +261,9 @@ class ProducerOptions(
|
|||
* @param signingOptions signing options
|
||||
* @return builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun sign(
|
||||
signingOptions: SigningOptions,
|
||||
api: PGPainless = PGPainless.getInstance()
|
||||
): ProducerOptions = ProducerOptions(null, signingOptions, api)
|
||||
fun sign(signingOptions: SigningOptions): ProducerOptions =
|
||||
ProducerOptions(null, signingOptions)
|
||||
|
||||
/**
|
||||
* Encrypt some data without signing.
|
||||
|
@ -276,21 +271,15 @@ class ProducerOptions(
|
|||
* @param encryptionOptions encryption options
|
||||
* @return builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun encrypt(
|
||||
encryptionOptions: EncryptionOptions,
|
||||
api: PGPainless = PGPainless.getInstance()
|
||||
): ProducerOptions = ProducerOptions(encryptionOptions, null, api)
|
||||
fun encrypt(encryptionOptions: EncryptionOptions): ProducerOptions =
|
||||
ProducerOptions(encryptionOptions, null)
|
||||
|
||||
/**
|
||||
* Only wrap the data in an OpenPGP packet. No encryption or signing will be applied.
|
||||
*
|
||||
* @return builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun noEncryptionNoSigning(api: PGPainless = PGPainless.getInstance()): ProducerOptions =
|
||||
ProducerOptions(null, null, api)
|
||||
@JvmStatic fun noEncryptionNoSigning(): ProducerOptions = ProducerOptions(null, null)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,8 +66,7 @@ public class SigningTest {
|
|||
.addRecipient(cryptieKey.toCertificate()),
|
||||
SigningOptions.get(api).addInlineSignature(
|
||||
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, cryptieSigningKey),
|
||||
cryptieKey, TestKeys.CRYPTIE_UID, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT),
|
||||
api
|
||||
cryptieKey, TestKeys.CRYPTIE_UID, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
|
||||
).setAsciiArmor(true));
|
||||
|
||||
byte[] messageBytes = "This message is signed and encrypted to Romeo and Juliet."
|
||||
|
@ -159,7 +158,7 @@ public class SigningTest {
|
|||
String data = "Hello, World!\n";
|
||||
EncryptionStream signer = api.generateMessage()
|
||||
.onOutputStream(new ByteArrayOutputStream())
|
||||
.withOptions(ProducerOptions.sign(options, api));
|
||||
.withOptions(ProducerOptions.sign(options));
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||
signer.close();
|
||||
|
@ -192,7 +191,7 @@ public class SigningTest {
|
|||
String data = "Hello, World!\n";
|
||||
EncryptionStream signer = api.generateMessage()
|
||||
.onOutputStream(new ByteArrayOutputStream())
|
||||
.withOptions(ProducerOptions.sign(options, api));
|
||||
.withOptions(ProducerOptions.sign(options));
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||
signer.close();
|
||||
|
@ -223,7 +222,7 @@ public class SigningTest {
|
|||
String data = "Hello, World!\n";
|
||||
EncryptionStream signer = api.generateMessage()
|
||||
.onOutputStream(new ByteArrayOutputStream())
|
||||
.withOptions(ProducerOptions.sign(options, api));
|
||||
.withOptions(ProducerOptions.sign(options));
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||
signer.close();
|
||||
|
|
|
@ -32,12 +32,13 @@ import org.pgpainless.util.ArmorUtils;
|
|||
|
||||
public class Sign {
|
||||
|
||||
private static OpenPGPKey secretKey;
|
||||
private static OpenPGPKey key;
|
||||
private static SecretKeyRingProtector protector;
|
||||
private static final PGPainless api = PGPainless.getInstance();
|
||||
|
||||
@BeforeAll
|
||||
public static void prepare() {
|
||||
secretKey = PGPainless.generateKeyRing()
|
||||
key = api.generateKey()
|
||||
.modernKeyRing("Emilia Example <emilia@example.org>");
|
||||
protector = SecretKeyRingProtector.unprotectedKeys(); // no password
|
||||
}
|
||||
|
@ -51,10 +52,10 @@ public class Sign {
|
|||
String message = "\"Derivative Works\" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.";
|
||||
InputStream messageIn = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
|
||||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
||||
EncryptionStream signingStream = api.generateMessage()
|
||||
.onOutputStream(signedOut)
|
||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
||||
.addSignature(protector, secretKey))
|
||||
.withOptions(ProducerOptions.sign(SigningOptions.get(api)
|
||||
.addSignature(protector, key))
|
||||
);
|
||||
|
||||
Streams.pipeAll(messageIn, signingStream);
|
||||
|
@ -84,7 +85,7 @@ public class Sign {
|
|||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(ignoreMe)
|
||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
||||
.addDetachedSignature(protector, secretKey, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT))
|
||||
.addDetachedSignature(protector, key, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT))
|
||||
.setAsciiArmor(false)
|
||||
);
|
||||
|
||||
|
@ -93,7 +94,7 @@ public class Sign {
|
|||
|
||||
EncryptionResult result = signingStream.getResult();
|
||||
|
||||
OpenPGPCertificate.OpenPGPComponentKey signingKey = PGPainless.inspectKeyRing(secretKey).getSigningSubkeys().get(0);
|
||||
OpenPGPCertificate.OpenPGPComponentKey signingKey = PGPainless.inspectKeyRing(key).getSigningSubkeys().get(0);
|
||||
PGPSignature signature = result.getDetachedSignatures().get(new SubkeyIdentifier(signingKey)).iterator().next();
|
||||
String detachedSignature = ArmorUtils.toAsciiArmoredString(signature.getEncoded());
|
||||
|
||||
|
@ -128,7 +129,7 @@ public class Sign {
|
|||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(signedOut)
|
||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
||||
.addDetachedSignature(protector, secretKey, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)) // Human-readable text document
|
||||
.addDetachedSignature(protector, key, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)) // Human-readable text document
|
||||
.setCleartextSigned() // <- Explicitly use Cleartext Signature Framework!!!
|
||||
);
|
||||
|
||||
|
|
|
@ -107,8 +107,7 @@ public class StupidAlgorithmPreferenceEncryptionTest {
|
|||
EncryptionStream encryptionStream = api.generateMessage()
|
||||
.onOutputStream(out)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions.get(api).addRecipient(certificate),
|
||||
api
|
||||
EncryptionOptions.get(api).addRecipient(certificate)
|
||||
));
|
||||
|
||||
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
|
||||
|
|
|
@ -58,7 +58,7 @@ class DetachedSignImpl(private val api: PGPainless) : DetachedSign {
|
|||
api.generateMessage()
|
||||
.discardOutput()
|
||||
.withOptions(
|
||||
ProducerOptions.sign(signingOptions, api)
|
||||
ProducerOptions.sign(signingOptions)
|
||||
.setAsciiArmor(armor)
|
||||
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED))
|
||||
|
||||
|
|
|
@ -57,9 +57,9 @@ class EncryptImpl(private val api: PGPainless) : Encrypt {
|
|||
|
||||
val options =
|
||||
if (signingOptions != null) {
|
||||
ProducerOptions.signAndEncrypt(encryptionOptions, signingOptions!!, api)
|
||||
ProducerOptions.signAndEncrypt(encryptionOptions, signingOptions!!)
|
||||
} else {
|
||||
ProducerOptions.encrypt(encryptionOptions, api)
|
||||
ProducerOptions.encrypt(encryptionOptions)
|
||||
}
|
||||
.setAsciiArmor(armor)
|
||||
.setEncoding(modeToStreamEncoding(mode))
|
||||
|
|
|
@ -56,7 +56,7 @@ class InlineSignImpl(private val api: PGPainless) : InlineSign {
|
|||
}
|
||||
|
||||
val producerOptions =
|
||||
ProducerOptions.sign(signingOptions, api).apply {
|
||||
ProducerOptions.sign(signingOptions).apply {
|
||||
when (mode) {
|
||||
InlineSignAs.clearsigned -> {
|
||||
setCleartextSigned()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue