mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-11-01 01:47:41 +00:00
44 lines
1.2 KiB
Dart
44 lines
1.2 KiB
Dart
|
import 'dart:io';
|
||
|
import 'dart:convert';
|
||
|
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,
|
||
|
@required Map<String, String> obj}) async {
|
||
|
final jsoned = json.encode(obj);
|
||
|
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: jsoned);
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
Future<String> read(
|
||
|
{@required String path, @required String password}) async {
|
||
|
final file = File(path);
|
||
|
|
||
|
if (!file.existsSync()) {
|
||
|
file.createSync();
|
||
|
}
|
||
|
|
||
|
final encrypted = file.readAsStringSync();
|
||
|
|
||
|
return decode(password: password, data: encrypted);
|
||
|
}
|