mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-01-09 19:57:57 +01:00
Add and test OpenPgpFingerprint.parseFromBinary(bytes)
This commit is contained in:
parent
3a9bfd57ac
commit
9921fc6ff6
3 changed files with 90 additions and 0 deletions
|
@ -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.");
|
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) {
|
public OpenPgpFingerprint(String fingerprint) {
|
||||||
String fp = fingerprint.replace(" ", "").trim().toUpperCase();
|
String fp = fingerprint.replace(" ", "").trim().toUpperCase();
|
||||||
if (!isValid(fp)) {
|
if (!isValid(fp)) {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||||
|
import org.bouncycastle.util.encoders.Hex;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
@ -102,4 +103,42 @@ public class OpenPgpV4FingerprintTest {
|
||||||
OpenPgpV4Fingerprint v4fp = (OpenPgpV4Fingerprint) parsed;
|
OpenPgpV4Fingerprint v4fp = (OpenPgpV4Fingerprint) parsed;
|
||||||
assertEquals(prettyPrint, v4fp.prettyPrint());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,11 @@
|
||||||
|
|
||||||
package org.pgpainless.key;
|
package org.pgpainless.key;
|
||||||
|
|
||||||
|
import org.bouncycastle.util.encoders.Hex;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class OpenPgpV5FingerprintTest {
|
public class OpenPgpV5FingerprintTest {
|
||||||
|
@ -33,4 +35,42 @@ public class OpenPgpV5FingerprintTest {
|
||||||
OpenPgpV5Fingerprint v5fp = (OpenPgpV5Fingerprint) parsed;
|
OpenPgpV5Fingerprint v5fp = (OpenPgpV5Fingerprint) parsed;
|
||||||
assertEquals(prettyPrint, v5fp.prettyPrint());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue