|
@@ -4,7 +4,6 @@ import 'dart:io' as io;
|
|
|
import 'package:aes_crypt/aes_crypt.dart';
|
|
|
import 'package:computer/computer.dart';
|
|
|
import 'package:encrypt/encrypt.dart';
|
|
|
-import 'dart:convert';
|
|
|
|
|
|
import 'package:photos/core/configuration.dart';
|
|
|
import 'package:steel_crypt/steel_crypt.dart' as steel;
|
|
@@ -15,6 +14,10 @@ class CryptoUtil {
|
|
|
return SecureRandom(length).bytes;
|
|
|
}
|
|
|
|
|
|
+ static String getSecureRandomString({int length = 32}) {
|
|
|
+ return SecureRandom(length).utf8;
|
|
|
+ }
|
|
|
+
|
|
|
static Uint8List scrypt(Uint8List plainText, Uint8List salt) {
|
|
|
return steel.PassCryptRaw.scrypt()
|
|
|
.hash(salt: salt, plain: plainText, len: 32);
|
|
@@ -41,56 +44,58 @@ class CryptoUtil {
|
|
|
}
|
|
|
|
|
|
static Future<String> encryptFileToFile(
|
|
|
- String sourcePath, String destinationPath, Uint8List key) async {
|
|
|
+ String sourcePath, String destinationPath, String password) async {
|
|
|
final args = Map<String, dynamic>();
|
|
|
- args["key"] = key;
|
|
|
+ args["password"] = password;
|
|
|
args["source"] = sourcePath;
|
|
|
args["destination"] = destinationPath;
|
|
|
return Computer().compute(runEncryptFileToFile, param: args);
|
|
|
}
|
|
|
|
|
|
static Future<String> encryptDataToFile(
|
|
|
- Uint8List source, String destinationPath, Uint8List key) async {
|
|
|
+ Uint8List source, String destinationPath, String password) async {
|
|
|
final args = Map<String, dynamic>();
|
|
|
- args["key"] = key;
|
|
|
+ args["password"] = password;
|
|
|
args["source"] = source;
|
|
|
args["destination"] = destinationPath;
|
|
|
return Computer().compute(runEncryptDataToFile, param: args);
|
|
|
}
|
|
|
|
|
|
- static Future<String> encryptDataToData(
|
|
|
- Uint8List source, Uint8List key) async {
|
|
|
+ static Future<Uint8List> encryptDataToData(
|
|
|
+ Uint8List source, String password) async {
|
|
|
final destinationPath =
|
|
|
Configuration.instance.getTempDirectory() + Uuid().v4();
|
|
|
- return encryptDataToFile(source, destinationPath, key).then((value) {
|
|
|
+ return encryptDataToFile(source, destinationPath, password).then((value) {
|
|
|
final file = io.File(destinationPath);
|
|
|
final data = file.readAsBytesSync();
|
|
|
file.deleteSync();
|
|
|
- return base64.encode(data);
|
|
|
+ return data;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
static Future<void> decryptFileToFile(
|
|
|
- String sourcePath, String destinationPath, Uint8List key) async {
|
|
|
+ String sourcePath, String destinationPath, String password) async {
|
|
|
final args = Map<String, dynamic>();
|
|
|
- args["key"] = key;
|
|
|
+ args["password"] = password;
|
|
|
args["source"] = sourcePath;
|
|
|
args["destination"] = destinationPath;
|
|
|
return Computer().compute(runDecryptFileToFile, param: args);
|
|
|
}
|
|
|
|
|
|
- static Future<Uint8List> decryptFileToData(String sourcePath, Uint8List key) {
|
|
|
+ static Future<Uint8List> decryptFileToData(
|
|
|
+ String sourcePath, String password) {
|
|
|
final args = Map<String, dynamic>();
|
|
|
- args["key"] = key;
|
|
|
+ args["password"] = password;
|
|
|
args["source"] = sourcePath;
|
|
|
return Computer().compute(runDecryptFileToData, param: args);
|
|
|
}
|
|
|
|
|
|
- static Future<Uint8List> decryptDataToData(Uint8List source, Uint8List key) {
|
|
|
+ static Future<Uint8List> decryptDataToData(
|
|
|
+ Uint8List source, String password) {
|
|
|
final sourcePath = Configuration.instance.getTempDirectory() + Uuid().v4();
|
|
|
final file = io.File(sourcePath);
|
|
|
file.writeAsBytesSync(source);
|
|
|
- return decryptFileToData(sourcePath, key).then((value) {
|
|
|
+ return decryptFileToData(sourcePath, password).then((value) {
|
|
|
file.deleteSync();
|
|
|
return value;
|
|
|
});
|
|
@@ -98,27 +103,27 @@ class CryptoUtil {
|
|
|
}
|
|
|
|
|
|
Future<String> runEncryptFileToFile(Map<String, dynamic> args) {
|
|
|
- final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
|
|
|
+ final encrypter = getEncrypter(args["password"]);
|
|
|
return encrypter.encryptFile(args["source"], args["destination"]);
|
|
|
}
|
|
|
|
|
|
Future<String> runEncryptDataToFile(Map<String, dynamic> args) {
|
|
|
- final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
|
|
|
+ final encrypter = getEncrypter(args["password"]);
|
|
|
return encrypter.encryptDataToFile(args["source"], args["destination"]);
|
|
|
}
|
|
|
|
|
|
Future<String> runDecryptFileToFile(Map<String, dynamic> args) async {
|
|
|
- final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
|
|
|
+ final encrypter = getEncrypter(args["password"]);
|
|
|
return encrypter.decryptFile(args["source"], args["destination"]);
|
|
|
}
|
|
|
|
|
|
Future<Uint8List> runDecryptFileToData(Map<String, dynamic> args) async {
|
|
|
- final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
|
|
|
+ final encrypter = getEncrypter(args["password"]);
|
|
|
return encrypter.decryptDataFromFile(args["source"]);
|
|
|
}
|
|
|
|
|
|
-AesCrypt getEncrypter(String key) {
|
|
|
- final encrypter = AesCrypt(key);
|
|
|
+AesCrypt getEncrypter(String password) {
|
|
|
+ final encrypter = AesCrypt(password);
|
|
|
encrypter.aesSetMode(AesMode.cbc);
|
|
|
encrypter.setOverwriteMode(AesCryptOwMode.on);
|
|
|
return encrypter;
|