From 66a9c884486d016dceabeee8b929dc31696bd23a Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 5 Jul 2023 02:36:05 +0530 Subject: feat(crypto-pgpainless): add `KeyUtils#isKeyUsable` --- .../src/main/kotlin/app/passwordstore/crypto/KeyUtils.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'crypto-pgpainless/src/main') 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 + } } -- cgit v1.2.3