Bläddra i källkod

Remove more unnecessary models

Vishnu Mohandas 4 år sedan
förälder
incheckning
8da3f0c687

+ 0 - 9
lib/models/encrypted_data_attributes.dart

@@ -1,9 +0,0 @@
-import 'dart:typed_data';
-
-class EncryptedData {
-  final Uint8List key;
-  final Uint8List nonce;
-  final Uint8List encryptedData;
-
-  EncryptedData(this.key, this.nonce, this.encryptedData);
-}

+ 0 - 8
lib/models/encrypted_file_attributes.dart

@@ -1,8 +0,0 @@
-import 'dart:typed_data';
-
-class ChaChaAttributes {
-  final Uint8List key;
-  final Uint8List header;
-
-  ChaChaAttributes(this.key, this.header);
-}

+ 2 - 1
lib/models/encryption_result.dart

@@ -2,8 +2,9 @@ import 'dart:typed_data';
 
 class EncryptionResult {
   final Uint8List encryptedData;
+  final Uint8List key;
   final Uint8List header;
   final Uint8List nonce;
 
-  EncryptionResult(this.encryptedData, {this.header, this.nonce});
+  EncryptionResult({this.encryptedData, this.key, this.header, this.nonce});
 }

+ 13 - 12
lib/utils/crypto_util.dart

@@ -7,9 +7,6 @@ import 'package:flutter_sodium/flutter_sodium.dart';
 import 'package:logging/logging.dart';
 import 'package:photos/models/encryption_result.dart';
 
-import 'package:photos/models/encrypted_data_attributes.dart';
-import 'package:photos/models/encrypted_file_attributes.dart';
-
 final int encryptionChunkSize = 4 * 1024 * 1024;
 final int decryptionChunkSize =
     encryptionChunkSize + Sodium.cryptoSecretstreamXchacha20poly1305Abytes;
@@ -32,7 +29,7 @@ bool cryptoPwhashStrVerify(Map<String, dynamic> args) {
   return Sodium.cryptoPwhashStrVerify(args["hash"], args["input"]) == 0;
 }
 
-ChaChaAttributes chachaEncryptFile(Map<String, dynamic> args) {
+EncryptionResult chachaEncryptFile(Map<String, dynamic> args) {
   final encryptionStartTime = DateTime.now().millisecondsSinceEpoch;
   final logger = Logger("ChaChaEncrypt");
   final sourceFile = io.File(args["sourceFilePath"]);
@@ -63,7 +60,7 @@ ChaChaAttributes chachaEncryptFile(Map<String, dynamic> args) {
   logger.info("Encryption time: " +
       (DateTime.now().millisecondsSinceEpoch - encryptionStartTime).toString());
 
-  return ChaChaAttributes(key, initPushResult.header);
+  return EncryptionResult(key: key, header: initPushResult.header);
 }
 
 void chachaDecrypt(Map<String, dynamic> args) {
@@ -99,7 +96,8 @@ void chachaDecrypt(Map<String, dynamic> args) {
 }
 
 class CryptoUtil {
-  static Future<EncryptedData> encrypt(Uint8List source, Uint8List key) async {
+  static Future<EncryptionResult> encrypt(
+      Uint8List source, Uint8List key) async {
     final nonce = Sodium.randombytesBuf(Sodium.cryptoSecretboxNoncebytes);
 
     final args = Map<String, dynamic>();
@@ -107,7 +105,8 @@ class CryptoUtil {
     args["nonce"] = nonce;
     args["key"] = key;
     final encryptedData = cryptoSecretboxEasy(args);
-    return EncryptedData(key, nonce, encryptedData);
+    return EncryptionResult(
+        key: key, nonce: nonce, encryptedData: encryptedData);
   }
 
   static Future<Uint8List> decrypt(
@@ -132,7 +131,8 @@ class CryptoUtil {
         source,
         null,
         Sodium.cryptoSecretstreamXchacha20poly1305TagFinal);
-    return EncryptionResult(encryptedData, header: initPushResult.header);
+    return EncryptionResult(
+        encryptedData: encryptedData, header: initPushResult.header);
   }
 
   static Uint8List decryptChaCha(
@@ -144,7 +144,7 @@ class CryptoUtil {
     return pullResult.m;
   }
 
-  static Future<ChaChaAttributes> encryptFile(
+  static Future<EncryptionResult> encryptFile(
     String sourceFilePath,
     String destinationFilePath,
   ) {
@@ -157,13 +157,14 @@ class CryptoUtil {
   static Future<void> decryptFile(
     String sourceFilePath,
     String destinationFilePath,
-    ChaChaAttributes attributes,
+    Uint8List header,
+    Uint8List key,
   ) {
     final args = Map<String, dynamic>();
     args["sourceFilePath"] = sourceFilePath;
     args["destinationFilePath"] = destinationFilePath;
-    args["header"] = attributes.header;
-    args["key"] = attributes.key;
+    args["header"] = header;
+    args["key"] = key;
     return Computer().compute(chachaDecrypt, param: args);
   }
 

+ 4 - 6
lib/utils/file_util.dart

@@ -15,7 +15,6 @@ import 'package:photos/core/cache/video_cache_manager.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/constants.dart';
 import 'package:photos/db/files_db.dart';
-import 'package:photos/models/encrypted_file_attributes.dart';
 import 'package:photos/models/file.dart';
 import 'package:photos/models/file_type.dart';
 
@@ -175,12 +174,11 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
       return null;
     }
     logger.info("File downloaded: " + file.uploadedFileID.toString());
-    var attributes = ChaChaAttributes(
-      await decryptFileKey(file),
-      Sodium.base642bin(file.fileDecryptionHeader),
-    );
     await CryptoUtil.decryptFile(
-        encryptedFilePath, decryptedFilePath, attributes);
+        encryptedFilePath,
+        decryptedFilePath,
+        await decryptFileKey(file),
+        Sodium.base642bin(file.fileDecryptionHeader));
     logger.info("File decrypted: " + file.uploadedFileID.toString());
     io.File(encryptedFilePath).deleteSync();
     final fileExtension = extension(file.title).substring(1).toLowerCase();