summaryrefslogtreecommitdiff
path: root/crypto-pgpainless/src
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-10-29 04:36:00 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2022-10-29 04:36:00 +0530
commit633cbe271460980ff18a350fb6b7864a424ec83a (patch)
treebe3560a97823f02316ad8d220c0b53ce893a99c9 /crypto-pgpainless/src
parent390286f95fffdfad434f60a460fff21473fefc32 (diff)
feat(crypto-common): support passing arbitrary crypto options
Diffstat (limited to 'crypto-pgpainless/src')
-rw-r--r--crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPDecryptOptions.kt21
-rw-r--r--crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPEncryptOptions.kt35
-rw-r--r--crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandler.kt5
-rw-r--r--crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt4
4 files changed, 64 insertions, 1 deletions
diff --git a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPDecryptOptions.kt b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPDecryptOptions.kt
new file mode 100644
index 00000000..15ce92f0
--- /dev/null
+++ b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPDecryptOptions.kt
@@ -0,0 +1,21 @@
+package app.passwordstore.crypto
+
+/** [CryptoOptions] implementation for PGPainless decrypt operations. */
+public class PGPDecryptOptions
+private constructor(
+ private val values: Map<String, Boolean>,
+) : CryptoOptions {
+
+ override fun isOptionEnabled(option: String): Boolean {
+ return values.getOrDefault(option, false)
+ }
+
+ /** Implementation of a builder pattern for [PGPDecryptOptions]. */
+ public class Builder {
+
+ /** Build the final [PGPDecryptOptions] object. */
+ public fun build(): PGPDecryptOptions {
+ return PGPDecryptOptions(emptyMap())
+ }
+ }
+}
diff --git a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPEncryptOptions.kt b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPEncryptOptions.kt
new file mode 100644
index 00000000..90de6b51
--- /dev/null
+++ b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPEncryptOptions.kt
@@ -0,0 +1,35 @@
+package app.passwordstore.crypto
+
+/** [CryptoOptions] implementation for PGPainless encrypt operations. */
+public class PGPEncryptOptions
+private constructor(
+ private val values: Map<String, Boolean>,
+) : CryptoOptions {
+
+ internal companion object {
+ const val ASCII_ARMOR = "ASCII_ARMOR"
+ }
+
+ override fun isOptionEnabled(option: String): Boolean {
+ return values.getOrDefault(option, false)
+ }
+
+ /** Implementation of a builder pattern for [PGPEncryptOptions]. */
+ public class Builder {
+ private val optionsMap = mutableMapOf<String, Boolean>()
+
+ /**
+ * Toggle whether the encryption operation output will be ASCII armored or in OpenPGP's binary
+ * format.
+ */
+ public fun withAsciiArmor(enabled: Boolean): Builder {
+ optionsMap[ASCII_ARMOR] = enabled
+ return this
+ }
+
+ /** Build the final [PGPEncryptOptions] object. */
+ public fun build(): PGPEncryptOptions {
+ return PGPEncryptOptions(optionsMap)
+ }
+ }
+}
diff --git a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandler.kt b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandler.kt
index faa94dff..91c17004 100644
--- a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandler.kt
+++ b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandler.kt
@@ -27,13 +27,15 @@ import org.pgpainless.exception.WrongPassphraseException
import org.pgpainless.key.protection.SecretKeyRingProtector
import org.pgpainless.util.Passphrase
-public class PGPainlessCryptoHandler @Inject constructor() : CryptoHandler<PGPKey> {
+public class PGPainlessCryptoHandler @Inject constructor() :
+ CryptoHandler<PGPKey, PGPEncryptOptions, PGPDecryptOptions> {
public override fun decrypt(
keys: List<PGPKey>,
passphrase: String,
ciphertextStream: InputStream,
outputStream: OutputStream,
+ options: PGPDecryptOptions,
): Result<Unit, CryptoHandlerException> =
runCatching {
if (keys.isEmpty()) throw NoKeysProvided("No keys provided for encryption")
@@ -63,6 +65,7 @@ public class PGPainlessCryptoHandler @Inject constructor() : CryptoHandler<PGPKe
keys: List<PGPKey>,
plaintextStream: InputStream,
outputStream: OutputStream,
+ options: PGPEncryptOptions,
): Result<Unit, CryptoHandlerException> =
runCatching {
if (keys.isEmpty()) throw NoKeysProvided("No keys provided for encryption")
diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt
index 80c8dc7c..386ca72b 100644
--- a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt
+++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt
@@ -41,6 +41,7 @@ class PGPainlessCryptoHandlerTest {
encryptionKey.keySet,
CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
ciphertextStream,
+ PGPEncryptOptions.Builder().build(),
)
assertIs<Ok<Unit>>(encryptRes)
val plaintextStream = ByteArrayOutputStream()
@@ -50,6 +51,7 @@ class PGPainlessCryptoHandlerTest {
CryptoConstants.KEY_PASSPHRASE,
ciphertextStream.toByteArray().inputStream(),
plaintextStream,
+ PGPDecryptOptions.Builder().build(),
)
assertIs<Ok<Unit>>(decryptRes)
assertEquals(CryptoConstants.PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8))
@@ -63,6 +65,7 @@ class PGPainlessCryptoHandlerTest {
encryptionKey.keySet,
CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
ciphertextStream,
+ PGPEncryptOptions.Builder().build(),
)
assertIs<Ok<Unit>>(encryptRes)
val plaintextStream = ByteArrayOutputStream()
@@ -72,6 +75,7 @@ class PGPainlessCryptoHandlerTest {
"very incorrect passphrase",
ciphertextStream.toByteArray().inputStream(),
plaintextStream,
+ PGPDecryptOptions.Builder().build(),
)
assertIs<Err<Throwable>>(result)
assertIs<IncorrectPassphraseException>(result.getError())