From bbbcc76d6562531502ffc4db7831ab34acddbacf Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 21 Feb 2022 20:12:19 +0530 Subject: Add failing test for multiple identities (#1741) --- .../kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt | 20 +++++++++++++++++++- .../main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt | 20 +------------------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'crypto-pgpainless/src/main/kotlin') diff --git a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt index 20337d3e..d336e86c 100644 --- a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt +++ b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt @@ -5,12 +5,30 @@ package dev.msfjarvis.aps.crypto +import java.util.Locale import java.util.regex.Pattern public sealed class GpgIdentifier { public data class KeyId(val id: Long) : GpgIdentifier() { override fun toString(): String { - return java.lang.Long.toHexString(id) + return convertKeyIdToHex(id) + } + + /** Convert a [Long] key ID to a formatted string. */ + private fun convertKeyIdToHex(keyId: Long): String { + return convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit(keyId) + } + + /** + * Converts [keyId] to an unsigned [Long] then uses [java.lang.Long.toHexString] to convert it + * to a lowercase hex ID. + */ + private fun convertKeyIdToHex32bit(keyId: Long): String { + var hexString = java.lang.Long.toHexString(keyId and 0xffffffffL).lowercase(Locale.ENGLISH) + while (hexString.length < 8) { + hexString = "0$hexString" + } + return hexString } } public data class UserId(val email: String) : GpgIdentifier() { diff --git a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt index 852ed628..1251463a 100644 --- a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt +++ b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt @@ -8,7 +8,6 @@ package dev.msfjarvis.aps.crypto import com.github.michaelbull.result.get import com.github.michaelbull.result.runCatching import dev.msfjarvis.aps.crypto.GpgIdentifier.KeyId -import java.util.Locale import org.bouncycastle.openpgp.PGPKeyRing import org.pgpainless.PGPainless @@ -33,23 +32,6 @@ public object KeyUtils { /** Parses a [PGPKeyRing] from the given [key] and calculates its long key ID */ public fun tryGetId(key: PGPKey): KeyId? { val keyRing = tryParseKeyring(key) ?: return null - return KeyId(convertKeyIdToHex(keyRing.publicKey.keyID).toLong(radix = 16)) - } - - /** Convert a [Long] key ID to a formatted string. */ - private fun convertKeyIdToHex(keyId: Long): String { - return convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit(keyId) - } - - /** - * Converts [keyId] to an unsigned [Long] then uses [java.lang.Long.toHexString] to convert it to - * a lowercase hex ID. - */ - private fun convertKeyIdToHex32bit(keyId: Long): String { - var hexString = java.lang.Long.toHexString(keyId and 0xffffffffL).lowercase(Locale.ENGLISH) - while (hexString.length < 8) { - hexString = "0$hexString" - } - return hexString + return KeyId(keyRing.publicKey.keyID) } } -- cgit v1.2.3