mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-14 04:39:39 +02:00
Allow customization of ASCII armor comment and version headers
This commit is contained in:
parent
a678ff1b6e
commit
fd867bbfbe
2 changed files with 110 additions and 1 deletions
|
@ -22,13 +22,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import org.bouncycastle.bcpg.ArmoredInputStream;
|
||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||
import org.bouncycastle.openpgp.PGPCompressedData;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPLiteralData;
|
||||
import org.bouncycastle.openpgp.PGPObjectFactory;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.algorithm.HashAlgorithm;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
|
@ -120,4 +128,54 @@ public class ArmorUtilsTest {
|
|||
String ascii = ArmorUtils.toAsciiArmoredString(in);
|
||||
assertTrue(ascii.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCustomVersionHeader() throws IOException {
|
||||
ArmoredOutputStreamFactory.setVersionInfo("MyVeryFirstOpenPGPProgram 1.0");
|
||||
ArmoredOutputStreamFactory.setComment("This is a comment\nThat spans multiple\nLines!");
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armorOut = ArmoredOutputStreamFactory.get(out);
|
||||
|
||||
byte[] data = "This is a very secret message that nobody is allowed to read.".getBytes(StandardCharsets.UTF_8);
|
||||
armorOut.write(data);
|
||||
armorOut.close();
|
||||
|
||||
assertEquals("-----BEGIN PGP MESSAGE-----\n" +
|
||||
"Version: MyVeryFirstOpenPGPProgram 1.0\n" +
|
||||
"Comment: This is a comment\n" +
|
||||
"Comment: That spans multiple\n" +
|
||||
"Comment: Lines!\n" +
|
||||
"\n" +
|
||||
"VGhpcyBpcyBhIHZlcnkgc2VjcmV0IG1lc3NhZ2UgdGhhdCBub2JvZHkgaXMgYWxs\n" +
|
||||
"b3dlZCB0byByZWFkLg==\n" +
|
||||
"=XMZb\n" +
|
||||
"-----END PGP MESSAGE-----\n", out.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeExampleTest() throws IOException, PGPException {
|
||||
String armored = "-----BEGIN PGP MESSAGE-----\n" +
|
||||
"Version: OpenPrivacy 0.99\n" +
|
||||
"\n" +
|
||||
"yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS\n" +
|
||||
"vBSFjNSiVHsuAA==\n" +
|
||||
"=njUN\n" +
|
||||
"-----END PGP MESSAGE-----";
|
||||
InputStream inputStream = PGPUtil.getDecoderStream(new ByteArrayInputStream(armored.getBytes(StandardCharsets.UTF_8)));
|
||||
PGPObjectFactory factory = new BcPGPObjectFactory(inputStream);
|
||||
PGPCompressedData compressed = (PGPCompressedData) factory.nextObject();
|
||||
factory = new BcPGPObjectFactory(compressed.getDataStream());
|
||||
PGPLiteralData literal = (PGPLiteralData) factory.nextObject();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
assertEquals("_CONSOLE", literal.getFileName());
|
||||
Streams.pipeAll(literal.getInputStream(), out);
|
||||
assertEquals("Can't anyone keep a secret around here?\n", out.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void resetHeaders() {
|
||||
ArmoredOutputStreamFactory.resetComment();
|
||||
ArmoredOutputStreamFactory.resetVersionInfo();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue