2021-12-24 12:52:08 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:cw_core/key.dart';
|
|
|
|
import 'package:encrypt/encrypt.dart' as encrypt;
|
|
|
|
|
2023-12-02 01:02:55 +00:00
|
|
|
Future<void> write({required String path, required String password, required String data}) async =>
|
|
|
|
writeData(path: path, password: password, data: data);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
Future<void> writeData(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required String path,
|
|
|
|
required String password,
|
|
|
|
required String data}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
final keys = extractKeys(password);
|
|
|
|
final key = encrypt.Key.fromBase64(keys.first);
|
|
|
|
final iv = encrypt.IV.fromBase64(keys.last);
|
|
|
|
final encrypted = await encode(key: key, iv: iv, data: data);
|
|
|
|
final f = File(path);
|
|
|
|
f.writeAsStringSync(encrypted);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<String> read({required String path, required String password}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
final file = File(path);
|
|
|
|
|
|
|
|
if (!file.existsSync()) {
|
|
|
|
file.createSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
final encrypted = file.readAsStringSync();
|
|
|
|
|
|
|
|
return decode(password: password, data: encrypted);
|
|
|
|
}
|