@@ -11,9 +11,11 @@ import app.passwordstore.crypto.PGPEncryptOptions
1111import app.passwordstore.crypto.PGPIdentifier
1212import app.passwordstore.crypto.PGPKeyManager
1313import app.passwordstore.crypto.PGPainlessCryptoHandler
14+ import app.passwordstore.crypto.errors.CryptoHandlerException
1415import app.passwordstore.injection.prefs.SettingsPreferences
1516import app.passwordstore.util.coroutines.DispatcherProvider
1617import app.passwordstore.util.settings.PreferenceKeys
18+ import com.github.michaelbull.result.Result
1719import com.github.michaelbull.result.filterValues
1820import com.github.michaelbull.result.map
1921import com.github.michaelbull.result.mapBoth
@@ -37,34 +39,48 @@ constructor(
3739 }
3840 }
3941
40- suspend fun isPasswordProtected (identifiers : List <PGPIdentifier >): Boolean {
41- val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
42- return pgpCryptoHandler.isPassphraseProtected(keys)
43- }
44-
4542 suspend fun decrypt (
4643 password : String ,
4744 identities : List <PGPIdentifier >,
4845 message : ByteArrayInputStream ,
4946 out : ByteArrayOutputStream ,
5047 ) =
5148 withContext(dispatcherProvider.io()) {
52- val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
53- val decryptionOptions = PGPDecryptOptions .Builder ().build()
54- pgpCryptoHandler.decrypt(keys, password, message, out , decryptionOptions).map { out }
49+ decryptPgp(password, identities, message, out ).map { out }
5550 }
5651
52+ suspend fun isPasswordProtected (identifiers : List <PGPIdentifier >): Boolean {
53+ val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
54+ return pgpCryptoHandler.isPassphraseProtected(keys)
55+ }
56+
5757 suspend fun encrypt (
5858 identities : List <PGPIdentifier >,
5959 content : ByteArrayInputStream ,
6060 out : ByteArrayOutputStream ,
61- ) =
62- withContext(dispatcherProvider.io()) {
63- val encryptionOptions =
64- PGPEncryptOptions .Builder ()
65- .withAsciiArmor(settings.getBoolean(PreferenceKeys .ASCII_ARMOR , false ))
66- .build()
67- val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
68- pgpCryptoHandler.encrypt(keys, content, out , encryptionOptions).map { out }
69- }
61+ ) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out ).map { out } }
62+
63+ private suspend fun decryptPgp (
64+ password : String ,
65+ identities : List <PGPIdentifier >,
66+ message : ByteArrayInputStream ,
67+ out : ByteArrayOutputStream ,
68+ ): Result <Unit , CryptoHandlerException > {
69+ val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
70+ val decryptionOptions = PGPDecryptOptions .Builder ().build()
71+ return pgpCryptoHandler.decrypt(keys, password, message, out , decryptionOptions)
72+ }
73+
74+ private suspend fun encryptPgp (
75+ identities : List <PGPIdentifier >,
76+ content : ByteArrayInputStream ,
77+ out : ByteArrayOutputStream ,
78+ ): Result <Unit , CryptoHandlerException > {
79+ val encryptionOptions =
80+ PGPEncryptOptions .Builder ()
81+ .withAsciiArmor(settings.getBoolean(PreferenceKeys .ASCII_ARMOR , false ))
82+ .build()
83+ val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
84+ return pgpCryptoHandler.encrypt(keys, content, out , encryptionOptions)
85+ }
7086}
0 commit comments