1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 04:42:06 +01:00

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

View file

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

View file

@ -42,11 +42,10 @@ public class Sign implements Runnable {
text text
} }
@CommandLine.Option(names = {"--armor"}, description = "ASCII Armor the output") @CommandLine.Option(names = "--no-armor",
boolean armor = false; description = "ASCII armor the output",
negatable = true)
@CommandLine.Option(names = {"--no-armor"}) boolean armor = true;
boolean noArmor = false;
@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.", @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}") paramLabel = "{binary|text}")
@ -74,15 +73,15 @@ public class Sign implements Runnable {
.createDetachedSignature() .createDetachedSignature()
.signWith(new UnprotectedKeysProtector(), secretKeys); .signWith(new UnprotectedKeysProtector(), secretKeys);
EncryptionBuilderInterface.Armor armor = type == Type.text ? documentType.signCanonicalText() : documentType.signBinaryDocument(); EncryptionBuilderInterface.Armor builder = type == Type.text ? documentType.signCanonicalText() : documentType.signBinaryDocument();
EncryptionStream encryptionStream = noArmor ? armor.noArmor() : armor.asciiArmor(); EncryptionStream encryptionStream = armor ? builder.asciiArmor() : builder.noArmor();
Streams.pipeAll(System.in, encryptionStream); Streams.pipeAll(System.in, encryptionStream);
encryptionStream.close(); encryptionStream.close();
PGPSignature signature = encryptionStream.getResult().getSignatures().iterator().next(); 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) { } catch (PGPException | IOException e) {
err_ln("Error signing data."); err_ln("Error signing data.");
err_ln(e.getMessage()); err_ln(e.getMessage());