1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Move default parameters of Options classes to factory methods

This commit is contained in:
Paul Schaub 2025-03-18 14:24:49 +01:00
parent 12fd807f75
commit 93ee037ef0
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
26 changed files with 117 additions and 92 deletions

View file

@ -24,7 +24,7 @@ import org.pgpainless.util.Passphrase
import org.pgpainless.util.SessionKey
/** Options for decryption and signature verification. */
class ConsumerOptions(private val api: PGPainless = PGPainless.getInstance()) {
class ConsumerOptions(private val api: PGPainless) {
private var ignoreMDCErrors = false
var isDisableAsciiArmorCRC = false
@ -468,6 +468,8 @@ class ConsumerOptions(private val api: PGPainless = PGPainless.getInstance()) {
}
companion object {
@JvmStatic fun get() = ConsumerOptions()
@JvmOverloads
@JvmStatic
fun get(api: PGPainless = PGPainless.getInstance()) = ConsumerOptions(api)
}
}

View file

@ -22,10 +22,7 @@ import org.pgpainless.key.info.KeyAccessor
import org.pgpainless.key.info.KeyRingInfo
import org.pgpainless.util.Passphrase
class EncryptionOptions(
private val purpose: EncryptionPurpose,
private val api: PGPainless = PGPainless.getInstance()
) {
class EncryptionOptions(private val purpose: EncryptionPurpose, private val api: PGPainless) {
private val _encryptionMethods: MutableSet<PGPKeyEncryptionMethodGenerator> = mutableSetOf()
private val _encryptionKeys: MutableSet<OpenPGPComponentKey> = mutableSetOf()
private val _encryptionKeyIdentifiers: MutableSet<SubkeyIdentifier> = mutableSetOf()
@ -55,7 +52,7 @@ class EncryptionOptions(
val encryptionAlgorithmOverride
get() = _encryptionAlgorithmOverride
constructor() : this(EncryptionPurpose.ANY)
constructor(api: PGPainless) : this(EncryptionPurpose.ANY, api)
/**
* Set the evaluation date for certificate evaluation.
@ -426,11 +423,19 @@ class EncryptionOptions(
}
companion object {
@JvmStatic fun get() = EncryptionOptions()
@JvmOverloads
@JvmStatic
fun get(api: PGPainless = PGPainless.getInstance()) = EncryptionOptions(api)
@JvmStatic fun encryptCommunications() = EncryptionOptions(EncryptionPurpose.COMMUNICATIONS)
@JvmOverloads
@JvmStatic
fun encryptCommunications(api: PGPainless = PGPainless.getInstance()) =
EncryptionOptions(EncryptionPurpose.COMMUNICATIONS, api)
@JvmStatic fun encryptDataAtRest() = EncryptionOptions(EncryptionPurpose.STORAGE)
@JvmOverloads
@JvmStatic
fun encryptDataAtRest(api: PGPainless = PGPainless.getInstance()) =
EncryptionOptions(EncryptionPurpose.STORAGE, api)
/**
* Only encrypt to the first valid encryption capable subkey we stumble upon.

View file

@ -13,7 +13,7 @@ import org.pgpainless.algorithm.StreamEncoding
class ProducerOptions(
val encryptionOptions: EncryptionOptions?,
val signingOptions: SigningOptions?,
val api: PGPainless = PGPainless.getInstance()
val api: PGPainless
) {
private var _fileName: String = ""
@ -249,9 +249,13 @@ class ProducerOptions(
* @param signingOptions signing options
* @return builder
*/
@JvmOverloads
@JvmStatic
fun signAndEncrypt(encryptionOptions: EncryptionOptions, signingOptions: SigningOptions) =
ProducerOptions(encryptionOptions, signingOptions)
fun signAndEncrypt(
encryptionOptions: EncryptionOptions,
signingOptions: SigningOptions,
api: PGPainless = PGPainless.getInstance()
): ProducerOptions = ProducerOptions(encryptionOptions, signingOptions, api)
/**
* Sign some data without encryption.
@ -259,7 +263,12 @@ class ProducerOptions(
* @param signingOptions signing options
* @return builder
*/
@JvmStatic fun sign(signingOptions: SigningOptions) = ProducerOptions(null, signingOptions)
@JvmOverloads
@JvmStatic
fun sign(
signingOptions: SigningOptions,
api: PGPainless = PGPainless.getInstance()
): ProducerOptions = ProducerOptions(null, signingOptions, api)
/**
* Encrypt some data without signing.
@ -267,14 +276,21 @@ class ProducerOptions(
* @param encryptionOptions encryption options
* @return builder
*/
@JvmOverloads
@JvmStatic
fun encrypt(encryptionOptions: EncryptionOptions) = ProducerOptions(encryptionOptions, null)
fun encrypt(
encryptionOptions: EncryptionOptions,
api: PGPainless = PGPainless.getInstance()
): ProducerOptions = ProducerOptions(encryptionOptions, null, api)
/**
* Only wrap the data in an OpenPGP packet. No encryption or signing will be applied.
*
* @return builder
*/
@JvmStatic fun noEncryptionNoSigning() = ProducerOptions(null, null)
@JvmOverloads
@JvmStatic
fun noEncryptionNoSigning(api: PGPainless = PGPainless.getInstance()): ProducerOptions =
ProducerOptions(null, null, api)
}
}

View file

@ -27,7 +27,7 @@ import org.pgpainless.signature.subpackets.BaseSignatureSubpackets.Callback
import org.pgpainless.signature.subpackets.SignatureSubpackets
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper
class SigningOptions(val api: PGPainless = PGPainless.getInstance()) {
class SigningOptions(private val api: PGPainless) {
val signingMethods: Map<OpenPGPPrivateKey, SigningMethod> = mutableMapOf()
private var _hashAlgorithmOverride: HashAlgorithm? = null
@ -500,7 +500,9 @@ class SigningOptions(val api: PGPainless = PGPainless.getInstance()) {
}
companion object {
@JvmStatic fun get() = SigningOptions()
@JvmOverloads
@JvmStatic
fun get(api: PGPainless = PGPainless.getInstance()) = SigningOptions(api)
}
/** A method of signing. */

View file

@ -299,7 +299,7 @@ public class CanonicalizedDataEncryptionTest {
ByteArrayInputStream in = new ByteArrayInputStream(encrypted.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKeys, SecretKeyRingProtector.unprotectedKeys())
.addVerificationCert(publicKeys));
@ -329,7 +329,7 @@ public class CanonicalizedDataEncryptionTest {
ByteArrayInputStream in = new ByteArrayInputStream(encrypted.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKeys, SecretKeyRingProtector.unprotectedKeys())
.addVerificationCert(publicKeys));
@ -375,7 +375,7 @@ public class CanonicalizedDataEncryptionTest {
ByteArrayInputStream in = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKeys, SecretKeyRingProtector.unprotectedKeys())
.addVerificationCert(publicKeys));
@ -446,7 +446,7 @@ public class CanonicalizedDataEncryptionTest {
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(cipherIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(publicKeys));
Streams.pipeAll(decryptionStream, decrypted);

View file

@ -136,7 +136,7 @@ public class CertificateWithMissingSecretKeyTest {
// Test decryption
ByteArrayInputStream cipherIn = new ByteArrayInputStream(out.toByteArray());
ConsumerOptions consumerOptions = new ConsumerOptions()
ConsumerOptions consumerOptions = ConsumerOptions.get()
.addDecryptionKey(missingDecryptionSecKey);
assertThrows(MissingDecryptionMethodException.class, () ->

View file

@ -81,7 +81,7 @@ public class CleartextSignatureVerificationTest {
public void cleartextSignVerification_InMemoryMultiPassStrategy()
throws IOException, PGPException {
PGPPublicKeyRing signingKeys = TestKeys.getEmilPublicKeyRing();
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(signingKeys);
InMemoryMultiPassStrategy multiPassStrategy = MultiPassStrategy.keepMessageInMemory();
@ -108,7 +108,7 @@ public class CleartextSignatureVerificationTest {
public void cleartextSignVerification_FileBasedMultiPassStrategy()
throws IOException, PGPException {
PGPPublicKeyRing signingKeys = TestKeys.getEmilPublicKeyRing();
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(signingKeys);
File tempDir = TestUtils.createTempDirectory();
@ -164,7 +164,7 @@ public class CleartextSignatureVerificationTest {
throws IOException, PGPException {
PGPSignature signature = SignatureUtils.readSignatures(SIGNATURE).get(0);
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.addVerificationCert(TestKeys.getEmilPublicKeyRing())
.addVerificationOfDetachedSignature(signature);
@ -201,7 +201,7 @@ public class CleartextSignatureVerificationTest {
ByteArrayInputStream signedIn = new ByteArrayInputStream(signed.getBytes(StandardCharsets.UTF_8));
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
.onInputStream(signedIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(TestKeys.getEmilPublicKeyRing()));
ByteArrayOutputStream msgOut = new ByteArrayOutputStream();
@ -236,7 +236,7 @@ public class CleartextSignatureVerificationTest {
ByteArrayInputStream in = new ByteArrayInputStream(cleartextSigned.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(PGPainless.extractCertificate(secretKeys)));
out = new ByteArrayOutputStream();

View file

@ -74,7 +74,7 @@ public class MissingPassphraseForDecryptionTest {
return true;
}
};
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.INTERACTIVE)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));
@ -108,7 +108,7 @@ public class MissingPassphraseForDecryptionTest {
}
};
ConsumerOptions options = new ConsumerOptions()
ConsumerOptions options = ConsumerOptions.get()
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));

View file

@ -217,7 +217,7 @@ public class ModificationDetectionTests {
InputStream in = new ByteArrayInputStream(MESSAGE_MISSING_MDC.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(secretKeyRings, SecretKeyRingProtector.unprotectedKeys())
);
@ -234,7 +234,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_TAMPERED_CIPHERTEXT.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
);
@ -251,7 +251,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_TAMPERED_CIPHERTEXT.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
.setIgnoreMDCErrors(true)
);
@ -267,7 +267,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_TAMPERED_MDC.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
);
@ -284,7 +284,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_TAMPERED_MDC.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
.setIgnoreMDCErrors(true)
);
@ -299,7 +299,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_TRUNCATED_MDC.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
);
@ -313,7 +313,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_MDC_WITH_BAD_CTB.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
);
@ -330,7 +330,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_MDC_WITH_BAD_CTB.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
.setIgnoreMDCErrors(true)
);
@ -346,7 +346,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_MDC_WITH_BAD_LENGTH.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
);
@ -363,7 +363,7 @@ public class ModificationDetectionTests {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE_MDC_WITH_BAD_LENGTH.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(getDecryptionKey(), SecretKeyRingProtector.unprotectedKeys())
.setIgnoreMDCErrors(true)
);
@ -534,7 +534,7 @@ public class ModificationDetectionTests {
assertThrows(MessageNotIntegrityProtectedException.class, () -> PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeyRing,
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeyRing,
SecretKeyRingProtector.unlockAnyKeyWith(passphrase)))
);

