Allow specification of file name and for-your-eyes-only flag

This commit is contained in:
Paul Schaub 2021-04-07 21:30:51 +02:00
parent d082f126b3
commit cd19f91d77
3 changed files with 43 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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 {

View File

@ -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;
}