1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-18 02:12:06 +01:00

SOP : Do not armor already-armored data.

This commit is contained in:
Paul Schaub 2022-11-11 13:18:22 +01:00
parent 2d6f9738ec
commit 48005da7f3
2 changed files with 18 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import java.io.OutputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.decryption_verification.OpenPgpInputStream;
import org.pgpainless.util.ArmoredOutputStreamFactory; import org.pgpainless.util.ArmoredOutputStreamFactory;
import sop.Ready; import sop.Ready;
import sop.enums.ArmorLabel; import sop.enums.ArmorLabel;
@ -31,8 +32,21 @@ public class ArmorImpl implements Armor {
public void writeTo(OutputStream outputStream) throws IOException { public void writeTo(OutputStream outputStream) throws IOException {
// By buffering the output stream, we can improve performance drastically // By buffering the output stream, we can improve performance drastically
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
// Determine nature of the given data
OpenPgpInputStream openPgpIn = new OpenPgpInputStream(data);
openPgpIn.reset();
if (openPgpIn.isAsciiArmored()) {
// armoring already-armored data is an idempotent operation
Streams.pipeAll(openPgpIn, bufferedOutputStream);
bufferedOutputStream.flush();
openPgpIn.close();
return;
}
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(bufferedOutputStream); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(bufferedOutputStream);
Streams.pipeAll(data, armor); Streams.pipeAll(openPgpIn, armor);
bufferedOutputStream.flush(); bufferedOutputStream.flush();
armor.close(); armor.close();
} }

View file

@ -29,7 +29,9 @@ public class ArmorTest {
@Test @Test
public void armor() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { public void armor() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
byte[] data = PGPainless.generateKeyRing().modernKeyRing("Alice").getEncoded(); byte[] data = PGPainless.generateKeyRing().modernKeyRing("Alice").getEncoded();
byte[] knownGoodArmor = ArmorUtils.toAsciiArmoredString(data).getBytes(StandardCharsets.UTF_8); byte[] knownGoodArmor = ArmorUtils.toAsciiArmoredString(data)
.replace("Version: PGPainless\n", "") // armor command does not add version anymore
.getBytes(StandardCharsets.UTF_8);
byte[] armored = new SOPImpl() byte[] armored = new SOPImpl()
.armor() .armor()
.data(data) .data(data)