View file

@ -743,7 +743,7 @@ public class OpenPgpInputStreamTest {
ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign()
.onOutputStream(signedOut)
.withOptions(ProducerOptions.sign(new SigningOptions()
.withOptions(ProducerOptions.sign(SigningOptions.get()
.addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys))
.setAsciiArmor(false)
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));

View file

@ -146,7 +146,7 @@ public class RecursionDepthTest {
assertThrows(MalformedOpenPgpMessageException.class, () -> {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions().addDecryptionKey(secretKey));
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKey));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, outputStream);

View file

@ -140,7 +140,7 @@ public class RejectWeakSymmetricAlgorithmDuringDecryptionTest {
assertThrows(UnacceptableAlgorithmException.class, () ->
PGPainless.decryptAndOrVerify()
.onInputStream(messageIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys))
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys))
);
}
@ -168,7 +168,7 @@ public class RejectWeakSymmetricAlgorithmDuringDecryptionTest {
assertThrows(UnacceptableAlgorithmException.class, () ->
PGPainless.decryptAndOrVerify()
.onInputStream(messageIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys))
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys))
);
}
@ -194,7 +194,7 @@ public class RejectWeakSymmetricAlgorithmDuringDecryptionTest {
InputStream messageIn = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
assertThrows(UnacceptableAlgorithmException.class, () ->
PGPainless.decryptAndOrVerify().onInputStream(messageIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys))
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys))
);
}
@ -219,7 +219,7 @@ public class RejectWeakSymmetricAlgorithmDuringDecryptionTest {
InputStream messageIn = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
PGPainless.decryptAndOrVerify().onInputStream(messageIn)
.withOptions(new ConsumerOptions().addDecryptionKey(secretKeys));
.withOptions(ConsumerOptions.get().addDecryptionKey(secretKeys));
}
}

View file

@ -53,7 +53,7 @@ public class VerifyWithMissingPublicKeyCallbackTest {
"is no different than saying you don't care about free speech because you have nothing to say.";
ByteArrayOutputStream signOut = new ByteArrayOutputStream();
EncryptionStream signingStream = PGPainless.encryptAndOrSign().onOutputStream(signOut)
.withOptions(ProducerOptions.sign(new SigningOptions().addInlineSignature(
.withOptions(ProducerOptions.sign(SigningOptions.get().addInlineSignature(
SecretKeyRingProtector.unprotectedKeys(),
signingSecKeys.getPGPSecretKeyRing(), DocumentSignatureType.CANONICAL_TEXT_DOCUMENT
)));
@ -62,7 +62,7 @@ public class VerifyWithMissingPublicKeyCallbackTest {
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(signOut.toByteArray()))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(unrelatedKeys)
.setMissingCertificateCallback(new OpenPGPKeyMaterialProvider.OpenPGPCertificateProvider() {
@Override

View file

@ -94,7 +94,7 @@ public class WrongSignerUserIdTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify().onInputStream(
new ByteArrayInputStream(messageWithWrongUserId.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKeys)
.addVerificationCert(certificate));

View file

@ -96,7 +96,7 @@ public class BcHashContextSignerTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(messageIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(certificate)
.addVerificationOfDetachedSignature(signature));

View file

@ -135,7 +135,7 @@ public class EncryptDecryptTest {
.onOutputStream(envelope)
.withOptions(ProducerOptions.signAndEncrypt(
EncryptionOptions.encryptCommunications().addRecipient(recipientPub),
new SigningOptions().addInlineSignature(keyDecryptor, senderSec, DocumentSignatureType.BINARY_DOCUMENT)
SigningOptions.get().addInlineSignature(keyDecryptor, senderSec, DocumentSignatureType.BINARY_DOCUMENT)
));
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
@ -156,7 +156,7 @@ public class EncryptDecryptTest {
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(envelopeIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(recipientSec, keyDecryptor)
.addVerificationCert(senderPub)
);
@ -184,7 +184,7 @@ public class EncryptDecryptTest {
ByteArrayOutputStream dummyOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(dummyOut)
.withOptions(ProducerOptions.sign(
new SigningOptions().addDetachedSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
SigningOptions.get().addDetachedSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
));
Streams.pipeAll(inputStream, signer);
signer.close();
@ -205,7 +205,7 @@ public class EncryptDecryptTest {
inputStream = new ByteArrayInputStream(testMessage.getBytes());
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
.onInputStream(inputStream)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(armorSig.getBytes()))
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(signingKeys))
);
@ -237,7 +237,7 @@ public class EncryptDecryptTest {
inputStream = new ByteArrayInputStream(signOut.toByteArray());
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
.onInputStream(inputStream)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(signingKeys))
);
signOut = new ByteArrayOutputStream();

View file

@ -66,7 +66,7 @@ public class EncryptionOptionsTest {
@Test
public void testOverrideEncryptionAlgorithmFailsForNULL() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertNull(options.getEncryptionAlgorithmOverride());
assertThrows(IllegalArgumentException.class, () -> options.overrideEncryptionAlgorithm(SymmetricKeyAlgorithm.NULL));
@ -76,7 +76,7 @@ public class EncryptionOptionsTest {
@Test
public void testOverrideEncryptionOptions() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertNull(options.getEncryptionAlgorithmOverride());
options.overrideEncryptionAlgorithm(SymmetricKeyAlgorithm.AES_128);
@ -105,7 +105,7 @@ public class EncryptionOptionsTest {
@Test
public void testAddRecipients_AllKeys() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
options.addRecipient(publicKeys, EncryptionOptions.encryptToAllCapableSubkeys());
Set<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys = options.getEncryptionKeys();
@ -117,7 +117,7 @@ public class EncryptionOptionsTest {
@Test
public void testAddEmptyRecipientsFails() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertThrows(IllegalArgumentException.class, () -> options.addRecipients(Collections.emptyList()));
assertThrows(IllegalArgumentException.class, () -> options.addRecipients(Collections.emptyList(),
ArrayList::new));
@ -125,14 +125,14 @@ public class EncryptionOptionsTest {
@Test
public void testAddEmptyPassphraseFails() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertThrows(IllegalArgumentException.class, () ->
options.addMessagePassphrase(Passphrase.emptyPassphrase()));
}
@Test
public void testAddRecipient_KeyWithoutEncryptionKeyFails() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
OpenPGPKey secretKeys = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
.addUserId("test@pgpainless.org")
@ -144,7 +144,7 @@ public class EncryptionOptionsTest {
@Test
public void testEncryptionKeySelectionStrategyEmpty_ThrowsAssertionError() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
() -> options.addRecipient(publicKeys, new EncryptionOptions.EncryptionKeySelector() {
@ -173,14 +173,14 @@ public class EncryptionOptionsTest {
PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(
Arrays.asList(publicKeys.getPGPPublicKeyRing(), secondKeyRing));
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
options.addRecipients(collection, EncryptionOptions.encryptToFirstSubkey());
assertEquals(2, options.getEncryptionKeyIdentifiers().size());
}
@Test
public void testAddRecipient_withValidUserId() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
options.addRecipient(publicKeys, "test@pgpainless.org", EncryptionOptions.encryptToFirstSubkey());
assertEquals(1, options.getEncryptionMethods().size());
@ -188,7 +188,7 @@ public class EncryptionOptionsTest {
@Test
public void testAddRecipient_withInvalidUserId() {
EncryptionOptions options = new EncryptionOptions();
EncryptionOptions options = EncryptionOptions.get();
assertThrows(KeyException.UnboundUserIdException.class, () -> options.addRecipient(publicKeys, "invalid@user.id"));
}
}

View file

@ -74,7 +74,7 @@ public class FileInformationTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);
@ -113,7 +113,7 @@ public class FileInformationTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);
@ -155,7 +155,7 @@ public class FileInformationTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);

View file

@ -55,7 +55,7 @@ public class RespectPreferredSymmetricAlgorithmDuringEncryptionTest {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign().onOutputStream(out)
.withOptions(
ProducerOptions.encrypt(new EncryptionOptions()
ProducerOptions.encrypt(EncryptionOptions.get()
.addRecipient(publicKeys) // no user-id passed
));
@ -67,7 +67,7 @@ public class RespectPreferredSymmetricAlgorithmDuringEncryptionTest {
out = new ByteArrayOutputStream();
encryptionStream = PGPainless.encryptAndOrSign().onOutputStream(out)
.withOptions(
ProducerOptions.encrypt(new EncryptionOptions()
ProducerOptions.encrypt(EncryptionOptions.get()
.addRecipient(publicKeys, "Bob Babbage <bob@openpgp.example>")
));
@ -79,7 +79,7 @@ public class RespectPreferredSymmetricAlgorithmDuringEncryptionTest {
out = new ByteArrayOutputStream();
encryptionStream = PGPainless.encryptAndOrSign().onOutputStream(out)
.withOptions(
ProducerOptions.encrypt(new EncryptionOptions()
ProducerOptions.encrypt(EncryptionOptions.get()
.addRecipient(publicKeys, "Bobby128 <bobby@aes128.example>")
));

View file

@ -69,7 +69,7 @@ public class SigningTest {
EncryptionOptions.encryptDataAtRest()
.addRecipients(keys)
.addRecipient(KeyRingUtils.publicKeyRingFrom(cryptieKeys)),
new SigningOptions().addInlineSignature(
SigningOptions.get().addInlineSignature(
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, cryptieSigningKey),
cryptieKeys, TestKeys.CRYPTIE_UID, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
).setAsciiArmor(true));
@ -94,7 +94,7 @@ public class SigningTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKeys(secretKeys, SecretKeyRingProtector.unprotectedKeys())
.addVerificationCerts(verificationKeys)
);
@ -119,7 +119,7 @@ public class SigningTest {
.getPGPSecretKeyRing();
SecretKeyRingProtector protector = SecretKeyRingProtector.unlockAnyKeyWith(Passphrase.fromPassword("password123"));
SigningOptions opts = new SigningOptions();
SigningOptions opts = SigningOptions.get();
// "bob" is not a valid user-id
assertThrows(KeyException.UnboundUserIdException.class,
() -> opts.addInlineSignature(protector, secretKeys, "bob",
@ -141,7 +141,7 @@ public class SigningTest {
final PGPSecretKeyRing fSecretKeys = secretKeys;
SigningOptions opts = new SigningOptions();
SigningOptions opts = SigningOptions.get();
// "alice" has been revoked
assertThrows(KeyException.UnboundUserIdException.class,
() -> opts.addInlineSignature(protector, fSecretKeys, "alice",
@ -154,7 +154,7 @@ public class SigningTest {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
SigningOptions options = new SigningOptions();
SigningOptions options = SigningOptions.get();
assertNull(options.getHashAlgorithmOverride());
options.overrideHashAlgorithm(HashAlgorithm.SHA224);
@ -192,7 +192,7 @@ public class SigningTest {
.build()
.getPGPSecretKeyRing();
SigningOptions options = new SigningOptions()
SigningOptions options = SigningOptions.get()
.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys,
DocumentSignatureType.BINARY_DOCUMENT);
String data = "Hello, World!\n";
@ -223,7 +223,7 @@ public class SigningTest {
.build()
.getPGPSecretKeyRing();
SigningOptions options = new SigningOptions()
SigningOptions options = SigningOptions.get()
.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys,
DocumentSignatureType.BINARY_DOCUMENT);
String data = "Hello, World!\n";
@ -251,7 +251,7 @@ public class SigningTest {
.build()
.getPGPSecretKeyRing();
SigningOptions options = new SigningOptions();
SigningOptions options = SigningOptions.get();
assertThrows(KeyException.UnacceptableSigningKeyException.class, () -> options.addDetachedSignature(
SecretKeyRingProtector.unprotectedKeys(), secretKeys, DocumentSignatureType.BINARY_DOCUMENT));
assertThrows(KeyException.UnacceptableSigningKeyException.class, () -> options.addInlineSignature(
@ -268,7 +268,7 @@ public class SigningTest {
.build()
.getPGPSecretKeyRing();
SigningOptions options = new SigningOptions();
SigningOptions options = SigningOptions.get();
assertThrows(KeyException.UnboundUserIdException.class, () ->
options.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys, "Bob",
DocumentSignatureType.BINARY_DOCUMENT));

View file

@ -154,7 +154,7 @@ public class Encrypt {
EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob)
.addRecipient(certificateAlice),
new SigningOptions()
SigningOptions.get()
.addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
).setAsciiArmor(true)
);
@ -167,7 +167,7 @@ public class Encrypt {
// Decrypt and verify signatures
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(keyBob, protectorBob)
.addVerificationCert(certificateAlice)
);
@ -209,7 +209,7 @@ public class Encrypt {
// Decrypt
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(asciiCiphertext.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions().addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3")));
.withOptions(ConsumerOptions.get().addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3")));
ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, plaintext);
@ -273,7 +273,7 @@ public class Encrypt {
// Decrypt and verify signatures
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(keyBob, protectorBob)
.addVerificationCert(certificateAlice)
);

View file

@ -106,7 +106,7 @@ public class StupidAlgorithmPreferenceEncryptionTest {
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
.onOutputStream(out)
.withOptions(ProducerOptions.encrypt(
new EncryptionOptions().addRecipient(certificate)
EncryptionOptions.get().addRecipient(certificate)
));
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));

View file

@ -144,7 +144,7 @@ public class FixUserIdDoesNotBreakEncryptionCapabilityTest {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
.onOutputStream(out)
.withOptions(ProducerOptions.encrypt(new EncryptionOptions()
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.addRecipient(cert)));
encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8));
@ -157,7 +157,7 @@ public class FixUserIdDoesNotBreakEncryptionCapabilityTest {
ByteArrayOutputStream plain = new ByteArrayOutputStream();
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(edited));
Streams.pipeAll(decryptionStream, plain);

View file

@ -111,7 +111,7 @@ public class S2KUsageFixTest {
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(keys, protector));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, out);

View file

@ -1384,7 +1384,7 @@ public class CertificateValidatorTest {
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(DATA.getBytes(StandardCharsets.UTF_8)))
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(PGPainless.readKeyRing().publicKeyRing(CERT))
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(SIG.getBytes(StandardCharsets.UTF_8))));

View file

@ -144,7 +144,7 @@ public class IgnoreMarkerPacketsTest {
InputStream messageIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(messageIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addVerificationCert(publicKeys)
.addVerificationOfDetachedSignature(signature)
);
@ -193,7 +193,7 @@ public class IgnoreMarkerPacketsTest {
InputStream messageIn = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(messageIn)
.withOptions(new ConsumerOptions()
.withOptions(ConsumerOptions.get()
.addDecryptionKey(secretKeys)
.addVerificationCert(publicKeys)
);