1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 06:11:08 +01:00

Allow customization of ASCII armor comment and version headers

This commit is contained in:
Paul Schaub 2021-08-14 13:56:16 +02:00
parent a678ff1b6e
commit fd867bbfbe
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 110 additions and 1 deletions

View file

@ -24,11 +24,62 @@ import org.bouncycastle.bcpg.ArmoredOutputStream;
*/
public class ArmoredOutputStreamFactory {
public static final String VERSION = "PGPainless";
public static final String PGPAINLESS = "PGPainless";
private static String VERSION = PGPAINLESS;
public static String[] COMMENT = new String[0];
public static ArmoredOutputStream get(OutputStream outputStream) {
ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream);
armoredOutputStream.setHeader(ArmorUtils.HEADER_VERSION, VERSION);
for (String comment : COMMENT) {
ArmorUtils.addCommentHeader(armoredOutputStream, comment);
}
return armoredOutputStream;
}
/**
* Overwrite the version header of ASCII armors with a custom value.
* Newlines in the version info string result in multiple version header entries.
*
* @param version version string
*/
public static void setVersionInfo(String version) {
if (version == null || version.trim().isEmpty()) {
throw new IllegalArgumentException("Version Info MUST NOT be null NOR empty.");
}
VERSION = version;
}
/**
* Reset the version header to its default value of {@link #PGPAINLESS}.
*/
public static void resetVersionInfo() {
VERSION = PGPAINLESS;
}
/**
* Set a comment header value in the ASCII armor header.
* If the comment contains newlines, it will be split into multiple header entries.
*
* @param comment comment
*/
public static void setComment(String comment) {
if (comment == null) {
throw new IllegalArgumentException("Comment cannot be null.");
}
String trimmed = comment.trim();
if (trimmed.isEmpty()) {
throw new IllegalArgumentException("Comment cannot be empty.");
}
String[] lines = comment.split("\n");
COMMENT = lines;
}
/**
* Reset to the default of no comment headers.
*/
public static void resetComment() {
COMMENT = new String[0];
}
}