Skip to content

Commit cb01717

Browse files
bjansenbjansen-caps
authored andcommitted
Added a builder method to sign/encrypt as a text document rather than binary data.
1 parent 3523ca7 commit cb01717

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/main/java/name/neuhalfen/projects/crypto/bouncycastle/openpgp/BuildEncryptionOutputStreamAPI.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class BuildEncryptionOutputStreamAPI {
5151
private String signWith;
5252
private Set<PGPPublicKey> recipients;
5353
private boolean armorOutput;
54+
private boolean textMode;
5455

5556
// Signature
5657

@@ -95,6 +96,8 @@ private KeySelectionStrategy getKeySelectionStrategy() {
9596

9697
public interface Build {
9798

99+
Build textMode();
100+
98101
OutputStream andWriteTo(OutputStream sinkForEncryptedData)
99102
throws PGPException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException, IOException;
100103
}
@@ -502,6 +505,12 @@ public Build armorAsciiOutput() {
502505

503506
public final class Builder implements Build {
504507

508+
@Override
509+
public Build textMode() {
510+
BuildEncryptionOutputStreamAPI.this.textMode = true;
511+
return this;
512+
}
513+
505514
@Override
506515
public OutputStream andWriteTo(OutputStream sinkForEncryptedData)
507516
throws PGPException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
@@ -513,7 +522,8 @@ public OutputStream andWriteTo(OutputStream sinkForEncryptedData)
513522
BuildEncryptionOutputStreamAPI.this.sinkForEncryptedData,
514523
getKeySelectionStrategy(),
515524
BuildEncryptionOutputStreamAPI.this.armorOutput,
516-
BuildEncryptionOutputStreamAPI.this.recipients);
525+
BuildEncryptionOutputStreamAPI.this.recipients,
526+
BuildEncryptionOutputStreamAPI.this.textMode);
517527

518528
}
519529
}

src/main/java/name/neuhalfen/projects/crypto/bouncycastle/openpgp/encrypting/PGPEncryptingStream.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private PGPEncryptingStream(final KeyringConfig config, final PGPAlgorithmSuite
7878
* @param keySelectionStrategy selection strategy
7979
* @param armor armor the file (true) or use binary.
8080
* @param encryptTo encrypt to
81+
* @param textMode simulates GnuPG's {@code --textmode} flag
8182
*
8283
* @return stream where plaintext gets written into
8384
*
@@ -93,7 +94,8 @@ public static OutputStream create(final KeyringConfig config,
9394
final OutputStream cipherTextSink,
9495
final KeySelectionStrategy keySelectionStrategy,
9596
final boolean armor,
96-
final Set<PGPPublicKey> encryptTo)
97+
final Set<PGPPublicKey> encryptTo,
98+
final boolean textMode)
9799
throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException {
98100

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

111113
final PGPEncryptingStream encryptingStream = new PGPEncryptingStream(config, algorithmSuite);
112-
encryptingStream.setup(cipherTextSink, signingUid, encryptTo, keySelectionStrategy, armor);
114+
encryptingStream.setup(cipherTextSink, signingUid, encryptTo, keySelectionStrategy, armor, textMode);
113115
return encryptingStream;
114116
}
115117

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

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

189-
signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
193+
signatureGenerator.init(textMode ? PGPSignature.CANONICAL_TEXT_DOCUMENT : PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
190194

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

209213
encryptionDataStreamGenerator = new PGPLiteralDataGenerator();
210214
encryptionDataStream = encryptionDataStreamGenerator
211-
.open(compressionStream, PGPLiteralData.BINARY, "", new Date(), new byte[1 << 16]);
215+
.open(compressionStream, textMode ? PGPLiteralData.TEXT : PGPLiteralData.BINARY, "", new Date(), new byte[1 << 16]);
212216
}
213217

214218
@Override
@@ -268,4 +272,4 @@ public void close() throws IOException {
268272
isClosed = true;
269273
}
270274
}
271-
}
275+
}

src/test/java/name/neuhalfen/projects/crypto/bouncycastle/openpgp/encrypting/EncryptWithOpenPGPTestDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ void encryptAndSign(final InputStream in, OutputStream out, final Set<PGPPublicK
9191

9292
try (final OutputStream encryptionStream = PGPEncryptingStream
9393
.create(config, algorithmSuite, signatureUid, out, keySelectionStrategy, armor,
94-
pubEncKeys)) {
94+
pubEncKeys, false)) {
9595
Streams.pipeAll(in, encryptionStream);
9696
encryptionStream.flush();
9797
}
9898
out.flush();
9999
}
100-
}
100+
}

0 commit comments

Comments
 (0)