From cb22561878d4358dba974c16ba9a05cc693d411a Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 26 Sep 2024 17:49:57 +0530 Subject: [PATCH] Revert "refactor(app): inline pointless methods in `CryptoRepository`" This reverts commit b05a6d411b98393abe3bdbdd4f673bbe49e30f88. --- .../data/crypto/CryptoRepository.kt | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt index eb858006..cd54dac2 100644 --- a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt +++ b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt @@ -11,9 +11,11 @@ import app.passwordstore.crypto.PGPEncryptOptions import app.passwordstore.crypto.PGPIdentifier import app.passwordstore.crypto.PGPKeyManager import app.passwordstore.crypto.PGPainlessCryptoHandler +import app.passwordstore.crypto.errors.CryptoHandlerException import app.passwordstore.injection.prefs.SettingsPreferences import app.passwordstore.util.coroutines.DispatcherProvider import app.passwordstore.util.settings.PreferenceKeys +import com.github.michaelbull.result.Result import com.github.michaelbull.result.filterValues import com.github.michaelbull.result.map import com.github.michaelbull.result.mapBoth @@ -37,11 +39,6 @@ constructor( } } - suspend fun isPasswordProtected(identifiers: List): Boolean { - val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues() - return pgpCryptoHandler.isPassphraseProtected(keys) - } - suspend fun decrypt( password: String, identities: List, @@ -49,22 +46,41 @@ constructor( out: ByteArrayOutputStream, ) = withContext(dispatcherProvider.io()) { - val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() - val decryptionOptions = PGPDecryptOptions.Builder().build() - pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions).map { out } + decryptPgp(password, identities, message, out).map { out } } + suspend fun isPasswordProtected(identifiers: List): Boolean { + val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues() + return pgpCryptoHandler.isPassphraseProtected(keys) + } + suspend fun encrypt( identities: List, content: ByteArrayInputStream, out: ByteArrayOutputStream, - ) = - withContext(dispatcherProvider.io()) { - val encryptionOptions = - PGPEncryptOptions.Builder() - .withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false)) - .build() - val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() - pgpCryptoHandler.encrypt(keys, content, out, encryptionOptions).map { out } - } + ) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out).map { out } } + + private suspend fun decryptPgp( + password: String, + identities: List, + message: ByteArrayInputStream, + out: ByteArrayOutputStream, + ): Result { + val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() + val decryptionOptions = PGPDecryptOptions.Builder().build() + return pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions) + } + + private suspend fun encryptPgp( + identities: List, + content: ByteArrayInputStream, + out: ByteArrayOutputStream, + ): Result { + val encryptionOptions = + PGPEncryptOptions.Builder() + .withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false)) + .build() + val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() + return pgpCryptoHandler.encrypt(keys, content, out, encryptionOptions) + } }