From cd19f91d77eb566ede20f82f26dd33c5755a5ef6 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 7 Apr 2021 21:30:51 +0200 Subject: [PATCH] Allow specification of file name and for-your-eyes-only flag --- .../encryption_signing/EncryptionBuilder.java | 10 +++++-- .../EncryptionBuilderInterface.java | 26 ++++++++++++++++++- .../encryption_signing/EncryptionStream.java | 14 +++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java index 866b417b..d4b1af04 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java @@ -68,6 +68,8 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { private HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256; private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED; private boolean asciiArmor = false; + private String fileName; + private boolean forYourEyesOnly; public EncryptionBuilder() { this.purpose = EncryptionStream.Purpose.COMMUNICATIONS; @@ -78,8 +80,10 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public ToRecipients onOutputStream(@Nonnull OutputStream outputStream) { + public ToRecipients onOutputStream(@Nonnull OutputStream outputStream, String fileName, boolean forYourEyesOnly) { this.outputStream = outputStream; + this.fileName = fileName == null ? "" : fileName; + this.forYourEyesOnly = forYourEyesOnly; return new ToRecipientsImpl(); } @@ -430,7 +434,9 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { EncryptionBuilder.this.symmetricKeyAlgorithm, EncryptionBuilder.this.hashAlgorithm, EncryptionBuilder.this.compressionAlgorithm, - EncryptionBuilder.this.asciiArmor); + EncryptionBuilder.this.asciiArmor, + fileName, + forYourEyesOnly); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java index df9a0f84..8d33036b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java @@ -47,7 +47,31 @@ public interface EncryptionBuilderInterface { * @param outputStream output stream of the plain data. * @return api handle */ - ToRecipients onOutputStream(@Nonnull OutputStream outputStream); + default ToRecipients onOutputStream(@Nonnull OutputStream outputStream) { + return onOutputStream(outputStream,false); + } + /** + * Create a {@link EncryptionStream} on an {@link OutputStream} that contains the plain data which shall + * be encrypted and/or signed. + * + * @param outputStream outputStream + * @param forYourEyesOnly flag indicating that the data is intended for the recipients eyes only + * @return api handle + */ + default ToRecipients onOutputStream(@Nonnull OutputStream outputStream, boolean forYourEyesOnly) { + return onOutputStream(outputStream, "", forYourEyesOnly); + } + + /** + * Creates a {@link EncryptionStream} on an {@link OutputStream} that contains the plain data which shall + * be encrypted and/or signed. + * + * @param outputStream outputStream + * @param fileName name of the file (or "" if the encrypted data is not a file) + * @param forYourEyesOnly flag indicating that the data is intended for the recipients eyes only + * @return api handle + */ + ToRecipients onOutputStream(@Nonnull OutputStream outputStream, String fileName, boolean forYourEyesOnly); interface ToRecipients { diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java index 36d14fdb..58a227b1 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java @@ -117,7 +117,9 @@ public final class EncryptionStream extends OutputStream { @Nonnull SymmetricKeyAlgorithm symmetricKeyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull CompressionAlgorithm compressionAlgorithm, - boolean asciiArmor) + boolean asciiArmor, + @Nonnull String fileName, + boolean forYourEyesOnly) throws IOException, PGPException { this.symmetricKeyAlgorithm = symmetricKeyAlgorithm; @@ -136,7 +138,7 @@ public final class EncryptionStream extends OutputStream { prepareSigning(); prepareCompression(); prepareOnePassSignatures(); - prepareLiteralDataProcessing(); + prepareLiteralDataProcessing(fileName, forYourEyesOnly); prepareResultBuilder(); } @@ -225,10 +227,14 @@ public final class EncryptionStream extends OutputStream { } } - private void prepareLiteralDataProcessing() throws IOException { + private void prepareLiteralDataProcessing(@Nonnull String fileName, boolean forYourEyesOnly) throws IOException { literalDataGenerator = new PGPLiteralDataGenerator(); + String name = fileName; + if (forYourEyesOnly) { + name = PGPLiteralData.CONSOLE; + } literalDataStream = literalDataGenerator.open(outermostStream, - PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[BUFFER_SIZE]); + PGPLiteralData.BINARY, name, new Date(), new byte[BUFFER_SIZE]); outermostStream = literalDataStream; }