@@ -69,21 +69,35 @@ class StorageNotifier extends AutoDisposeAsyncNotifier<Storage> {
69
69
}
70
70
}
71
71
72
- List <Totp > currentTotps = await currentStorage.listTotps ();
73
- Totp ? firstTotp = ( await newStorage.listTotps (limit : 1 )).firstOrNull ;
72
+ List <Totp > currentStorageTotps = await currentStorage.listTotps ();
73
+ List < Totp > newStorageTotps = await newStorage.listTotps () ;
74
74
List <Totp > toAdd = [];
75
- if (firstTotp == null ) {
76
- toAdd.addAll (currentTotps );
75
+ if (newStorageTotps.isEmpty ) {
76
+ toAdd.addAll (currentStorageTotps );
77
77
} else {
78
78
CryptoStore ? currentCryptoStore = ref.read (cryptoStoreProvider).value;
79
- CryptoStore newCryptoStore = await CryptoStore .fromPassword (masterPassword, firstTotp.encryptedData.encryptionSalt);
80
- for (Totp totp in currentTotps) {
81
- CryptoStore oldCryptoStore = currentCryptoStore? .salt == totp.encryptedData.encryptionSalt
82
- ? currentCryptoStore!
83
- : await CryptoStore .fromPassword (
84
- masterPassword,
85
- totp.encryptedData.encryptionSalt,
86
- );
79
+ CryptoStore ? newCryptoStore;
80
+ for (Totp totp in newStorageTotps) {
81
+ CryptoStore cryptoStore = await CryptoStore .fromPassword (masterPassword, totp.encryptedData.encryptionSalt);
82
+ if (await totp.encryptedData.canDecryptData (cryptoStore)) {
83
+ newCryptoStore = cryptoStore;
84
+ break ;
85
+ }
86
+ }
87
+ newCryptoStore ?? = await CryptoStore .fromPassword (masterPassword, newStorageTotps.first.encryptedData.encryptionSalt);
88
+
89
+ for (Totp totp in currentStorageTotps) {
90
+ CryptoStore oldCryptoStore;
91
+ if (currentCryptoStore != null && await totp.encryptedData.canDecryptData (currentCryptoStore)) {
92
+ oldCryptoStore = currentCryptoStore;
93
+ } else if (await totp.encryptedData.canDecryptData (newCryptoStore)) {
94
+ oldCryptoStore = newCryptoStore;
95
+ } else {
96
+ oldCryptoStore = await CryptoStore .fromPassword (
97
+ masterPassword,
98
+ totp.encryptedData.encryptionSalt,
99
+ );
100
+ }
87
101
DecryptedTotp ? decryptedTotp = await totp.changeEncryptionKey (oldCryptoStore, newCryptoStore);
88
102
toAdd.add (decryptedTotp ?? totp);
89
103
}
0 commit comments