| 1 | import 'dart:io'; |
| 2 | import 'package:cw_core/key.dart'; |
| 3 | import 'package:encrypt/encrypt.dart' as encrypt; |
| 4 | |
| 5 | Future<void> write({required String path, required String password, required String data}) async => |
| 6 | writeData(path: path, password: password, data: data); |
| 7 | |
| 8 | Future<void> writeData( |
| 9 | {required String path, required String password, required String data}) async { |
| 10 | final keys = extractKeys(password); |
| 11 | final key = encrypt.Key.fromBase64(keys.first); |
| 12 | final iv = encrypt.IV.fromBase64(keys.last); |
| 13 | final encrypted = await encode(key: key, iv: iv, data: data); |
| 14 | final f = File(path); |
| 15 | f.writeAsStringSync(encrypted); |
| 16 | } |
| 17 | |
| 18 | Future<String> read({required String path, required String password}) async { |
| 19 | final file = File(path); |
| 20 | |
| 21 | if (!file.existsSync()) { |
| 22 | file.createSync(); |
| 23 | } |
| 24 | |
| 25 | final encrypted = file.readAsStringSync(); |
| 26 | |
| 27 | return decode(password: password, data: encrypted); |
| 28 | } |