From 260ecaaea3fbef59edeec23006ed35e57898580b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 9 Jul 2018 16:40:02 +0200 Subject: [PATCH] Do not throw PGPException in OpenPgpV4Fingerprint --- .../DecryptionBuilder.java | 7 +------ .../decryption_verification/PainlessResult.java | 3 +-- .../pgpainless/key/OpenPgpV4Fingerprint.java | 17 ++++++++--------- .../org/pgpainless/pgpainless/util/BCUtil.java | 2 -- .../pgpainless/OpenPgpV4FingerprintTest.java | 17 ++++++++--------- 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/pgpainless/pgpainless/decryption_verification/DecryptionBuilder.java b/src/main/java/org/pgpainless/pgpainless/decryption_verification/DecryptionBuilder.java index 0c7a97bb..f9431f62 100644 --- a/src/main/java/org/pgpainless/pgpainless/decryption_verification/DecryptionBuilder.java +++ b/src/main/java/org/pgpainless/pgpainless/decryption_verification/DecryptionBuilder.java @@ -76,12 +76,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { Set publicKeyRings = new HashSet<>(); for (Iterator i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { PGPPublicKeyRing p = i.next(); - OpenPgpV4Fingerprint fingerprint; - try { - fingerprint = new OpenPgpV4Fingerprint(p); - } catch (PGPException e) { - throw new IllegalArgumentException(e); - } + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(p); if (trustedKeyIds.contains(fingerprint)) { publicKeyRings.add(p); } diff --git a/src/main/java/org/pgpainless/pgpainless/decryption_verification/PainlessResult.java b/src/main/java/org/pgpainless/pgpainless/decryption_verification/PainlessResult.java index 997550cd..f66ffbd7 100644 --- a/src/main/java/org/pgpainless/pgpainless/decryption_verification/PainlessResult.java +++ b/src/main/java/org/pgpainless/pgpainless/decryption_verification/PainlessResult.java @@ -19,7 +19,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm; @@ -94,7 +93,7 @@ public class PainlessResult { return !verifiedSignaturesFingerprints.isEmpty(); } - public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) throws PGPException { + public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) { for (PGPPublicKey key : publicKeys) { OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key); if (verifiedSignaturesFingerprints.contains(fingerprint)) { diff --git a/src/main/java/org/pgpainless/pgpainless/key/OpenPgpV4Fingerprint.java b/src/main/java/org/pgpainless/pgpainless/key/OpenPgpV4Fingerprint.java index 8e453821..4a87dfe3 100644 --- a/src/main/java/org/pgpainless/pgpainless/key/OpenPgpV4Fingerprint.java +++ b/src/main/java/org/pgpainless/pgpainless/key/OpenPgpV4Fingerprint.java @@ -20,7 +20,6 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Arrays; -import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKey; @@ -40,38 +39,38 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable * @param fingerprint hexadecimal representation of the fingerprint. */ - public OpenPgpV4Fingerprint(String fingerprint) throws PGPException { + public OpenPgpV4Fingerprint(String fingerprint) { if (fingerprint == null) { throw new NullPointerException("Fingerprint MUST NOT be null."); } String fp = fingerprint.trim().toUpperCase(); if (!isValid(fp)) { - throw new PGPException("Fingerprint " + fingerprint + + throw new IllegalArgumentException("Fingerprint " + fingerprint + " does not appear to be a valid OpenPGP v4 fingerprint."); } this.fingerprint = fp; } - public OpenPgpV4Fingerprint(byte[] bytes) throws PGPException { + public OpenPgpV4Fingerprint(byte[] bytes) { this(new String(bytes, Charset.forName("UTF-8"))); } - public OpenPgpV4Fingerprint(PGPPublicKey key) throws PGPException { + public OpenPgpV4Fingerprint(PGPPublicKey key) { this(Hex.encode(key.getFingerprint())); if (key.getVersion() != 4) { - throw new PGPException("Key is not a v4 OpenPgp key."); + throw new IllegalArgumentException("Key is not a v4 OpenPgp key."); } } - public OpenPgpV4Fingerprint(PGPSecretKey key) throws PGPException { + public OpenPgpV4Fingerprint(PGPSecretKey key) { this(key.getPublicKey()); } - public OpenPgpV4Fingerprint(PGPPublicKeyRing ring) throws PGPException { + public OpenPgpV4Fingerprint(PGPPublicKeyRing ring) { this(ring.getPublicKey()); } - public OpenPgpV4Fingerprint(PGPSecretKeyRing ring) throws PGPException { + public OpenPgpV4Fingerprint(PGPSecretKeyRing ring) { this(ring.getPublicKey()); } diff --git a/src/main/java/org/pgpainless/pgpainless/util/BCUtil.java b/src/main/java/org/pgpainless/pgpainless/util/BCUtil.java index 173ebb05..e32224bb 100644 --- a/src/main/java/org/pgpainless/pgpainless/util/BCUtil.java +++ b/src/main/java/org/pgpainless/pgpainless/util/BCUtil.java @@ -19,8 +19,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; diff --git a/src/test/java/org/pgpainless/pgpainless/OpenPgpV4FingerprintTest.java b/src/test/java/org/pgpainless/pgpainless/OpenPgpV4FingerprintTest.java index 110d4632..e7ba3dda 100644 --- a/src/test/java/org/pgpainless/pgpainless/OpenPgpV4FingerprintTest.java +++ b/src/test/java/org/pgpainless/pgpainless/OpenPgpV4FingerprintTest.java @@ -19,47 +19,46 @@ import static junit.framework.TestCase.assertEquals; import java.io.IOException; -import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; import org.junit.Test; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; public class OpenPgpV4FingerprintTest { - @Test(expected = PGPException.class) - public void fpTooShort() throws PGPException { + @Test(expected = IllegalArgumentException.class) + public void fpTooShort() { String fp = "484f57414c495645"; // Asking Mark new OpenPgpV4Fingerprint(fp); } - @Test(expected = PGPException.class) - public void invalidHexTest() throws PGPException { + @Test(expected = IllegalArgumentException.class) + public void invalidHexTest() { String fp = "UNFORTUNATELYTHISISNOVALIDHEXADECIMALDOH"; new OpenPgpV4Fingerprint(fp); } @Test - public void validFingerprintTest() throws PGPException { + public void validFingerprintTest() { String fp = "4A4F48414E4E53454E2049532041204E45524421"; OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp); assertEquals(fp, finger.toString()); } @Test - public void convertsToUpperCaseTest() throws PGPException { + public void convertsToUpperCaseTest() { String fp = "444f4e5420552048415645204120484f4242593f"; OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp); assertEquals("444F4E5420552048415645204120484F4242593F", finger.toString()); } @Test - public void equalsOtherFingerprintTest() throws PGPException { + public void equalsOtherFingerprintTest() { OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint("5448452043414b452049532041204c4945212121"); assertEquals(finger, new OpenPgpV4Fingerprint("5448452043414B452049532041204C4945212121")); } @Test - public void keyIdTest() throws IOException, PGPException { + public void keyIdTest() throws IOException { PGPPublicKey key = TestKeys.getJulietPublicKeyRing().getPublicKey(); long keyId = key.getKeyID();