aboutsummaryrefslogtreecommitdiffstats
path: root/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-01-09 16:19:52 +0530
committerGitHub <noreply@github.com>2022-01-09 16:19:52 +0530
commit799f1393e49955d05f68b81af26d6cfaf9beadfd (patch)
treee1da29f5c1b3807016bea5f2f1d7046e92529e53 /crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
parentccb33af854132f1b35b71393ff68d24850de6960 (diff)
downloadHardenedPass-799f1393e49955d05f68b81af26d6cfaf9beadfd.tar.gz
HardenedPass-799f1393e49955d05f68b81af26d6cfaf9beadfd.zip
Make CryptoHandler use Key as the abstraction layer (#1651)
Diffstat (limited to 'crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt')
-rw-r--r--crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt45
1 files changed, 2 insertions, 43 deletions
diff --git a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
index f1c53721..2053ecd7 100644
--- a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
+++ b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
@@ -8,15 +8,13 @@ package dev.msfjarvis.aps.crypto
import androidx.annotation.VisibleForTesting
import com.github.michaelbull.result.Result
-import com.github.michaelbull.result.get
import com.github.michaelbull.result.runCatching
+import dev.msfjarvis.aps.crypto.KeyUtils.tryGetId
+import dev.msfjarvis.aps.crypto.KeyUtils.tryParseKeyring
import java.io.File
-import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
-import org.bouncycastle.openpgp.PGPKeyRing
-import org.pgpainless.PGPainless
import org.pgpainless.util.selection.userid.SelectUserId
public class PGPKeyManager
@@ -118,45 +116,6 @@ constructor(
return keyDir.exists() || keyDir.mkdirs()
}
- /**
- * Attempts to parse a [PGPKeyRing] from a given [key]. The key is first tried as a secret key and
- * then as a public one before the method gives up and returns null.
- */
- private fun tryParseKeyring(key: Key): PGPKeyRing? {
- val secKeyRing = runCatching { PGPainless.readKeyRing().secretKeyRing(key.contents) }.get()
- if (secKeyRing != null) {
- return secKeyRing
- }
- val pubKeyRing = runCatching { PGPainless.readKeyRing().publicKeyRing(key.contents) }.get()
- if (pubKeyRing != null) {
- return pubKeyRing
- }
- return null
- }
-
- /** Parses a [PGPKeyRing] from the given [key] and returns its hex-formatted key ID. */
- private fun tryGetId(key: Key): String? {
- val keyRing = tryParseKeyring(key) ?: return null
- return convertKeyIdToHex(keyRing.publicKey.keyID)
- }
-
- /** Convert a [Long] key ID to a formatted string. */
- private fun convertKeyIdToHex(keyId: Long): String {
- return "0x" + 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 companion object {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)