aboutsummaryrefslogtreecommitdiff
path: root/crypto-pgpainless/src/main/kotlin
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-02-21 20:12:19 +0530
committerGitHub <noreply@github.com>2022-02-21 20:12:19 +0530
commitbbbcc76d6562531502ffc4db7831ab34acddbacf (patch)
tree93761e9857c9a675dc0feee3cd01a0409b4d0a5f /crypto-pgpainless/src/main/kotlin
parente343c66d8b15037c5b73a32dd71f5f6fee26a120 (diff)
Add failing test for multiple identities (#1741)
Diffstat (limited to 'crypto-pgpainless/src/main/kotlin')
-rw-r--r--crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt20
-rw-r--r--crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt20
2 files changed, 20 insertions, 20 deletions
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)
}
}