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)
|
@Throws(IOException::class)
|
||||||
private fun prepareCompression() {
|
private fun prepareCompression() {
|
||||||
options.negotiateCompressionAlgorithm().let {
|
options.negotiateCompressionAlgorithm(api.algorithmPolicy).let {
|
||||||
resultBuilder.setCompressionAlgorithm(it)
|
resultBuilder.setCompressionAlgorithm(it)
|
||||||
compressedDataGenerator = PGPCompressedDataGenerator(it.algorithmId)
|
compressedDataGenerator = PGPCompressedDataGenerator(it.algorithmId)
|
||||||
if (it == CompressionAlgorithm.UNCOMPRESSED) return
|
if (it == CompressionAlgorithm.UNCOMPRESSED) return
|
||||||
|
|
|
@ -6,14 +6,13 @@ package org.pgpainless.encryption_signing
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import org.bouncycastle.openpgp.PGPLiteralData
|
import org.bouncycastle.openpgp.PGPLiteralData
|
||||||
import org.pgpainless.PGPainless
|
|
||||||
import org.pgpainless.algorithm.CompressionAlgorithm
|
import org.pgpainless.algorithm.CompressionAlgorithm
|
||||||
import org.pgpainless.algorithm.StreamEncoding
|
import org.pgpainless.algorithm.StreamEncoding
|
||||||
|
import org.pgpainless.policy.Policy
|
||||||
|
|
||||||
class ProducerOptions(
|
class ProducerOptions(
|
||||||
val encryptionOptions: EncryptionOptions?,
|
val encryptionOptions: EncryptionOptions?,
|
||||||
val signingOptions: SigningOptions?,
|
val signingOptions: SigningOptions?
|
||||||
val api: PGPainless
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private var _fileName: String = ""
|
private var _fileName: String = ""
|
||||||
|
@ -24,8 +23,8 @@ class ProducerOptions(
|
||||||
private var _hideArmorHeaders = false
|
private var _hideArmorHeaders = false
|
||||||
var isDisableAsciiArmorCRC = false
|
var isDisableAsciiArmorCRC = false
|
||||||
|
|
||||||
private var _compressionAlgorithmOverride: CompressionAlgorithm =
|
private var _compressionAlgorithmOverride: CompressionAlgorithm? = null
|
||||||
api.algorithmPolicy.compressionAlgorithmPolicy.defaultCompressionAlgorithm
|
|
||||||
private var asciiArmor = true
|
private var asciiArmor = true
|
||||||
private var _comment: String? = null
|
private var _comment: String? = null
|
||||||
private var _version: String? = null
|
private var _version: String? = null
|
||||||
|
@ -219,7 +218,7 @@ class ProducerOptions(
|
||||||
_compressionAlgorithmOverride = compressionAlgorithm
|
_compressionAlgorithmOverride = compressionAlgorithm
|
||||||
}
|
}
|
||||||
|
|
||||||
val compressionAlgorithmOverride: CompressionAlgorithm
|
val compressionAlgorithmOverride: CompressionAlgorithm?
|
||||||
get() = _compressionAlgorithmOverride
|
get() = _compressionAlgorithmOverride
|
||||||
|
|
||||||
val isHideArmorHeaders: Boolean
|
val isHideArmorHeaders: Boolean
|
||||||
|
@ -237,8 +236,9 @@ class ProducerOptions(
|
||||||
_hideArmorHeaders = hideArmorHeaders
|
_hideArmorHeaders = hideArmorHeaders
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun negotiateCompressionAlgorithm(): CompressionAlgorithm {
|
internal fun negotiateCompressionAlgorithm(policy: Policy): CompressionAlgorithm {
|
||||||
return compressionAlgorithmOverride
|
return compressionAlgorithmOverride
|
||||||
|
?: policy.compressionAlgorithmPolicy.defaultCompressionAlgorithm
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -249,13 +249,11 @@ class ProducerOptions(
|
||||||
* @param signingOptions signing options
|
* @param signingOptions signing options
|
||||||
* @return builder
|
* @return builder
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun signAndEncrypt(
|
fun signAndEncrypt(
|
||||||
encryptionOptions: EncryptionOptions,
|
encryptionOptions: EncryptionOptions,
|
||||||
signingOptions: SigningOptions,
|
signingOptions: SigningOptions
|
||||||
api: PGPainless = PGPainless.getInstance()
|
): ProducerOptions = ProducerOptions(encryptionOptions, signingOptions)
|
||||||
): ProducerOptions = ProducerOptions(encryptionOptions, signingOptions, api)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sign some data without encryption.
|
* Sign some data without encryption.
|
||||||
|
@ -263,12 +261,9 @@ class ProducerOptions(
|
||||||
* @param signingOptions signing options
|
* @param signingOptions signing options
|
||||||
* @return builder
|
* @return builder
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun sign(
|
fun sign(signingOptions: SigningOptions): ProducerOptions =
|
||||||
signingOptions: SigningOptions,
|
ProducerOptions(null, signingOptions)
|
||||||
api: PGPainless = PGPainless.getInstance()
|
|
||||||
): ProducerOptions = ProducerOptions(null, signingOptions, api)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt some data without signing.
|
* Encrypt some data without signing.
|
||||||
|
@ -276,21 +271,15 @@ class ProducerOptions(
|
||||||
* @param encryptionOptions encryption options
|
* @param encryptionOptions encryption options
|
||||||
* @return builder
|
* @return builder
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun encrypt(
|
fun encrypt(encryptionOptions: EncryptionOptions): ProducerOptions =
|
||||||
encryptionOptions: EncryptionOptions,
|
ProducerOptions(encryptionOptions, null)
|
||||||
api: PGPainless = PGPainless.getInstance()
|
|
||||||
): ProducerOptions = ProducerOptions(encryptionOptions, null, api)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only wrap the data in an OpenPGP packet. No encryption or signing will be applied.
|
* Only wrap the data in an OpenPGP packet. No encryption or signing will be applied.
|
||||||
*
|
*
|
||||||
* @return builder
|
* @return builder
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
@JvmStatic fun noEncryptionNoSigning(): ProducerOptions = ProducerOptions(null, null)
|
||||||
@JvmStatic
|
|
||||||
fun noEncryptionNoSigning(api: PGPainless = PGPainless.getInstance()): ProducerOptions =
|
|
||||||
ProducerOptions(null, null, api)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,8 +66,7 @@ public class SigningTest {
|
||||||
.addRecipient(cryptieKey.toCertificate()),
|
.addRecipient(cryptieKey.toCertificate()),
|
||||||
SigningOptions.get(api).addInlineSignature(
|
SigningOptions.get(api).addInlineSignature(
|
||||||
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, cryptieSigningKey),
|
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, cryptieSigningKey),
|
||||||
cryptieKey, TestKeys.CRYPTIE_UID, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT),
|
cryptieKey, TestKeys.CRYPTIE_UID, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
|
||||||
api
|
|
||||||
).setAsciiArmor(true));
|
).setAsciiArmor(true));
|
||||||
|
|
||||||
byte[] messageBytes = "This message is signed and encrypted to Romeo and Juliet."
|
byte[] messageBytes = "This message is signed and encrypted to Romeo and Juliet."
|
||||||
|
@ -159,7 +158,7 @@ public class SigningTest {
|
||||||
String data = "Hello, World!\n";
|
String data = "Hello, World!\n";
|
||||||
EncryptionStream signer = api.generateMessage()
|
EncryptionStream signer = api.generateMessage()
|
||||||
.onOutputStream(new ByteArrayOutputStream())
|
.onOutputStream(new ByteArrayOutputStream())
|
||||||
.withOptions(ProducerOptions.sign(options, api));
|
.withOptions(ProducerOptions.sign(options));
|
||||||
|
|
||||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||||
signer.close();
|
signer.close();
|
||||||
|
@ -192,7 +191,7 @@ public class SigningTest {
|
||||||
String data = "Hello, World!\n";
|
String data = "Hello, World!\n";
|
||||||
EncryptionStream signer = api.generateMessage()
|
EncryptionStream signer = api.generateMessage()
|
||||||
.onOutputStream(new ByteArrayOutputStream())
|
.onOutputStream(new ByteArrayOutputStream())
|
||||||
.withOptions(ProducerOptions.sign(options, api));
|
.withOptions(ProducerOptions.sign(options));
|
||||||
|
|
||||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||||
signer.close();
|
signer.close();
|
||||||
|
@ -223,7 +222,7 @@ public class SigningTest {
|
||||||
String data = "Hello, World!\n";
|
String data = "Hello, World!\n";
|
||||||
EncryptionStream signer = api.generateMessage()
|
EncryptionStream signer = api.generateMessage()
|
||||||
.onOutputStream(new ByteArrayOutputStream())
|
.onOutputStream(new ByteArrayOutputStream())
|
||||||
.withOptions(ProducerOptions.sign(options, api));
|
.withOptions(ProducerOptions.sign(options));
|
||||||
|
|
||||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), signer);
|
||||||
signer.close();
|
signer.close();
|
||||||
|
|
|
@ -32,12 +32,13 @@ import org.pgpainless.util.ArmorUtils;
|
||||||
|
|
||||||
public class Sign {
|
public class Sign {
|
||||||
|
|
||||||
private static OpenPGPKey secretKey;
|
private static OpenPGPKey key;
|
||||||
private static SecretKeyRingProtector protector;
|
private static SecretKeyRingProtector protector;
|
||||||
|
private static final PGPainless api = PGPainless.getInstance();
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void prepare() {
|
public static void prepare() {
|
||||||
secretKey = PGPainless.generateKeyRing()
|
key = api.generateKey()
|
||||||
.modernKeyRing("Emilia Example <emilia@example.org>");
|
.modernKeyRing("Emilia Example <emilia@example.org>");
|
||||||
protector = SecretKeyRingProtector.unprotectedKeys(); // no password
|
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.";
|
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));
|
InputStream messageIn = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
|
||||||
ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
|
||||||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
EncryptionStream signingStream = api.generateMessage()
|
||||||
.onOutputStream(signedOut)
|
.onOutputStream(signedOut)
|
||||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
.withOptions(ProducerOptions.sign(SigningOptions.get(api)
|
||||||
.addSignature(protector, secretKey))
|
.addSignature(protector, key))
|
||||||
);
|
);
|
||||||
|
|
||||||
Streams.pipeAll(messageIn, signingStream);
|
Streams.pipeAll(messageIn, signingStream);
|
||||||
|
@ -84,7 +85,7 @@ public class Sign {
|
||||||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
||||||
.onOutputStream(ignoreMe)
|
.onOutputStream(ignoreMe)
|
||||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
||||||
.addDetachedSignature(protector, secretKey, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT))
|
.addDetachedSignature(protector, key, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT))
|
||||||
.setAsciiArmor(false)
|
.setAsciiArmor(false)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -93,7 +94,7 @@ public class Sign {
|
||||||
|
|
||||||
EncryptionResult result = signingStream.getResult();
|
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();
|
PGPSignature signature = result.getDetachedSignatures().get(new SubkeyIdentifier(signingKey)).iterator().next();
|
||||||
String detachedSignature = ArmorUtils.toAsciiArmoredString(signature.getEncoded());
|
String detachedSignature = ArmorUtils.toAsciiArmoredString(signature.getEncoded());
|
||||||
|
|
||||||
|
@ -128,7 +129,7 @@ public class Sign {
|
||||||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
||||||
.onOutputStream(signedOut)
|
.onOutputStream(signedOut)
|
||||||
.withOptions(ProducerOptions.sign(SigningOptions.get()
|
.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!!!
|
.setCleartextSigned() // <- Explicitly use Cleartext Signature Framework!!!
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -107,8 +107,7 @@ public class StupidAlgorithmPreferenceEncryptionTest {
|
||||||
EncryptionStream encryptionStream = api.generateMessage()
|
EncryptionStream encryptionStream = api.generateMessage()
|
||||||
.onOutputStream(out)
|
.onOutputStream(out)
|
||||||
.withOptions(ProducerOptions.encrypt(
|
.withOptions(ProducerOptions.encrypt(
|
||||||
EncryptionOptions.get(api).addRecipient(certificate),
|
EncryptionOptions.get(api).addRecipient(certificate)
|
||||||
api
|
|
||||||
));
|
));
|
||||||
|
|
||||||
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
|
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
|
@ -58,7 +58,7 @@ class DetachedSignImpl(private val api: PGPainless) : DetachedSign {
|
||||||
api.generateMessage()
|
api.generateMessage()
|
||||||
.discardOutput()
|
.discardOutput()
|
||||||
.withOptions(
|
.withOptions(
|
||||||
ProducerOptions.sign(signingOptions, api)
|
ProducerOptions.sign(signingOptions)
|
||||||
.setAsciiArmor(armor)
|
.setAsciiArmor(armor)
|
||||||
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED))
|
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED))
|
||||||
|
|
||||||
|
|
|
@ -57,9 +57,9 @@ class EncryptImpl(private val api: PGPainless) : Encrypt {
|
||||||
|
|
||||||
val options =
|
val options =
|
||||||
if (signingOptions != null) {
|
if (signingOptions != null) {
|
||||||
ProducerOptions.signAndEncrypt(encryptionOptions, signingOptions!!, api)
|
ProducerOptions.signAndEncrypt(encryptionOptions, signingOptions!!)
|
||||||
} else {
|
} else {
|
||||||
ProducerOptions.encrypt(encryptionOptions, api)
|
ProducerOptions.encrypt(encryptionOptions)
|
||||||
}
|
}
|
||||||
.setAsciiArmor(armor)
|
.setAsciiArmor(armor)
|
||||||
.setEncoding(modeToStreamEncoding(mode))
|
.setEncoding(modeToStreamEncoding(mode))
|
||||||
|
|
|
@ -56,7 +56,7 @@ class InlineSignImpl(private val api: PGPainless) : InlineSign {
|
||||||
}
|
}
|
||||||
|
|
||||||
val producerOptions =
|
val producerOptions =
|
||||||
ProducerOptions.sign(signingOptions, api).apply {
|
ProducerOptions.sign(signingOptions).apply {
|
||||||
when (mode) {
|
when (mode) {
|
||||||
InlineSignAs.clearsigned -> {
|
InlineSignAs.clearsigned -> {
|
||||||
setCleartextSigned()
|
setCleartextSigned()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue