summaryrefslogtreecommitdiff
path: root/crypto-common/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'crypto-common/src/main')
-rw-r--r--crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt13
-rw-r--r--crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/errors/CryptoException.kt13
2 files changed, 21 insertions, 5 deletions
diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt
index fc4c500d..e8e9f995 100644
--- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt
+++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt
@@ -5,6 +5,8 @@
package dev.msfjarvis.aps.crypto
+import com.github.michaelbull.result.Result
+import dev.msfjarvis.aps.crypto.errors.CryptoHandlerException
import java.io.InputStream
import java.io.OutputStream
@@ -13,24 +15,27 @@ public interface CryptoHandler<Key> {
/**
* Decrypt the given [ciphertextStream] using a [privateKey] and [passphrase], and writes the
- * resultant plaintext to [outputStream].
+ * resultant plaintext to [outputStream]. The returned [Result] should be checked to ensure it is
+ * **not** an instance of [com.github.michaelbull.result.Err] before the contents of
+ * [outputStream] are used.
*/
public fun decrypt(
privateKey: Key,
passphrase: String,
ciphertextStream: InputStream,
outputStream: OutputStream,
- )
+ ): Result<Unit, CryptoHandlerException>
/**
* Encrypt the given [plaintextStream] to the provided [keys], and writes the encrypted ciphertext
- * to [outputStream].
+ * to [outputStream]. The returned [Result] should be checked to ensure it is **not** an instance
+ * of [com.github.michaelbull.result.Err] before the contents of [outputStream] are used.
*/
public fun encrypt(
keys: List<Key>,
plaintextStream: InputStream,
outputStream: OutputStream,
- )
+ ): Result<Unit, CryptoHandlerException>
/** Given a [fileName], return whether this instance can handle it. */
public fun canHandle(fileName: String): Boolean
diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/errors/CryptoException.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/errors/CryptoException.kt
index 5e911c35..0fb75691 100644
--- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/errors/CryptoException.kt
+++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/errors/CryptoException.kt
@@ -2,7 +2,8 @@ package dev.msfjarvis.aps.crypto.errors
import dev.msfjarvis.aps.crypto.KeyManager
-public sealed class CryptoException(message: String? = null) : Exception(message)
+public sealed class CryptoException(message: String? = null, cause: Throwable? = null) :
+ Exception(message, cause)
/** Sealed exception types for [KeyManager]. */
public sealed class KeyManagerException(message: String? = null) : CryptoException(message)
@@ -28,3 +29,13 @@ public class KeyNotFoundException(keyId: String) :
/** Attempting to add another key for [keyId] without requesting a replace. */
public class KeyAlreadyExistsException(keyId: String) :
KeyManagerException("Pre-existing key was found for $keyId")
+
+/** Sealed exception types for [CryptoHandler]. */
+public sealed class CryptoHandlerException(message: String? = null, cause: Throwable? = null) :
+ CryptoException(message, cause)
+
+/** The passphrase provided for decryption was incorrect. */
+public class IncorrectPassphraseException(cause: Throwable) : CryptoHandlerException(null, cause)
+
+/** An unexpected error that cannot be mapped to a known type. */
+public class UnknownError(cause: Throwable) : CryptoHandlerException(null, cause)