From 86b06ee5e3f7040505b5e26f610938820b89eef7 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 11 Nov 2022 13:45:25 +0100 Subject: [PATCH] SOP: Hide armor version header by default --- .../util/ArmoredOutputStreamFactory.java | 18 ++++++++++++++---- .../main/java/org/pgpainless/sop/SOPImpl.java | 5 +++++ .../org/pgpainless/sop/ExtractCertTest.java | 2 -- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java b/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java index 403115ba..67cc1c20 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java @@ -31,7 +31,11 @@ public final class ArmoredOutputStreamFactory { */ public static ArmoredOutputStream get(OutputStream outputStream) { ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream); - armoredOutputStream.setHeader(ArmorUtils.HEADER_VERSION, version); + armoredOutputStream.clearHeaders(); + if (version != null && !version.isEmpty()) { + armoredOutputStream.setHeader(ArmorUtils.HEADER_VERSION, version); + } + for (String comment : comment) { ArmorUtils.addCommentHeader(armoredOutputStream, comment); } @@ -55,10 +59,16 @@ public final class ArmoredOutputStreamFactory { * @param versionString version string */ public static void setVersionInfo(String versionString) { - if (versionString == null || versionString.trim().isEmpty()) { - throw new IllegalArgumentException("Version Info MUST NOT be null NOR empty."); + if (versionString == null) { + version = null; + return; + } + String trimmed = versionString.trim(); + if (trimmed.isEmpty()) { + version = null; + } else { + version = trimmed; } - version = versionString; } /** diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/SOPImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/SOPImpl.java index 35ff8994..28772f10 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/SOPImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/SOPImpl.java @@ -4,6 +4,7 @@ package org.pgpainless.sop; +import org.pgpainless.util.ArmoredOutputStreamFactory; import sop.SOP; import sop.operation.Armor; import sop.operation.Dearmor; @@ -20,6 +21,10 @@ import sop.operation.Version; public class SOPImpl implements SOP { + static { + ArmoredOutputStreamFactory.setVersionInfo(null); + } + @Override public Version version() { return new VersionImpl(); diff --git a/pgpainless-sop/src/test/java/org/pgpainless/sop/ExtractCertTest.java b/pgpainless-sop/src/test/java/org/pgpainless/sop/ExtractCertTest.java index 84a1f471..b3910482 100644 --- a/pgpainless-sop/src/test/java/org/pgpainless/sop/ExtractCertTest.java +++ b/pgpainless-sop/src/test/java/org/pgpainless/sop/ExtractCertTest.java @@ -18,7 +18,6 @@ import sop.exception.SOPGPException; public class ExtractCertTest { public static final String key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + - "Version: PGPainless\n" + "Comment: A8D9 9FF4 C8DD BBA6 C610 A6B7 9ACB 2195 A9BC DF5B\n" + "Comment: Alice \n" + "\n" + @@ -42,7 +41,6 @@ public class ExtractCertTest { "-----END PGP PRIVATE KEY BLOCK-----\n"; public static final String cert = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + - "Version: PGPainless\n" + "Comment: A8D9 9FF4 C8DD BBA6 C610 A6B7 9ACB 2195 A9BC DF5B\n" + "Comment: Alice \n" + "\n" +