import "package:shared_preferences/shared_preferences.dart"; class LockScreenSettings { LockScreenSettings._privateConstructor(); static final LockScreenSettings instance = LockScreenSettings._privateConstructor(); static const password = "ls_password"; static const pin = "ls_pin"; static const saltKey = "ls_salt"; static const keyInvalidAttempts = "ls_invalid_attempts"; static const lastInvalidAttemptTime = "ls_last_invalid_attempt_time"; static const autoLockTime = "ls_auto_lock_time"; final List autoLockDurations = const [ Duration(seconds: 0), Duration(seconds: 30), Duration(minutes: 1), Duration(minutes: 5), Duration(minutes: 15), Duration(minutes: 30), Duration(hours: 1), ]; late SharedPreferences _preferences; Future init() async { _preferences = await SharedPreferences.getInstance(); } Future setAutoLockTime(Duration duration) async { await _preferences.setInt(autoLockTime, duration.inMilliseconds); } int getAutoLockTime() { return _preferences.getInt(autoLockTime) ?? 0; } Future setLastInvalidAttemptTime(int time) async { await _preferences.setInt(lastInvalidAttemptTime, time); } int getlastInvalidAttemptTime() { return _preferences.getInt(lastInvalidAttemptTime) ?? 0; } int getInvalidAttemptCount() { return _preferences.getInt(keyInvalidAttempts) ?? 0; } Future setInvalidAttemptCount(int count) async { await _preferences.setInt(keyInvalidAttempts, count); } // static Uint8List _generateSalt() { // return Sodium.randombytesBuf(Sodium.cryptoPwhashSaltbytes); // } Future setPin(String userPin) async { //await _secureStorage.delete(key: saltKey); await _preferences.setString(pin, userPin); await _preferences.remove(password); // final salt = _generateSalt(); // final hash = cryptoPwHash({ // "password": utf8.encode(userPin), // "salt": salt, // "opsLimit": Sodium.cryptoPwhashOpslimitInteractive, // "memLimit": Sodium.cryptoPwhashMemlimitInteractive, // }); // final String saltPin = base64Encode(salt); // final String hashedPin = base64Encode(hash); // await _secureStorage.write(key: saltKey, value: saltPin); // await _secureStorage.write(key: pin, value: hashedPin); // await _secureStorage.delete(key: password); return; } // Future getSalt() async { // final String? salt = await _secureStorage.read(key: saltKey); // if (salt == null) return null; // return base64Decode(salt); // } Future getPin() async { return _preferences.getString(pin); // return _secureStorage.read(key: pin); } Future setPassword(String pass) async { await _preferences.setString(password, pass); await _preferences.remove(pin); // await _secureStorage.delete(key: saltKey); // final salt = _generateSalt(); // final hash = cryptoPwHash({ // "password": utf8.encode(pass), // "salt": salt, // "opsLimit": Sodium.cryptoPwhashOpslimitInteractive, // "memLimit": Sodium.cryptoPwhashMemlimitInteractive, // }); // final String saltPassword = base64Encode(salt); // final String hashPassword = base64Encode(hash); // await _secureStorage.write(key: saltKey, value: saltPassword); // await _secureStorage.write(key: password, value: hashPassword); // await _secureStorage.delete(key: pin); return; } Future getPassword() async { return _preferences.getString(password); // return _secureStorage.read(key: password); } Future removePinAndPassword() async { await _preferences.remove(pin); await _preferences.remove(password); // await _secureStorage.delete(key: saltKey); // await _secureStorage.delete(key: pin); // await _secureStorage.delete(key: password); } Future isPinSet() async { return _preferences.containsKey(pin); // return await _secureStorage.containsKey(key: pin); } Future isPasswordSet() async { return _preferences.containsKey(password); // return await _secureStorage.containsKey(key: password); } }