@@ -44,10 +44,14 @@ import 'package:worker_manager/worker_manager.dart';
44
44
45
45
part 'app_setup.g.dart' ;
46
46
47
+ const _hiveKeyName = 'hive_key' ;
48
+
47
49
setupBoxes () async {
48
50
await Hive .initFlutter ();
49
51
50
- Uint8List keyMaterial = await _hiveKey ();
52
+ // For the key is not needed. It can be used to encrypt the data in the box. But this is not a high security app with sensitive data.
53
+ // Caused problems on some devices and the key is not used for hive encryption.
54
+ // Uint8List keyMaterial = await _hiveKey();
51
55
52
56
// Ignore old/deperecates types!
53
57
// 2 - WebcamSetting
@@ -98,7 +102,8 @@ setupBoxes() async {
98
102
// Hive.deleteBoxFromDisk('printers');
99
103
100
104
try {
101
- await openBoxes (keyMaterial);
105
+ // await openBoxes(keyMaterial);
106
+ await openBoxes ();
102
107
Hive .box <Machine >("printers" ).values.forEach ((element) {
103
108
logger.i ('Machine in box is ${element .logName }#${element .hashCode }' );
104
109
// ToDo remove after machine migration!
@@ -130,7 +135,6 @@ setupBoxes() async {
130
135
}
131
136
132
137
Future <Uint8List > _hiveKey () async {
133
- const keyName = 'hive_key' ;
134
138
135
139
/// due to the move to encSharedPref it could be that the hive_key is still in the normmal shared pref
136
140
/// Therfore first try to load it from the secureShared pref else try the normal one else generate a new one
@@ -141,38 +145,35 @@ Future<Uint8List> _hiveKey() async {
141
145
aOptions: AndroidOptions (encryptedSharedPreferences: false ),
142
146
);
143
147
144
- Uint8List ? encryptionKey;
145
- try {
146
- encryptionKey = await secureStorage.read (key: keyName).then ((value) => value? .let (base64Decode));
147
- } on PlatformException catch (e) {
148
- logger.e ('Error while reading hive_key from secure storage' , e);
149
- encryptionKey = await nonEncSharedPrefSecureStorage
150
- .read (key: keyName)
151
- .then ((value) => value? .let (base64Decode))
152
- .onError ((error, stackTrace) {
153
- logger.e ('Error while reading hive_key from non-encryptedSharedPreferences' , error, stackTrace);
154
- return null ;
155
- });
156
- await nonEncSharedPrefSecureStorage.delete (key: keyName);
157
- await secureStorage.write (
158
- key: keyName,
159
- value: encryptionKey? .let (base64Encode),
160
- );
161
- logger.e (
162
- 'Transfered hive_key from non-encryptedSharedPreferences to secureStorage using encryptedSharedPreferences' ,
163
- );
148
+ Uint8List ? encryptionKey = await _readStorage (secureStorage);
149
+ if (encryptionKey != null ) {
150
+ return encryptionKey;
164
151
}
165
152
153
+ encryptionKey ?? = await _readStorage (nonEncSharedPrefSecureStorage);
166
154
if (encryptionKey != null ) {
155
+ await secureStorage.write (key: _hiveKeyName, value: encryptionKey.let (base64Encode));
156
+ await nonEncSharedPrefSecureStorage.delete (key: _hiveKeyName);
167
157
return encryptionKey;
168
158
}
169
159
170
160
final key = Hive .generateSecureKey ();
171
- await secureStorage.write (key: keyName , value: base64UrlEncode (key));
161
+ await secureStorage.write (key: _hiveKeyName , value: base64UrlEncode (key));
172
162
return Uint8List .fromList (key);
173
163
}
174
164
175
- Future <List <Box >> openBoxes (Uint8List _) {
165
+ Future <Uint8List ?> _readStorage (FlutterSecureStorage storage) async {
166
+ try {
167
+ String ? value = await storage.read (key: _hiveKeyName);
168
+ return value? .let (base64Decode);
169
+ } catch (e) {
170
+ logger.e ('Error while reading $_hiveKeyName from storage' , e);
171
+ return null ;
172
+ }
173
+ }
174
+
175
+ // Future<List<Box>> openBoxes(Uint8List _) {
176
+ Future <List <Box >> openBoxes () {
176
177
return Future .wait ([
177
178
Hive .openBox <Machine >('printers' ).then (_migrateMachine),
178
179
Hive .openBox <String >('uuidbox' ),
0 commit comments