@@ -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+ }
0 commit comments