2019-07-18 02:20:04 +00:00
|
|
|
part of 'repositories.dart';
|
2019-07-16 06:14:54 +00:00
|
|
|
|
|
|
|
class EncryptedSharedPreferences extends ConfigBase implements ConfigRepo {
|
|
|
|
@override
|
|
|
|
Future<ConnectionConfig> get connectionConfig async {
|
|
|
|
checkIfPasswordMatched();
|
|
|
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
final cipherText = prefs.getString(ConfigBase.keyConnectionConfig);
|
|
|
|
|
|
|
|
if (cipherText == null) return null;
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
final configJson = crypto.decrypt(cipherText, _password);
|
2019-07-16 06:14:54 +00:00
|
|
|
|
|
|
|
return ConnectionConfig.fromJson(json.decode(configJson));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> deleteAll() async {
|
|
|
|
checkIfPasswordMatched();
|
|
|
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
prefs.remove(ConfigBase.keyConnectionConfig);
|
|
|
|
prefs.remove(ConfigBase.keyPassword);
|
|
|
|
prefs.remove(ConfigBase.keyPrivateKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> matchesPasswordHash(String password) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
passwordMatched = crypto.matchHashedPassword(
|
|
|
|
prefs.getString(ConfigBase.keyPassword),
|
|
|
|
password,
|
|
|
|
);
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
if (passwordMatched) _password = password;
|
2019-07-16 06:14:54 +00:00
|
|
|
|
|
|
|
return passwordMatched;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> get passwordIsSet async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
final isSet = prefs.containsKey(ConfigBase.keyPassword);
|
|
|
|
passwordMatched = !isSet;
|
|
|
|
|
|
|
|
return isSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> get privateKey async {
|
|
|
|
checkIfPasswordMatched();
|
|
|
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
final cipherText = prefs.getString(ConfigBase.keyPrivateKey);
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
return crypto.decrypt(cipherText, _password);
|
2019-07-16 06:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setConnectionConfig(ConnectionConfig config) async {
|
|
|
|
checkIfPasswordMatched();
|
|
|
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
final configJson = json.encode(config);
|
|
|
|
|
|
|
|
prefs.setString(
|
|
|
|
ConfigBase.keyConnectionConfig,
|
2019-07-18 02:20:04 +00:00
|
|
|
crypto.encrypt(configJson, _password),
|
2019-07-16 06:14:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setPassword(String password) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
_password = password;
|
2019-07-16 06:14:54 +00:00
|
|
|
passwordMatched = true;
|
|
|
|
|
|
|
|
prefs.setString(ConfigBase.keyPassword, crypto.hashPassword(password));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setPrivateKey(String key) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
prefs.setString(ConfigBase.keyPrivateKey, crypto.encrypt(key, _password));
|
2019-07-16 06:14:54 +00:00
|
|
|
}
|
|
|
|
}
|