1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-18 02:12:06 +01:00

Add and test OpenPgpFingerprint.parseFromBinary(bytes)

This commit is contained in:
Paul Schaub 2022-05-18 14:19:08 +02:00
parent 3a9bfd57ac
commit 9921fc6ff6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 90 additions and 0 deletions

View file

@ -67,6 +67,17 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
throw new IllegalArgumentException("Fingerprint does not appear to match any known fingerprint patterns.");
}
/**
* Parse a binary OpenPGP fingerprint into an {@link OpenPgpFingerprint} object.
*
* @param binaryFingerprint
* @return
*/
public static OpenPgpFingerprint parseFromBinary(byte[] binaryFingerprint) {
String hex = Hex.toHexString(binaryFingerprint).toUpperCase();
return parse(hex);
}
public OpenPgpFingerprint(String fingerprint) {
String fp = fingerprint.replace(" ", "").trim().toUpperCase();
if (!isValid(fp)) {

View file

@ -14,6 +14,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
@ -102,4 +103,42 @@ public class OpenPgpV4FingerprintTest {
OpenPgpV4Fingerprint v4fp = (OpenPgpV4Fingerprint) parsed;
assertEquals(prettyPrint, v4fp.prettyPrint());
}
@Test
public void testParseFromBinary() {
String hex = "5448452043414B452049532041204C4945212121";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV4Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_leadingZeros() {
String hex = "00000000000000000049532041204C4945212121";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV4Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_trailingZeros() {
String hex = "49532041204C4945212121000000000000000000";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV4Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_wrongLength() {
String hex = "5448452043414B452049532041204C49452121"; // 2 missing digits
byte[] binary = Hex.decode(hex);
assertThrows(IllegalArgumentException.class, () -> OpenPgpFingerprint.parseFromBinary(binary));
}
}

View file

@ -4,9 +4,11 @@
package org.pgpainless.key;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class OpenPgpV5FingerprintTest {
@ -33,4 +35,42 @@ public class OpenPgpV5FingerprintTest {
OpenPgpV5Fingerprint v5fp = (OpenPgpV5Fingerprint) parsed;
assertEquals(prettyPrint, v5fp.prettyPrint());
}
@Test
public void testParseFromBinary() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV5Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_leadingZeros() {
String hex = "000000000000000001AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV5Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_trailingZeros() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA100000000000000000";
byte[] binary = Hex.decode(hex);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary);
assertTrue(fingerprint instanceof OpenPgpV5Fingerprint);
assertEquals(hex, fingerprint.toString());
}
@Test
public void testParseFromBinary_wrongLength() {
String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA012345"; // missing 2 digits
byte[] binary = Hex.decode(hex);
assertThrows(IllegalArgumentException.class, () -> OpenPgpFingerprint.parseFromBinary(binary));
}
}