mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-23 12:09:43 +00:00
42 lines
No EOL
1.5 KiB
Dart
42 lines
No EOL
1.5 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:cw_bitcoin/file.dart' as bf;
|
|
import 'package:cake_backup/backup.dart' as cwb;
|
|
|
|
EncryptionFileUtils encryptionFileUtilsFor(bool direct)
|
|
=> direct
|
|
? XChaCha20EncryptionFileUtils()
|
|
: Salsa20EncryhptionFileUtils();
|
|
|
|
abstract class EncryptionFileUtils {
|
|
Future<void> write({required String path, required String password, required String data});
|
|
Future<String> read({required String path, required String password});
|
|
}
|
|
|
|
class Salsa20EncryhptionFileUtils extends EncryptionFileUtils {
|
|
// Requires legacy complex key + iv as password
|
|
@override
|
|
Future<void> write({required String path, required String password, required String data}) async
|
|
=> await bf.write(path: path, password: password, data: data);
|
|
|
|
// Requires legacy complex key + iv as password
|
|
@override
|
|
Future<String> read({required String path, required String password}) async
|
|
=> await bf.read(path: path, password: password);
|
|
}
|
|
|
|
class XChaCha20EncryptionFileUtils extends EncryptionFileUtils {
|
|
@override
|
|
Future<void> write({required String path, required String password, required String data}) async {
|
|
final encrypted = await cwb.encrypt(password, Uint8List.fromList(data.codeUnits));
|
|
await File(path).writeAsBytes(encrypted);
|
|
}
|
|
|
|
@override
|
|
Future<String> read({required String path, required String password}) async {
|
|
final file = File(path);
|
|
final encrypted = await file.readAsBytes();
|
|
final bytes = await cwb.decrypt(password, encrypted);
|
|
return String.fromCharCodes(bytes);
|
|
}
|
|
} |