mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 04:42:06 +01:00
Add OpenPgpFingerprint.parse(String)
This commit is contained in:
parent
8f473b513f
commit
6b9b956c2c
3 changed files with 39 additions and 0 deletions
|
@ -50,6 +50,23 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
|
|||
return of(ring.getPublicKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to parse an {@link OpenPgpFingerprint} from the given fingerprint string.
|
||||
*
|
||||
* @param fingerprint fingerprint
|
||||
* @return parsed fingerprint
|
||||
*/
|
||||
public static OpenPgpFingerprint parse(String fingerprint) {
|
||||
String fp = fingerprint.replace(" ", "").trim().toUpperCase();
|
||||
if (fp.matches("^[0-9A-F]{40}$")) {
|
||||
return new OpenPgpV4Fingerprint(fp);
|
||||
}
|
||||
if (fp.matches("^[0-9A-F]{64}$")) {
|
||||
return new OpenPgpV5Fingerprint(fp);
|
||||
}
|
||||
throw new IllegalArgumentException("Fingerprint does not appear to match any known fingerprint patterns.");
|
||||
}
|
||||
|
||||
public OpenPgpFingerprint(String fingerprint) {
|
||||
String fp = fingerprint.replace(" ", "").trim().toUpperCase();
|
||||
if (!isValid(fp)) {
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.pgpainless.key;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
@ -91,4 +92,14 @@ public class OpenPgpV4FingerprintTest {
|
|||
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(prettyPrint);
|
||||
assertEquals(prettyPrint, fingerprint.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
String prettyPrint = "C94B 884B 9A56 7B1C FB23 6999 7DC5 BDAC BBDF BF87";
|
||||
OpenPgpFingerprint parsed = OpenPgpFingerprint.parse(prettyPrint);
|
||||
|
||||
assertTrue(parsed instanceof OpenPgpV4Fingerprint);
|
||||
OpenPgpV4Fingerprint v4fp = (OpenPgpV4Fingerprint) parsed;
|
||||
assertEquals(prettyPrint, v4fp.prettyPrint());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.pgpainless.key;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class OpenPgpV5FingerprintTest {
|
||||
|
||||
|
@ -22,4 +23,14 @@ public class OpenPgpV5FingerprintTest {
|
|||
long id = fingerprint.getKeyId();
|
||||
assertEquals("76543210abcdefab", Long.toHexString(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
String prettyPrint = "76543210 ABCDEFAB 01AB23CD 1C0FFEE1 1EEFF0C1 DC32BA10 BAFEDCBA 01234567";
|
||||
OpenPgpFingerprint parsed = OpenPgpFingerprint.parse(prettyPrint);
|
||||
|
||||
assertTrue(parsed instanceof OpenPgpV5Fingerprint);
|
||||
OpenPgpV5Fingerprint v5fp = (OpenPgpV5Fingerprint) parsed;
|
||||
assertEquals(prettyPrint, v5fp.prettyPrint());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue