1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +01:00

Rename InputStreamFactory -> DecryptionStreamFactory

This commit is contained in:
Paul Schaub 2018-06-10 17:55:51 +02:00
parent 8647fde896
commit 992432fc59
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 36 additions and 38 deletions

View file

@ -90,7 +90,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
@Override @Override
public PainlessResult.ResultAndInputStream build() throws IOException, PGPException { public PainlessResult.ResultAndInputStream build() throws IOException, PGPException {
return InputStreamFactory.create(inputStream, return DecryptionStreamFactory.create(inputStream,
decryptionKeys, decryptionKeyDecryptor, verificationKeys, trustedKeyIds, missingPublicKeyCallback); decryptionKeys, decryptionKeyDecryptor, verificationKeys, trustedKeyIds, missingPublicKeyCallback);
} }
} }

View file

@ -35,7 +35,7 @@ import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
public class InputStreamFactory { public class DecryptionStreamFactory {
private final PGPSecretKeyRingCollection decryptionKeys; private final PGPSecretKeyRingCollection decryptionKeys;
private final SecretKeyRingProtector decryptionKeyDecryptor; private final SecretKeyRingProtector decryptionKeyDecryptor;
@ -48,12 +48,11 @@ public class InputStreamFactory {
private final KeyFingerPrintCalculator fingerCalc = new BcKeyFingerprintCalculator(); private final KeyFingerPrintCalculator fingerCalc = new BcKeyFingerprintCalculator();
private final Map<Long, PGPOnePassSignature> verifiableOnePassSignatures = new HashMap<>(); private final Map<Long, PGPOnePassSignature> verifiableOnePassSignatures = new HashMap<>();
private InputStreamFactory(PGPSecretKeyRingCollection decryptionKeys, private DecryptionStreamFactory(PGPSecretKeyRingCollection decryptionKeys,
SecretKeyRingProtector decryptor, SecretKeyRingProtector decryptor,
Set<PGPPublicKeyRing> verificationKeys, Set<PGPPublicKeyRing> verificationKeys,
Set<Long> trustedKeyIds, Set<Long> trustedKeyIds,
MissingPublicKeyCallback missingPublicKeyCallback) MissingPublicKeyCallback missingPublicKeyCallback) {
throws IOException {
this.decryptionKeys = decryptionKeys; this.decryptionKeys = decryptionKeys;
this.decryptionKeyDecryptor = decryptor; this.decryptionKeyDecryptor = decryptor;
this.verificationKeys.addAll(verificationKeys != null ? verificationKeys : Collections.emptyList()); this.verificationKeys.addAll(verificationKeys != null ? verificationKeys : Collections.emptyList());
@ -69,7 +68,7 @@ public class InputStreamFactory {
MissingPublicKeyCallback missingPublicKeyCallback) MissingPublicKeyCallback missingPublicKeyCallback)
throws IOException, PGPException { throws IOException, PGPException {
InputStreamFactory factory = new InputStreamFactory(decryptionKeys, DecryptionStreamFactory factory = new DecryptionStreamFactory(decryptionKeys,
decryptor, decryptor,
verificationKeys, verificationKeys,
trustedKeyIds, trustedKeyIds,

View file

@ -32,51 +32,50 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
private static final Charset UTF8 = Charset.forName("UTF-8"); private static final Charset UTF8 = Charset.forName("UTF-8");
@Test @Test
public void freshRsaTest() public void freshKeysRsaToRsaTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
PGPSecretKeyRing aliceSec = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._4096); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("hatter@wonderland.lit", RsaLength._4096);
PGPSecretKeyRing hatterSec = PGPainless.generateKeyRing().simpleRsaKeyRing("hatter@wonderland.lit", RsaLength._4096); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._4096);
encryptDecryptForSecretKeyRings(aliceSec, hatterSec); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Test @Test
public void freshEcTest() throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException, public void freshKeysEcToEcTest() throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException { InvalidAlgorithmParameterException {
PGPSecretKeyRing aliceSec = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit"); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("hatter@wonderland.lit");
PGPSecretKeyRing hatterSec = PGPainless.generateKeyRing().simpleEcKeyRing("hatter@wonderland.lit"); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit");
encryptDecryptForSecretKeyRings(aliceSec, hatterSec); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Test @Test
public void freshRsaEcTest() public void freshKeysEcToRsaTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
PGPSecretKeyRing aliceSec = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._4096); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("hatter@wonderland.lit");
PGPSecretKeyRing hatterSec = PGPainless.generateKeyRing().simpleEcKeyRing("hatter@wonderland.lit"); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._4096);
encryptDecryptForSecretKeyRings(aliceSec, hatterSec); encryptDecryptForSecretKeyRings(sender, recipient);
} }
@Test @Test
public void freshEcRsaTest() public void freshKeysRsaToEcTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException { IOException {
PGPSecretKeyRing aliceSec = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit"); PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("hatter@wonderland.lit", RsaLength._4096);
PGPSecretKeyRing hatterSec = PGPainless.generateKeyRing().simpleRsaKeyRing("hatter@wonderland.lit", RsaLength._4096); PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit");
encryptDecryptForSecretKeyRings(sender, recipient);
encryptDecryptForSecretKeyRings(aliceSec, hatterSec);
} }
@Ignore @Ignore
private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing aliceSec, PGPSecretKeyRing hatterSec) private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing sender, PGPSecretKeyRing recipient)
throws PGPException, throws PGPException,
IOException { IOException {
PGPPublicKeyRing alicePub = BCUtil.publicKeyRingFromSecretKeyRing(aliceSec); PGPPublicKeyRing recipientPub = BCUtil.publicKeyRingFromSecretKeyRing(recipient);
PGPPublicKeyRing hatterPub = BCUtil.publicKeyRingFromSecretKeyRing(hatterSec); PGPPublicKeyRing senderPub = BCUtil.publicKeyRingFromSecretKeyRing(sender);
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector(); SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
@ -87,16 +86,13 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
"Unfold the imagined happiness that both\n" + "Unfold the imagined happiness that both\n" +
"Receive in either by this dear encounter.").getBytes(UTF8); "Receive in either by this dear encounter.").getBytes(UTF8);
Logger.getLogger(EncryptDecryptTest.class.getName())
.log(Level.INFO, " " + secretMessage.length);
ByteArrayOutputStream envelope = new ByteArrayOutputStream(); ByteArrayOutputStream envelope = new ByteArrayOutputStream();
OutputStream encryptor = PGPainless.createEncryptor() OutputStream encryptor = PGPainless.createEncryptor()
.onOutputStream(envelope) .onOutputStream(envelope)
.toRecipients(Collections.singleton(alicePub)) .toRecipients(Collections.singleton(recipientPub))
.usingSecureAlgorithms() .usingSecureAlgorithms()
.signWith(hatterSec, keyDecryptor) .signWith(sender, keyDecryptor)
.noArmor(); .noArmor();
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor); Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
@ -108,8 +104,8 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage); ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
PainlessResult.ResultAndInputStream resultAndInputStream = PGPainless.createDecryptor() PainlessResult.ResultAndInputStream resultAndInputStream = PGPainless.createDecryptor()
.onInputStream(envelopeIn) .onInputStream(envelopeIn)
.decryptWith(BCUtil.keyRingsToKeyRingCollection(aliceSec), keyDecryptor) .decryptWith(BCUtil.keyRingsToKeyRingCollection(recipient), keyDecryptor)
.verifyWith(Collections.singleton(TestKeys.ROMEO_KEY_ID), BCUtil.keyRingsToKeyRingCollection(hatterPub)) .verifyWith(Collections.singleton(TestKeys.ROMEO_KEY_ID), BCUtil.keyRingsToKeyRingCollection(senderPub))
.ignoreMissingPublicKeys() .ignoreMissingPublicKeys()
.build(); .build();
@ -120,8 +116,11 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
decryptor.close(); decryptor.close();
assertTrue(Arrays.equals(secretMessage, ((ByteArrayOutputStream) decryptedSecretMessage).toByteArray())); assertTrue(Arrays.equals(secretMessage, ((ByteArrayOutputStream) decryptedSecretMessage).toByteArray()));
PainlessResult result = resultAndInputStream.getResult(); PainlessResult result = resultAndInputStream.getResult();
assertTrue(result.containsVerifiedSignatureFrom(hatterPub)); assertTrue(result.containsVerifiedSignatureFrom(senderPub));
assertTrue(result.isIntegrityProtected());
assertTrue(result.isSigned());
assertTrue(result.isEncrypted());
assertTrue(result.isVerified());
} }
} }