Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 06fbe6c

Browse files
committed
refactor: refine CryptoRepository#{encrypt,decrypt} APIs
1 parent 1c6bbc5 commit 06fbe6c

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import app.passwordstore.util.coroutines.DispatcherProvider
1717
import app.passwordstore.util.settings.PreferenceKeys
1818
import com.github.michaelbull.result.Result
1919
import com.github.michaelbull.result.filterValues
20+
import com.github.michaelbull.result.map
2021
import com.github.michaelbull.result.mapBoth
2122
import java.io.ByteArrayInputStream
2223
import java.io.ByteArrayOutputStream
@@ -43,7 +44,10 @@ constructor(
4344
identities: List<PGPIdentifier>,
4445
message: ByteArrayInputStream,
4546
out: ByteArrayOutputStream,
46-
) = withContext(dispatcherProvider.io()) { decryptPgp(password, identities, message, out) }
47+
) =
48+
withContext(dispatcherProvider.io()) {
49+
decryptPgp(password, identities, message, out).map { out }
50+
}
4751

4852
suspend fun isPasswordProtected(identifiers: List<PGPIdentifier>): Boolean {
4953
val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
@@ -54,7 +58,7 @@ constructor(
5458
identities: List<PGPIdentifier>,
5559
content: ByteArrayInputStream,
5660
out: ByteArrayOutputStream,
57-
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out) }
61+
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out).map { out } }
5862

5963
private suspend fun decryptPgp(
6064
password: String,

app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import androidx.lifecycle.lifecycleScope
1717
import app.passwordstore.Application.Companion.screenWasOff
1818
import app.passwordstore.R
1919
import app.passwordstore.crypto.PGPIdentifier
20-
import app.passwordstore.crypto.errors.CryptoHandlerException
2120
import app.passwordstore.data.crypto.PGPPassphraseCache
2221
import app.passwordstore.data.passfile.PasswordEntry
2322
import app.passwordstore.data.repo.PasswordRepository
@@ -34,14 +33,11 @@ import app.passwordstore.util.features.Features
3433
import app.passwordstore.util.settings.PreferenceKeys
3534
import com.github.androidpasswordstore.autofillparser.AutofillAction
3635
import com.github.androidpasswordstore.autofillparser.Credentials
37-
import com.github.michaelbull.result.Result
3836
import com.github.michaelbull.result.getOrElse
39-
import com.github.michaelbull.result.map
4037
import com.github.michaelbull.result.onFailure
4138
import com.github.michaelbull.result.onSuccess
4239
import com.github.michaelbull.result.runCatching
4340
import dagger.hilt.android.AndroidEntryPoint
44-
import java.io.ByteArrayInputStream
4541
import java.io.ByteArrayOutputStream
4642
import java.io.File
4743
import javax.inject.Inject
@@ -213,7 +209,9 @@ class AutofillDecryptActivity : BasePGPActivity() {
213209
return null
214210
}
215211
.onSuccess { encryptedInput ->
216-
decryptPGPStream(encryptedInput, password, identifiers)
212+
val outputStream = ByteArrayOutputStream()
213+
repository
214+
.decrypt(password, identifiers, encryptedInput, outputStream)
217215
.onFailure { e ->
218216
logcat(ERROR) { e.asLog("Decryption failed") }
219217
return null
@@ -235,17 +233,6 @@ class AutofillDecryptActivity : BasePGPActivity() {
235233
return null
236234
}
237235

238-
private suspend fun decryptPGPStream(
239-
message: ByteArrayInputStream,
240-
passphrase: String,
241-
gpgIdentifiers: List<PGPIdentifier>,
242-
): Result<ByteArrayOutputStream, CryptoHandlerException> {
243-
val outputStream = ByteArrayOutputStream()
244-
return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map {
245-
outputStream
246-
}
247-
}
248-
249236
companion object {
250237

251238
private const val EXTRA_FILE_PATH = "app.passwordstore.autofill.oreo.EXTRA_FILE_PATH"

app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import androidx.lifecycle.lifecycleScope
1515
import app.passwordstore.Application.Companion.screenWasOff
1616
import app.passwordstore.R
1717
import app.passwordstore.crypto.PGPIdentifier
18-
import app.passwordstore.crypto.errors.CryptoHandlerException
1918
import app.passwordstore.crypto.errors.NonStandardAEAD
2019
import app.passwordstore.data.crypto.PGPPassphraseCache
2120
import app.passwordstore.data.passfile.PasswordEntry
@@ -32,8 +31,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache
3231
import app.passwordstore.util.features.Features
3332
import app.passwordstore.util.settings.Constants
3433
import app.passwordstore.util.settings.PreferenceKeys
35-
import com.github.michaelbull.result.Result
36-
import com.github.michaelbull.result.map
3734
import dagger.hilt.android.AndroidEntryPoint
3835
import java.io.ByteArrayOutputStream
3936
import java.io.File
@@ -236,7 +233,9 @@ class DecryptActivity : BasePGPActivity() {
236233
authResult: BiometricResult,
237234
onSuccess: suspend () -> Unit = {},
238235
) {
239-
val result = decryptPGPStream(passphrase, identifiers)
236+
val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() }
237+
val outputStream = ByteArrayOutputStream()
238+
val result = repository.decrypt(passphrase, identifiers, message, outputStream)
240239
if (result.isOk) {
241240
if (features.isEnabled(EnablePGPPassphraseCache))
242241
settings.edit { putBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, clearCache) }
@@ -268,17 +267,6 @@ class DecryptActivity : BasePGPActivity() {
268267
}
269268
}
270269

271-
private suspend fun decryptPGPStream(
272-
passphrase: String,
273-
gpgIdentifiers: List<PGPIdentifier>,
274-
): Result<ByteArrayOutputStream, CryptoHandlerException> {
275-
val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() }
276-
val outputStream = ByteArrayOutputStream()
277-
return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map {
278-
outputStream
279-
}
280-
}
281-
282270
private suspend fun createPasswordUI(entry: PasswordEntry) =
283271
withContext(dispatcherProvider.main()) {
284272
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)

0 commit comments

Comments
 (0)