Skip to content

Commit

Permalink
Fixed issue with oppen ssl on some android devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Feb 20, 2024
1 parent 75b049f commit 1178daf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Mobileraker - Changelog

## [2.6.12] - 2024-02-20

### Bug Fixes

- Fixed an issue that prevented the app from starting on some android devices.

## [2.6.11] - 2024-02-02

### Major Updates
Expand Down
51 changes: 26 additions & 25 deletions lib/app_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ import 'package:worker_manager/worker_manager.dart';

part 'app_setup.g.dart';

const _hiveKeyName = 'hive_key';

setupBoxes() async {
await Hive.initFlutter();

Uint8List keyMaterial = await _hiveKey();
// 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.
// Caused problems on some devices and the key is not used for hive encryption.
// Uint8List keyMaterial = await _hiveKey();

// Ignore old/deperecates types!
// 2 - WebcamSetting
Expand Down Expand Up @@ -98,7 +102,8 @@ setupBoxes() async {
// Hive.deleteBoxFromDisk('printers');

try {
await openBoxes(keyMaterial);
// await openBoxes(keyMaterial);
await openBoxes();
Hive.box<Machine>("printers").values.forEach((element) {
logger.i('Machine in box is ${element.logName}#${element.hashCode}');
// ToDo remove after machine migration!
Expand Down Expand Up @@ -130,7 +135,6 @@ setupBoxes() async {
}

Future<Uint8List> _hiveKey() async {
const keyName = 'hive_key';

/// due to the move to encSharedPref it could be that the hive_key is still in the normmal shared pref
/// Therfore first try to load it from the secureShared pref else try the normal one else generate a new one
Expand All @@ -141,38 +145,35 @@ Future<Uint8List> _hiveKey() async {
aOptions: AndroidOptions(encryptedSharedPreferences: false),
);

Uint8List? encryptionKey;
try {
encryptionKey = await secureStorage.read(key: keyName).then((value) => value?.let(base64Decode));
} on PlatformException catch (e) {
logger.e('Error while reading hive_key from secure storage', e);
encryptionKey = await nonEncSharedPrefSecureStorage
.read(key: keyName)
.then((value) => value?.let(base64Decode))
.onError((error, stackTrace) {
logger.e('Error while reading hive_key from non-encryptedSharedPreferences', error, stackTrace);
return null;
});
await nonEncSharedPrefSecureStorage.delete(key: keyName);
await secureStorage.write(
key: keyName,
value: encryptionKey?.let(base64Encode),
);
logger.e(
'Transfered hive_key from non-encryptedSharedPreferences to secureStorage using encryptedSharedPreferences',
);
Uint8List? encryptionKey = await _readStorage(secureStorage);
if (encryptionKey != null) {
return encryptionKey;
}

encryptionKey ??= await _readStorage(nonEncSharedPrefSecureStorage);
if (encryptionKey != null) {
await secureStorage.write(key: _hiveKeyName, value: encryptionKey.let(base64Encode));
await nonEncSharedPrefSecureStorage.delete(key: _hiveKeyName);
return encryptionKey;
}

final key = Hive.generateSecureKey();
await secureStorage.write(key: keyName, value: base64UrlEncode(key));
await secureStorage.write(key: _hiveKeyName, value: base64UrlEncode(key));
return Uint8List.fromList(key);
}

Future<List<Box>> openBoxes(Uint8List _) {
Future<Uint8List?> _readStorage(FlutterSecureStorage storage) async {
try {
String? value = await storage.read(key: _hiveKeyName);
return value?.let(base64Decode);
} catch (e) {
logger.e('Error while reading $_hiveKeyName from storage', e);
return null;
}
}

// Future<List<Box>> openBoxes(Uint8List _) {
Future<List<Box>> openBoxes() {
return Future.wait([
Hive.openBox<Machine>('printers').then(_migrateMachine),
Hive.openBox<String>('uuidbox'),
Expand Down

0 comments on commit 1178daf

Please sign in to comment.