1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-22 20:32:05 +01:00

Add toUri/fromUri methods to OpenPgpV4Fingerprint

This commit is contained in:
Paul Schaub 2020-07-10 18:17:29 +02:00
parent 6c449b86af
commit 46af22cc50
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 38 additions and 0 deletions

View file

@ -15,6 +15,8 @@
*/ */
package org.pgpainless.key; package org.pgpainless.key;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.Buffer; import java.nio.Buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -31,6 +33,8 @@ import org.bouncycastle.util.encoders.Hex;
*/ */
public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4Fingerprint> { public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4Fingerprint> {
public static final String SCHEME = "openpgp4fpr";
private static final Charset utf8 = Charset.forName("UTF-8"); private static final Charset utf8 = Charset.forName("UTF-8");
private final String fingerprint; private final String fingerprint;
@ -138,6 +142,21 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
return fingerprint; return fingerprint;
} }
public URI toUri() {
try {
return new URI(SCHEME, toString(), null);
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
}
public static OpenPgpV4Fingerprint fromUri(URI uri) {
if (!SCHEME.equals(uri.getScheme())) {
throw new IllegalArgumentException("URI scheme MUST equal '" + SCHEME + "'");
}
return new OpenPgpV4Fingerprint(uri.getSchemeSpecificPart());
}
@Override @Override
public int compareTo(@Nonnull OpenPgpV4Fingerprint openPgpV4Fingerprint) { public int compareTo(@Nonnull OpenPgpV4Fingerprint openPgpV4Fingerprint) {
return fingerprint.compareTo(openPgpV4Fingerprint.fingerprint); return fingerprint.compareTo(openPgpV4Fingerprint.fingerprint);

View file

@ -19,6 +19,8 @@ import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.junit.Test; import org.junit.Test;
@ -74,4 +76,21 @@ public class OpenPgpV4FingerprintTest {
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key);
assertEquals(keyId, fingerprint.getKeyId()); assertEquals(keyId, fingerprint.getKeyId());
} }
@Test
public void testToUri() {
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint("5448452043414B452049532041204C4945212121");
URI uri = fingerprint.toUri();
assertEquals("openpgp4fpr:5448452043414B452049532041204C4945212121", uri.toString());
OpenPgpV4Fingerprint parsed = OpenPgpV4Fingerprint.fromUri(uri);
assertEquals(fingerprint, parsed);
}
@Test(expected = IllegalArgumentException.class)
public void testFromUriThrowsIfWrongScheme() throws URISyntaxException {
URI uri = new URI(null, "5448452043414B452049532041204C4945212121", null);
OpenPgpV4Fingerprint.fromUri(uri);
}
} }