@@ -115,6 +115,7 @@ class SetupEncryptionDialogFragment :
115115
116116 // Setup layout
117117 viewThemeUtils.material.colorTextInputLayout(binding.encryptionPasswordInputContainer)
118+ viewThemeUtils.material.colorProgressBar(binding.progressBar)
118119
119120 val builder = buildMaterialAlertDialog(binding.root)
120121 viewThemeUtils.dialog.colorMaterialAlertDialogBackground(requireContext(), builder)
@@ -158,70 +159,79 @@ class SetupEncryptionDialogFragment :
158159 private fun decryptPrivateKey (dialog : DialogInterface ) {
159160 Log_OC .d(TAG , " Decrypt private key" )
160161 binding.encryptionStatus.setText(R .string.end_to_end_encryption_decrypting)
162+ binding.progressBar.visibility = View .VISIBLE
161163
162- try {
163- if (downloadKeyResult !is DownloadKeyResult .Success ) {
164- Log_OC .d(TAG , " DownloadKeyResult is not success" )
165- return
166- }
164+ lifecycleScope.launch(Dispatchers .IO ) {
165+ try {
166+ if (downloadKeyResult !is DownloadKeyResult .Success ) {
167+ Log_OC .d(TAG , " DownloadKeyResult is not success" )
168+ return @launch
169+ }
167170
168- val privateKey = (downloadKeyResult as DownloadKeyResult .Success ).privateKey
169- if (privateKey.isNullOrEmpty()) {
170- Log_OC .e(TAG , " privateKey is null or empty" )
171- return
172- }
173- val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
174- val mnemonic =
175- binding.encryptionPasswordInput.text.toString().replace(" \\ s" .toRegex(), " " )
176- .lowercase()
177- val decryptedPrivateKey = CryptoHelper .decryptPrivateKey(
178- privateKey,
179- mnemonic
180- )
181- val accountName = user?.accountName ? : return
171+ val privateKey = (downloadKeyResult as DownloadKeyResult .Success ).privateKey
172+ if (privateKey.isNullOrEmpty()) {
173+ Log_OC .e(TAG , " privateKey is null or empty" )
174+ return @launch
175+ }
176+ val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
177+ val mnemonic =
178+ binding.encryptionPasswordInput.text.toString().replace(" \\ s" .toRegex(), " " )
179+ .lowercase()
180+ val decryptedPrivateKey = CryptoHelper .decryptPrivateKey(
181+ privateKey,
182+ mnemonic
183+ )
184+ val accountName = user?.accountName ? : return @launch
182185
183- arbitraryDataProvider?.storeOrUpdateKeyValue(
184- accountName,
185- EncryptionUtils .PRIVATE_KEY ,
186- decryptedPrivateKey
187- )
188- dialog.dismiss()
186+ arbitraryDataProvider?.storeOrUpdateKeyValue(
187+ accountName,
188+ EncryptionUtils .PRIVATE_KEY ,
189+ decryptedPrivateKey
190+ )
189191
190- Log_OC .d(TAG , " Private key successfully decrypted and stored" )
192+ Log_OC .d(TAG , " Private key successfully decrypted and stored" )
191193
192- arbitraryDataProvider?.storeOrUpdateKeyValue(
193- accountName,
194- EncryptionUtils .MNEMONIC ,
195- mnemonicUnchanged
196- )
194+ arbitraryDataProvider?.storeOrUpdateKeyValue(
195+ accountName,
196+ EncryptionUtils .MNEMONIC ,
197+ mnemonicUnchanged
198+ )
197199
198- // check if private key and public key match
199- val publicKey = arbitraryDataProvider?.getValue(
200- accountName,
201- EncryptionUtils .PUBLIC_KEY
202- )
200+ // check if private key and public key match
201+ val publicKey = arbitraryDataProvider?.getValue(
202+ accountName,
203+ EncryptionUtils .PUBLIC_KEY
204+ )
203205
204- val firstKey = EncryptionUtils .generateKey()
205- val base64encodedKey = EncryptionUtils .encodeBytesToBase64String(firstKey)
206- val encryptedString = EncryptionUtils .encryptStringAsymmetric(
207- base64encodedKey,
208- publicKey
209- )
210- val decryptedString = EncryptionUtils .decryptStringAsymmetric(
211- encryptedString,
212- decryptedPrivateKey
213- )
214- val secondKey = EncryptionUtils .decodeStringToBase64Bytes(decryptedString)
206+ val firstKey = EncryptionUtils .generateKey()
207+ val base64encodedKey = EncryptionUtils .encodeBytesToBase64String(firstKey)
208+ val encryptedString = EncryptionUtils .encryptStringAsymmetric(
209+ base64encodedKey,
210+ publicKey
211+ )
212+ val decryptedString = EncryptionUtils .decryptStringAsymmetric(
213+ encryptedString,
214+ decryptedPrivateKey
215+ )
216+ val secondKey = EncryptionUtils .decodeStringToBase64Bytes(decryptedString)
215217
216- if (! firstKey.contentEquals(secondKey)) {
217- EncryptionUtils .reportE2eError(arbitraryDataProvider, user)
218- throw Exception (" Keys do not match" )
219- }
218+ if (! firstKey.contentEquals(secondKey)) {
219+ EncryptionUtils .reportE2eError(arbitraryDataProvider, user)
220+ throw Exception (" Keys do not match" )
221+ }
220222
221- notifyResult()
222- } catch (e: Exception ) {
223- binding.encryptionStatus.setText(R .string.end_to_end_encryption_wrong_password)
224- Log_OC .e(TAG , " Error while decrypting private key: " + e.message)
223+ withContext(Dispatchers .Main ) {
224+ dialog.dismiss()
225+ notifyResult()
226+ }
227+ } catch (e: Exception ) {
228+ Log_OC .e(TAG , " Error while decrypting private key: " + e.message)
229+
230+ withContext(Dispatchers .Main ) {
231+ binding.encryptionStatus.setText(R .string.end_to_end_encryption_wrong_password)
232+ binding.progressBar.visibility = View .GONE
233+ }
234+ }
225235 }
226236 }
227237
0 commit comments