Refactor and start with 256MB as memLimit for lowSpecDevices
This commit is contained in:
@@ -17,6 +17,7 @@ import 'package:ente_auth/ui/code_widget.dart';
|
||||
import 'package:ente_auth/ui/common/loading_widget.dart';
|
||||
import 'package:ente_auth/ui/scanner_page.dart';
|
||||
import 'package:ente_auth/ui/settings_page.dart';
|
||||
import 'package:ente_auth/utils/device_info.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
|
||||
import 'package:move_to_background/move_to_background.dart';
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import 'dart:io' as io;
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:computer/computer.dart';
|
||||
import 'package:ente_auth/models/derived_key_result.dart';
|
||||
import 'package:ente_auth/models/encryption_result.dart';
|
||||
import 'package:ente_auth/utils/device_info.dart';
|
||||
import 'package:flutter_sodium/flutter_sodium.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
@@ -283,35 +283,21 @@ class CryptoUtil {
|
||||
return Sodium.cryptoBoxSeal(input, publicKey);
|
||||
}
|
||||
|
||||
static bool _isLowSpecDevice() {
|
||||
try {
|
||||
if (Platform.isIOS) {
|
||||
String version = Platform.operatingSystemVersion;
|
||||
int majorVersion = int.parse(version.split('.').first);
|
||||
// iPhone 6 or lower
|
||||
if (majorVersion <= 9) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Logger("_isLowSpecDevice").severe("deviceSpec check failed", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static Future<DerivedKeyResult> deriveSensitiveKey(
|
||||
Uint8List password,
|
||||
Uint8List salt,
|
||||
) async {
|
||||
final logger = Logger("pwhash");
|
||||
// Default with 1 GB mem and 4 ops limit
|
||||
int memLimit = Sodium.cryptoPwhashMemlimitSensitive;
|
||||
int opsLimit = Sodium.cryptoPwhashOpslimitSensitive;
|
||||
|
||||
if (_isLowSpecDevice()) {
|
||||
if (await isLowSpecDevice()) {
|
||||
logger.info("low spec device detected");
|
||||
// When high memLimit is used, on low spec device the OS might kill the
|
||||
// app with OOM. To avoid that, default to max ops limit
|
||||
memLimit = Sodium.cryptoPwhashMemlimitMin;
|
||||
opsLimit = Sodium.cryptoPwhashOpslimitMax;
|
||||
// app with OOM. To avoid that, start with 256 MB and 16 ops limit
|
||||
memLimit = Sodium.cryptoPwhashMemlimitModerate;
|
||||
opsLimit = 16;
|
||||
}
|
||||
Uint8List key;
|
||||
while (memLimit >= Sodium.cryptoPwhashMemlimitMin &&
|
||||
|
||||
31
lib/utils/device_info.dart
Normal file
31
lib/utils/device_info.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
late DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
|
||||
|
||||
// https://gist.github.com/adamawolf/3048717
|
||||
late Set<String> iOSLowEndMachineCodes = <String>{
|
||||
"iPhone5,2",
|
||||
"iPhone5,3",
|
||||
"iPhone5,4",
|
||||
"iPhone6,1",
|
||||
"iPhone6,2",
|
||||
"iPhone7,2",
|
||||
"iPhone7,1",
|
||||
};
|
||||
|
||||
Future<bool> isLowSpecDevice() async {
|
||||
try {
|
||||
if (Platform.isIOS) {
|
||||
final IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
|
||||
debugPrint("ios utc name ${iosInfo.utsname.machine}");
|
||||
return iOSLowEndMachineCodes.contains(iosInfo.utsname.machine);
|
||||
}
|
||||
} catch (e) {
|
||||
Logger("device_info").severe("deviceSpec check failed", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user