mirror of
https://github.com/android-password-store/Android-Password-Store
synced 2025-08-30 22:05:19 +00:00
refactor: refine CryptoRepository#{encrypt,decrypt}
APIs
This commit is contained in:
@@ -17,6 +17,7 @@ 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
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
@@ -43,7 +44,10 @@ constructor(
|
||||
identities: List<PGPIdentifier>,
|
||||
message: ByteArrayInputStream,
|
||||
out: ByteArrayOutputStream,
|
||||
) = withContext(dispatcherProvider.io()) { decryptPgp(password, identities, message, out) }
|
||||
) =
|
||||
withContext(dispatcherProvider.io()) {
|
||||
decryptPgp(password, identities, message, out).map { out }
|
||||
}
|
||||
|
||||
suspend fun isPasswordProtected(identifiers: List<PGPIdentifier>): Boolean {
|
||||
val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
|
||||
@@ -54,7 +58,7 @@ constructor(
|
||||
identities: List<PGPIdentifier>,
|
||||
content: ByteArrayInputStream,
|
||||
out: ByteArrayOutputStream,
|
||||
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out) }
|
||||
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out).map { out } }
|
||||
|
||||
private suspend fun decryptPgp(
|
||||
password: String,
|
||||
|
@@ -17,7 +17,6 @@ import androidx.lifecycle.lifecycleScope
|
||||
import app.passwordstore.Application.Companion.screenWasOff
|
||||
import app.passwordstore.R
|
||||
import app.passwordstore.crypto.PGPIdentifier
|
||||
import app.passwordstore.crypto.errors.CryptoHandlerException
|
||||
import app.passwordstore.data.crypto.PGPPassphraseCache
|
||||
import app.passwordstore.data.passfile.PasswordEntry
|
||||
import app.passwordstore.data.repo.PasswordRepository
|
||||
@@ -34,14 +33,11 @@ import app.passwordstore.util.features.Features
|
||||
import app.passwordstore.util.settings.PreferenceKeys
|
||||
import com.github.androidpasswordstore.autofillparser.AutofillAction
|
||||
import com.github.androidpasswordstore.autofillparser.Credentials
|
||||
import com.github.michaelbull.result.Result
|
||||
import com.github.michaelbull.result.getOrElse
|
||||
import com.github.michaelbull.result.map
|
||||
import com.github.michaelbull.result.onFailure
|
||||
import com.github.michaelbull.result.onSuccess
|
||||
import com.github.michaelbull.result.runCatching
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
@@ -213,7 +209,9 @@ class AutofillDecryptActivity : BasePGPActivity() {
|
||||
return null
|
||||
}
|
||||
.onSuccess { encryptedInput ->
|
||||
decryptPGPStream(encryptedInput, password, identifiers)
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
repository
|
||||
.decrypt(password, identifiers, encryptedInput, outputStream)
|
||||
.onFailure { e ->
|
||||
logcat(ERROR) { e.asLog("Decryption failed") }
|
||||
return null
|
||||
@@ -235,17 +233,6 @@ class AutofillDecryptActivity : BasePGPActivity() {
|
||||
return null
|
||||
}
|
||||
|
||||
private suspend fun decryptPGPStream(
|
||||
message: ByteArrayInputStream,
|
||||
passphrase: String,
|
||||
gpgIdentifiers: List<PGPIdentifier>,
|
||||
): Result<ByteArrayOutputStream, CryptoHandlerException> {
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map {
|
||||
outputStream
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val EXTRA_FILE_PATH = "app.passwordstore.autofill.oreo.EXTRA_FILE_PATH"
|
||||
|
@@ -15,7 +15,6 @@ import androidx.lifecycle.lifecycleScope
|
||||
import app.passwordstore.Application.Companion.screenWasOff
|
||||
import app.passwordstore.R
|
||||
import app.passwordstore.crypto.PGPIdentifier
|
||||
import app.passwordstore.crypto.errors.CryptoHandlerException
|
||||
import app.passwordstore.crypto.errors.NonStandardAEAD
|
||||
import app.passwordstore.data.crypto.PGPPassphraseCache
|
||||
import app.passwordstore.data.passfile.PasswordEntry
|
||||
@@ -32,8 +31,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache
|
||||
import app.passwordstore.util.features.Features
|
||||
import app.passwordstore.util.settings.Constants
|
||||
import app.passwordstore.util.settings.PreferenceKeys
|
||||
import com.github.michaelbull.result.Result
|
||||
import com.github.michaelbull.result.map
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
@@ -236,7 +233,9 @@ class DecryptActivity : BasePGPActivity() {
|
||||
authResult: BiometricResult,
|
||||
onSuccess: suspend () -> Unit = {},
|
||||
) {
|
||||
val result = decryptPGPStream(passphrase, identifiers)
|
||||
val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() }
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
val result = repository.decrypt(passphrase, identifiers, message, outputStream)
|
||||
if (result.isOk) {
|
||||
if (features.isEnabled(EnablePGPPassphraseCache))
|
||||
settings.edit { putBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, clearCache) }
|
||||
@@ -268,17 +267,6 @@ class DecryptActivity : BasePGPActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun decryptPGPStream(
|
||||
passphrase: String,
|
||||
gpgIdentifiers: List<PGPIdentifier>,
|
||||
): Result<ByteArrayOutputStream, CryptoHandlerException> {
|
||||
val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() }
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map {
|
||||
outputStream
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun createPasswordUI(entry: PasswordEntry) =
|
||||
withContext(dispatcherProvider.main()) {
|
||||
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
|
||||
|
Reference in New Issue
Block a user