Improve --[no-]armor flag implementation

This commit is contained in:
Paul Schaub 2020-12-22 22:05:47 +01:00
parent 7d6c0f4396
commit 1e3721c4e5
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 17 additions and 20 deletions

View File

@ -31,11 +31,10 @@ import static org.pgpainless.sop.Print.print_ln;
@CommandLine.Command(name = "extract-cert")
public class ExtractCert implements Runnable {
@CommandLine.Option(names = {"--armor"}, description = "ASCII Armor the output")
boolean armor = false;
@CommandLine.Option(names = {"--no-armor"})
boolean noArmor = false;
@CommandLine.Option(names = "--no-armor",
description = "ASCII armor the output",
negatable = true)
boolean armor = true;
@Override
public void run() {
@ -43,7 +42,7 @@ public class ExtractCert implements Runnable {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(System.in);
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
print_ln(Print.toString(publicKeys.getEncoded(), !noArmor));
print_ln(Print.toString(publicKeys.getEncoded(), armor));
} catch (IOException | PGPException e) {
err_ln("Error extracting certificate from keys;");
err_ln(e.getMessage());

View File

@ -31,11 +31,10 @@ import static org.pgpainless.sop.Print.print_ln;
@CommandLine.Command(name = "generate-key")
public class GenerateKey implements Runnable {
@CommandLine.Option(names = {"--armor"}, description = "ASCII Armor the output")
boolean armor = false;
@CommandLine.Option(names = {"--no-armor"}, description = "Non-armored, binary output")
boolean noArmor = false;
@CommandLine.Option(names = "--no-armor",
description = "ASCII armor the output",
negatable = true)
boolean armor = true;
@CommandLine.Parameters(description = "User-ID, eg. \"Alice <alice@example.com>\"")
String userId;
@ -45,7 +44,7 @@ public class GenerateKey implements Runnable {
try {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing(userId);
print_ln(Print.toString(secretKeys.getEncoded(), !noArmor));
print_ln(Print.toString(secretKeys.getEncoded(), armor));
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | PGPException | IOException e) {
err_ln("Error creating OpenPGP key:");

View File

@ -42,11 +42,10 @@ public class Sign implements Runnable {
text
}
@CommandLine.Option(names = {"--armor"}, description = "ASCII Armor the output")
boolean armor = false;
@CommandLine.Option(names = {"--no-armor"})
boolean noArmor = false;
@CommandLine.Option(names = "--no-armor",
description = "ASCII armor the output",
negatable = true)
boolean armor = true;
@CommandLine.Option(names = "--as", description = "Defaults to 'binary'. If '--as=text' and the input data is not valid UTF-8, sign fails with return code 53.",
paramLabel = "{binary|text}")
@ -74,15 +73,15 @@ public class Sign implements Runnable {
.createDetachedSignature()
.signWith(new UnprotectedKeysProtector(), secretKeys);
EncryptionBuilderInterface.Armor armor = type == Type.text ? documentType.signCanonicalText() : documentType.signBinaryDocument();
EncryptionStream encryptionStream = noArmor ? armor.noArmor() : armor.asciiArmor();
EncryptionBuilderInterface.Armor builder = type == Type.text ? documentType.signCanonicalText() : documentType.signBinaryDocument();
EncryptionStream encryptionStream = armor ? builder.asciiArmor() : builder.noArmor();
Streams.pipeAll(System.in, encryptionStream);
encryptionStream.close();
PGPSignature signature = encryptionStream.getResult().getSignatures().iterator().next();
print_ln(Print.toString(signature.getEncoded(), !noArmor));
print_ln(Print.toString(signature.getEncoded(), armor));
} catch (PGPException | IOException e) {
err_ln("Error signing data.");
err_ln(e.getMessage());