cake_wallet/lib/bitcoin/file.dart

41 lines
1.2 KiB
Dart
Raw Normal View History

2020-05-12 12:04:54 +00:00
import 'dart:io';
import 'package:cake_wallet/bitcoin/key.dart';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:flutter/foundation.dart';
Future<void> write(
{@required String path,
@required String password,
2020-06-20 07:10:00 +00:00
@required String data}) async {
2020-05-12 12:04:54 +00:00
final keys = extractKeys(password);
final key = encrypt.Key.fromBase64(keys.first);
final iv = encrypt.IV.fromBase64(keys.last);
2020-06-20 07:10:00 +00:00
final encrypted = await encode(key: key, iv: iv, data: data);
2020-05-12 12:04:54 +00:00
final f = File(path);
f.writeAsStringSync(encrypted);
}
Future<void> writeData(
{@required String path,
@required String password,
@required String data}) async {
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);
}
2020-09-15 20:35:49 +00:00
Future<String> read({@required String path, @required String password}) async {
2020-05-12 12:04:54 +00:00
final file = File(path);
if (!file.existsSync()) {
file.createSync();
}
2020-09-15 20:35:49 +00:00
2020-05-12 12:04:54 +00:00
final encrypted = file.readAsStringSync();
return decode(password: password, data: encrypted);
}