1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-12 23:31:08 +01:00

Kotlin conversion: KeyIdUtil

This PR also introduces LongExtensions.kt which provides extension methods to
parse Long from Hex KeyIDs and to format Longs as Hex KeyIDs.
This commit is contained in:
Paul Schaub 2023-09-04 14:30:50 +02:00
parent d075ed6637
commit 44c22f9044
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 61 additions and 37 deletions

View file

@ -1,37 +0,0 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.util;
import java.math.BigInteger;
import java.util.regex.Pattern;
public final class KeyIdUtil {
private KeyIdUtil() {
}
private static final Pattern LONG_KEY_ID = Pattern.compile("^[0-9A-Fa-f]{16}$");
/**
* Convert a long key-id into a key-id.
* A long key-id is a 16 digit hex string.
*
* @param longKeyId 16-digit hexadecimal string
* @return key-id converted to {@link Long}.
*/
public static long fromLongKeyId(String longKeyId) {
if (!LONG_KEY_ID.matcher(longKeyId).matches()) {
throw new IllegalArgumentException("Provided long key-id does not match expected format. " +
"A long key-id consists of 16 hexadecimal characters.");
}
return new BigInteger(longKeyId, 16).longValue();
}
public static String formatKeyId(long keyId) {
return String.format("%016X", keyId);
}
}