Skip to content

Commit

Permalink
Added a builder method to sign/encrypt as a text document rather than…
Browse files Browse the repository at this point in the history
… binary data.
  • Loading branch information
bjansen authored and bjansen-caps committed Mar 22, 2021
1 parent 3523ca7 commit cb01717
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class BuildEncryptionOutputStreamAPI {
private String signWith;
private Set<PGPPublicKey> recipients;
private boolean armorOutput;
private boolean textMode;

// Signature

Expand Down Expand Up @@ -95,6 +96,8 @@ private KeySelectionStrategy getKeySelectionStrategy() {

public interface Build {

Build textMode();

OutputStream andWriteTo(OutputStream sinkForEncryptedData)
throws PGPException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException, IOException;
}
Expand Down Expand Up @@ -502,6 +505,12 @@ public Build armorAsciiOutput() {

public final class Builder implements Build {

@Override
public Build textMode() {
BuildEncryptionOutputStreamAPI.this.textMode = true;
return this;
}

@Override
public OutputStream andWriteTo(OutputStream sinkForEncryptedData)
throws PGPException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
Expand All @@ -513,7 +522,8 @@ public OutputStream andWriteTo(OutputStream sinkForEncryptedData)
BuildEncryptionOutputStreamAPI.this.sinkForEncryptedData,
getKeySelectionStrategy(),
BuildEncryptionOutputStreamAPI.this.armorOutput,
BuildEncryptionOutputStreamAPI.this.recipients);
BuildEncryptionOutputStreamAPI.this.recipients,
BuildEncryptionOutputStreamAPI.this.textMode);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private PGPEncryptingStream(final KeyringConfig config, final PGPAlgorithmSuite
* @param keySelectionStrategy selection strategy
* @param armor armor the file (true) or use binary.
* @param encryptTo encrypt to
* @param textMode simulates GnuPG's {@code --textmode} flag
*
* @return stream where plaintext gets written into
*
Expand All @@ -93,7 +94,8 @@ public static OutputStream create(final KeyringConfig config,
final OutputStream cipherTextSink,
final KeySelectionStrategy keySelectionStrategy,
final boolean armor,
final Set<PGPPublicKey> encryptTo)
final Set<PGPPublicKey> encryptTo,
final boolean textMode)
throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException {

requireNonNull(config, "callback must not be null");
Expand All @@ -109,7 +111,7 @@ public static OutputStream create(final KeyringConfig config,
}

final PGPEncryptingStream encryptingStream = new PGPEncryptingStream(config, algorithmSuite);
encryptingStream.setup(cipherTextSink, signingUid, encryptTo, keySelectionStrategy, armor);
encryptingStream.setup(cipherTextSink, signingUid, encryptTo, keySelectionStrategy, armor, textMode);
return encryptingStream;
}

Expand All @@ -122,6 +124,7 @@ public static OutputStream create(final KeyringConfig config,
* @param pubEncKeys the pub enc keys
* @param keySelectionStrategy key selection strategy (for signatures)
* @param armor if OutputStream should be "armored", that means base64 encoded
* @param textMode simulates GnuPG's {@code --textmode} flag
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws PGPException the pGP exception
Expand All @@ -134,7 +137,8 @@ private void setup(final OutputStream cipherTextSink,
@Nullable final String signingUid,
final Set<PGPPublicKey> pubEncKeys,
final KeySelectionStrategy keySelectionStrategy,
final boolean armor) throws
final boolean armor,
final boolean textMode) throws
IOException, PGPException {
isDoSign = signingUid != null;

Expand Down Expand Up @@ -186,7 +190,7 @@ private void setup(final OutputStream cipherTextSink,
new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(),
algorithmSuite.getHashAlgorithmCode().getAlgorithmId()));

signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
signatureGenerator.init(textMode ? PGPSignature.CANONICAL_TEXT_DOCUMENT : PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

final Iterator<?> userIDs = pgpSec.getPublicKey().getUserIDs();
if (userIDs.hasNext()) {
Expand All @@ -208,7 +212,7 @@ private void setup(final OutputStream cipherTextSink,

encryptionDataStreamGenerator = new PGPLiteralDataGenerator();
encryptionDataStream = encryptionDataStreamGenerator
.open(compressionStream, PGPLiteralData.BINARY, "", new Date(), new byte[1 << 16]);
.open(compressionStream, textMode ? PGPLiteralData.TEXT : PGPLiteralData.BINARY, "", new Date(), new byte[1 << 16]);
}

@Override
Expand Down Expand Up @@ -268,4 +272,4 @@ public void close() throws IOException {
isClosed = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ void encryptAndSign(final InputStream in, OutputStream out, final Set<PGPPublicK

try (final OutputStream encryptionStream = PGPEncryptingStream
.create(config, algorithmSuite, signatureUid, out, keySelectionStrategy, armor,
pubEncKeys)) {
pubEncKeys, false)) {
Streams.pipeAll(in, encryptionStream);
encryptionStream.flush();
}
out.flush();
}
}
}

0 comments on commit cb01717

Please sign in to comment.