summaryrefslogtreecommitdiff
path: root/crypto-pgpainless/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2023-07-05 02:36:05 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2023-07-05 02:36:05 +0530
commit66a9c884486d016dceabeee8b929dc31696bd23a (patch)
treeb4dc64ae236aa6dff5fb8d23e121ba4a5a8e9285 /crypto-pgpainless/src/main
parent496a1c4cb77743f724ccd3414115f92291345dbd (diff)
feat(crypto-pgpainless): add `KeyUtils#isKeyUsable`
Diffstat (limited to 'crypto-pgpainless/src/main')
-rw-r--r--crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt13
1 files changed, 13 insertions, 0 deletions
diff --git a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt
index 3c36060f..5b23dc18 100644
--- a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt
+++ b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt
@@ -10,6 +10,7 @@ import app.passwordstore.crypto.PGPIdentifier.UserId
import com.github.michaelbull.result.get
import com.github.michaelbull.result.runCatching
import org.bouncycastle.openpgp.PGPKeyRing
+import org.pgpainless.PGPainless
import org.pgpainless.key.parsing.KeyRingReader
/** Utility methods to deal with [PGPKey]s. */
@@ -36,4 +37,16 @@ public object KeyUtils {
val keyRing = tryParseKeyring(key) ?: return null
return UserId(keyRing.publicKey.userIDs.next())
}
+
+ /**
+ * Tests if the given [key] can be used for encryption, which is a bare minimum necessity for the
+ * app.
+ */
+ public fun isKeyUsable(key: PGPKey): Boolean {
+ return runCatching {
+ val keyRing = tryParseKeyring(key) ?: return false
+ PGPainless.inspectKeyRing(keyRing).isUsableForEncryption
+ }
+ .get() != null
+ }
}