@@ -10,6 +10,7 @@ import 'package:open_authenticator/app.dart';
1010import 'package:open_authenticator/i18n/localizable_exception.dart' ;
1111import 'package:open_authenticator/i18n/translations.g.dart' ;
1212import 'package:open_authenticator/model/app_unlock/methods/method.dart' ;
13+ import 'package:open_authenticator/model/crypto/salt.dart' ;
1314import 'package:open_authenticator/model/password_verification/methods/password_signature.dart' ;
1415import 'package:open_authenticator/model/settings/app_unlock_method.dart' ;
1516import 'package:open_authenticator/utils/utils.dart' ;
@@ -25,7 +26,7 @@ class StoredCryptoStore extends AsyncNotifier<CryptoStore?> {
2526
2627 @override
2728 FutureOr <CryptoStore ?> build () async {
28- Salt ? salt = await Salt . readFromLocalStorage ( );
29+ Salt ? salt = await ref. watch (saltProvider.future );
2930 if (salt == null ) {
3031 return null ;
3132 }
@@ -45,7 +46,7 @@ class StoredCryptoStore extends AsyncNotifier<CryptoStore?> {
4546 Future <void > deleteFromLocalStorage ({bool deleteSalt = false }) async {
4647 await SimpleSecureStorage .delete (_kPasswordDerivedKeyKey);
4748 if (deleteSalt) {
48- await Salt .deleteFromLocalStorage ();
49+ await ref. read (saltProvider.notifier) .deleteFromLocalStorage ();
4950 }
5051 }
5152
@@ -57,7 +58,11 @@ class StoredCryptoStore extends AsyncNotifier<CryptoStore?> {
5758 }
5859
5960 /// Changes the current crypto store password, preserving the current salt if possible.
60- Future <CryptoStore > changeCryptoStore (String newPassword, {CryptoStore ? newCryptoStore, bool checkSettings = true }) async {
61+ Future <CryptoStore > changeCryptoStore (
62+ String newPassword, {
63+ CryptoStore ? newCryptoStore,
64+ bool checkSettings = true ,
65+ }) async {
6166 Salt ? salt = newCryptoStore? .salt;
6267 if (salt == null ) {
6368 CryptoStore ? currentCryptoStore = await future;
@@ -71,7 +76,7 @@ class StoredCryptoStore extends AsyncNotifier<CryptoStore?> {
7176 }
7277 }
7378 Future <void > saveCryptoStoreOnLocalStorage () async => await SimpleSecureStorage .write (_kPasswordDerivedKeyKey, base64.encode (newCryptoStore! .key));
74- await salt. saveToLocalStorage ( );
79+ await ref. read (saltProvider.notifier). changeSalt (salt );
7580 if (checkSettings) {
7681 String unlockMethod = await ref.read (appUnlockMethodSettingsEntryProvider.future);
7782 if (unlockMethod == MasterPasswordAppUnlockMethod .kMethodId) {
@@ -89,9 +94,6 @@ class StoredCryptoStore extends AsyncNotifier<CryptoStore?> {
8994
9095/// Allows to encrypt some data according to a key.
9196class CryptoStore {
92- /// The key length.
93- static const int _keyLength = 256 ~ / 8 ;
94-
9597 /// The initialization vector length.
9698 static const int _initializationVectorLength = 96 ~ / 8 ;
9799
@@ -109,10 +111,11 @@ class CryptoStore {
109111 });
110112
111113 /// Creates a [CryptoStoreWithPasswordSignature] from the given [password] .
112- CryptoStore .fromPassword (String password, Salt salt) : this ._(
113- key: _deriveKey (password, salt).bytes,
114- salt: salt,
115- );
114+ CryptoStore .fromPassword (String password, Salt salt)
115+ : this ._(
116+ key: _deriveKey (password, salt).bytes,
117+ salt: salt,
118+ );
116119
117120 /// Generates a derived key from the given [password] and save it to the device secure storage.
118121 /// Also returns the salt that has been used.
@@ -164,48 +167,6 @@ class CryptoStore {
164167 MACHashBase get hmacSecretKey => sha256.hmac.by (key);
165168}
166169
167- /// Represents a decoded salt.
168- class Salt {
169- /// The salt length.
170- static const int _saltLength = CryptoStore ._keyLength;
171-
172- /// The password derived key storage key.
173- static const String _kPasswordDerivedKeySaltKey = 'passwordDerivedKeySalt' ;
174-
175- /// The salt value.
176- final Uint8List value;
177-
178- /// Creates a new salt instance.
179- const Salt .fromRawValue ({
180- required this .value,
181- });
182-
183- /// Reads the salt from local storage.
184- static Future <Salt ?> readFromLocalStorage () async {
185- String ? value = await SimpleSecureStorage .read (_kPasswordDerivedKeySaltKey);
186- if (value == null ) {
187- return null ;
188- }
189- return Salt .fromRawValue (
190- value: base64.decode (value),
191- );
192- }
193-
194- /// Generates a random salt.
195- static Salt generate () => Salt .fromRawValue (
196- value: randomBytes (_saltLength),
197- );
198-
199- /// Deletes the salt from local storage.
200- static Future <void > deleteFromLocalStorage () async => await SimpleSecureStorage .delete (_kPasswordDerivedKeySaltKey);
201-
202- /// Writes the salt to the secure storage.
203- Future <void > saveToLocalStorage () async => await SimpleSecureStorage .write (_kPasswordDerivedKeySaltKey, base64.encode (value));
204-
205- @override
206- String toString () => base64.encode (value);
207- }
208-
209170/// Thrown when the password entered for a new crypto store is incorrect.
210171class _PasswordMismatchException extends LocalizableException {
211172 /// Creates a new password mismatch exception instance.
0 commit comments