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:
parent
6c449b86af
commit
46af22cc50
2 changed files with 38 additions and 0 deletions
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.pgpainless.key;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
|
@ -31,6 +33,8 @@ import org.bouncycastle.util.encoders.Hex;
|
|||
*/
|
||||
public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4Fingerprint> {
|
||||
|
||||
public static final String SCHEME = "openpgp4fpr";
|
||||
|
||||
private static final Charset utf8 = Charset.forName("UTF-8");
|
||||
private final String fingerprint;
|
||||
|
||||
|
@ -138,6 +142,21 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
|
|||
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
|
||||
public int compareTo(@Nonnull OpenPgpV4Fingerprint openPgpV4Fingerprint) {
|
||||
return fingerprint.compareTo(openPgpV4Fingerprint.fingerprint);
|
||||
|
|
|
@ -19,6 +19,8 @@ import static junit.framework.TestCase.assertEquals;
|
|||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.junit.Test;
|
||||
|
@ -74,4 +76,21 @@ public class OpenPgpV4FingerprintTest {
|
|||
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue