From fc97215f8018bb58ab75ca07ad849c2eb2758e76 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 4 Sep 2023 14:30:50 +0200 Subject: [PATCH] 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. --- .../org/pgpainless/key/util/KeyIdUtil.java | 37 ------------------- .../src/main/kotlin/_kotlin/LongExtensions.kt | 25 +++++++++++++ .../org/pgpainless/key/util/KeyIdUtil.kt | 36 ++++++++++++++++++ 3 files changed, 61 insertions(+), 37 deletions(-) delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/util/KeyIdUtil.java create mode 100644 pgpainless-core/src/main/kotlin/_kotlin/LongExtensions.kt create mode 100644 pgpainless-core/src/main/kotlin/org/pgpainless/key/util/KeyIdUtil.kt diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyIdUtil.java b/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyIdUtil.java deleted file mode 100644 index 78e03f71..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyIdUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-FileCopyrightText: 2021 Paul Schaub -// -// 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); - } -} diff --git a/pgpainless-core/src/main/kotlin/_kotlin/LongExtensions.kt b/pgpainless-core/src/main/kotlin/_kotlin/LongExtensions.kt new file mode 100644 index 00000000..b2c64e2e --- /dev/null +++ b/pgpainless-core/src/main/kotlin/_kotlin/LongExtensions.kt @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package _kotlin + +/** + * Format this Long as a 16 digit uppercase hex number. + */ +fun Long.hexKeyId(): String { + return String.format("%016X", this).uppercase() +} + +/** + * Parse a 16 digit hex number into a Long. + */ +fun Long.Companion.fromHexKeyId(hexKeyId: String): Long { + require("^[0-9A-Fa-f]{16}$".toRegex().matches(hexKeyId)) { + "Provided long key-id does not match expected format. " + + "A long key-id consists of 16 hexadecimal characters." + } + // Calling toLong() only fails with a NumberFormatException. + // Therefore, we call toULong(16).toLong(), which seems to work. + return hexKeyId.toULong(16).toLong() +} \ No newline at end of file diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/util/KeyIdUtil.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/util/KeyIdUtil.kt new file mode 100644 index 00000000..b6ff1789 --- /dev/null +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/util/KeyIdUtil.kt @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.key.util + +import _kotlin.fromHexKeyId +import _kotlin.hexKeyId + +class KeyIdUtil { + + companion object { + + /** + * 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}. + */ + @JvmStatic + @Deprecated("Superseded by Long extension method.", + ReplaceWith("Long.fromHexKeyId(longKeyId)")) + fun fromLongKeyId(longKeyId: String) = Long.fromHexKeyId(longKeyId) + + /** + * Format a long key-ID as upper-case hex string. + * @param keyId keyId + * @return hex encoded key ID + */ + @JvmStatic + @Deprecated("Superseded by Long extension method.", + ReplaceWith("keyId.hexKeyId()")) + fun formatKeyId(keyId: Long) = keyId.hexKeyId() + } +} \ No newline at end of file