diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/Print.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/Print.java index 0fcb5220..97113421 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/Print.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/Print.java @@ -17,10 +17,29 @@ package org.pgpainless.sop; import java.io.IOException; +import org.bouncycastle.openpgp.PGPKeyRing; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.pgpainless.util.ArmorUtils; public class Print { + public static String toString(PGPSecretKeyRing keyRing, boolean armor) throws IOException { + if (armor) { + return ArmorUtils.toAsciiArmoredString(keyRing); + } else { + return new String(keyRing.getEncoded(), "UTF-8"); + } + } + + public static String toString(PGPPublicKeyRing keyRing, boolean armor) throws IOException { + if (armor) { + return ArmorUtils.toAsciiArmoredString(keyRing); + } else { + return new String(keyRing.getEncoded(), "UTF-8"); + } + } + public static String toString(byte[] bytes, boolean armor) throws IOException { if (armor) { return ArmorUtils.toAsciiArmoredString(bytes); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java index 955c8e21..65efafae 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java @@ -51,7 +51,8 @@ public class Armor implements Runnable { @Override public void run() { - try (PushbackInputStream pbIn = new PushbackInputStream(System.in); ArmoredOutputStream armoredOutputStream = ArmoredOutputStreamFactory.get(System.out)) { + try (PushbackInputStream pbIn = new PushbackInputStream(System.in); + ArmoredOutputStream armoredOutputStream = ArmoredOutputStreamFactory.get(System.out)) { byte[] start = new byte[14]; int read = pbIn.read(start); pbIn.unread(read); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Encrypt.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Encrypt.java index 979cc093..71b13465 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Encrypt.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Encrypt.java @@ -105,9 +105,7 @@ public class Encrypt implements Runnable { for (int i = 0; i < signWith.length; i++) { try (FileInputStream fileIn = new FileInputStream(signWith[i])) { PGPSecretKeyRing secretKey = PGPainless.readKeyRing().secretKeyRing(fileIn); - signOpt.addInlineSignature(protector, secretKey, - type == Type.text || type == Type.mime ? - DocumentSignatureType.CANONICAL_TEXT_DOCUMENT : DocumentSignatureType.BINARY_DOCUMENT); + signOpt.addInlineSignature(protector, secretKey, parseType(type)); } catch (IOException | PGPException e) { err_ln("Cannot read secret key from file " + signWith[i].getName()); err_ln(e.getMessage()); @@ -131,4 +129,8 @@ public class Encrypt implements Runnable { System.exit(1); } } + + private static DocumentSignatureType parseType(Type type) { + return type == Type.binary ? DocumentSignatureType.BINARY_DOCUMENT : DocumentSignatureType.CANONICAL_TEXT_DOCUMENT; + } } diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/ExtractCert.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/ExtractCert.java index a2f46757..52480575 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/ExtractCert.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/ExtractCert.java @@ -44,7 +44,7 @@ public class ExtractCert implements Runnable { PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(System.in); PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); - print_ln(Print.toString(publicKeys.getEncoded(), armor)); + print_ln(Print.toString(publicKeys, armor)); } catch (IOException | PGPException e) { err_ln("Error extracting certificate from keys;"); err_ln(e.getMessage()); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java index 3cb40532..0d37d14c 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java @@ -84,7 +84,7 @@ public class GenerateKey implements Runnable { .withoutPassphrase() .build(); - print_ln(Print.toString(secretKeys.getEncoded(), armor)); + print_ln(Print.toString(secretKeys, armor)); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | PGPException | IOException e) { err_ln("Error creating OpenPGP key:");