1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 14:21:09 +01:00

Add OpenPgpFingerprint.parse(String)

This commit is contained in:
Paul Schaub 2022-03-10 12:22:02 +01:00
parent 8f473b513f
commit 6b9b956c2c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 39 additions and 0 deletions

View file

@ -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)) {