Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 1956b66

Browse files
committed
crypto-pgpainless: perform hex conversion inside KeyId#toString
1 parent 1880c5a commit 1956b66

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

Diff for: crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifier.kt

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,30 @@
55

66
package dev.msfjarvis.aps.crypto
77

8+
import java.util.Locale
89
import java.util.regex.Pattern
910

1011
public sealed class GpgIdentifier {
1112
public data class KeyId(val id: Long) : GpgIdentifier() {
1213
override fun toString(): String {
13-
return java.lang.Long.toHexString(id)
14+
return convertKeyIdToHex(id)
15+
}
16+
17+
/** Convert a [Long] key ID to a formatted string. */
18+
private fun convertKeyIdToHex(keyId: Long): String {
19+
return convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit(keyId)
20+
}
21+
22+
/**
23+
* Converts [keyId] to an unsigned [Long] then uses [java.lang.Long.toHexString] to convert it
24+
* to a lowercase hex ID.
25+
*/
26+
private fun convertKeyIdToHex32bit(keyId: Long): String {
27+
var hexString = java.lang.Long.toHexString(keyId and 0xffffffffL).lowercase(Locale.ENGLISH)
28+
while (hexString.length < 8) {
29+
hexString = "0$hexString"
30+
}
31+
return hexString
1432
}
1533
}
1634
public data class UserId(val email: String) : GpgIdentifier() {

Diff for: crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyUtils.kt

+1-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package dev.msfjarvis.aps.crypto
88
import com.github.michaelbull.result.get
99
import com.github.michaelbull.result.runCatching
1010
import dev.msfjarvis.aps.crypto.GpgIdentifier.KeyId
11-
import java.util.Locale
1211
import org.bouncycastle.openpgp.PGPKeyRing
1312
import org.pgpainless.PGPainless
1413

@@ -33,23 +32,6 @@ public object KeyUtils {
3332
/** Parses a [PGPKeyRing] from the given [key] and calculates its long key ID */
3433
public fun tryGetId(key: PGPKey): KeyId? {
3534
val keyRing = tryParseKeyring(key) ?: return null
36-
return KeyId(convertKeyIdToHex(keyRing.publicKey.keyID).toLong(radix = 16))
37-
}
38-
39-
/** Convert a [Long] key ID to a formatted string. */
40-
private fun convertKeyIdToHex(keyId: Long): String {
41-
return convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit(keyId)
42-
}
43-
44-
/**
45-
* Converts [keyId] to an unsigned [Long] then uses [java.lang.Long.toHexString] to convert it to
46-
* a lowercase hex ID.
47-
*/
48-
private fun convertKeyIdToHex32bit(keyId: Long): String {
49-
var hexString = java.lang.Long.toHexString(keyId and 0xffffffffL).lowercase(Locale.ENGLISH)
50-
while (hexString.length < 8) {
51-
hexString = "0$hexString"
52-
}
53-
return hexString
35+
return KeyId(keyRing.publicKey.keyID)
5436
}
5537
}

0 commit comments

Comments
 (0)