Prechádzať zdrojové kódy

Refactor crypto util

Vishnu Mohandas 4 rokov pred
rodič
commit
7f707d187e

+ 3 - 2
lib/models/chacha_encryption_result.dart

@@ -3,6 +3,7 @@ import 'dart:typed_data';
 class ChaChaEncryptionResult {
   final Uint8List encryptedData;
   final Uint8List header;
+  final Uint8List nonce;
 
-  ChaChaEncryptionResult(this.encryptedData, this.header);
-}
+  ChaChaEncryptionResult(this.encryptedData, {this.header, this.nonce});
+}

+ 17 - 17
lib/utils/crypto_util.dart

@@ -119,7 +119,21 @@ class CryptoUtil {
         EncryptionAttribute(bytes: encryptedData));
   }
 
-  static ChaChaEncryptionResult encryptStream(Uint8List source, Uint8List key) {
+  static Future<Uint8List> decrypt(
+      Uint8List cipher, Uint8List key, Uint8List nonce,
+      {bool background = false}) async {
+    final args = Map<String, dynamic>();
+    args["cipher"] = cipher;
+    args["nonce"] = nonce;
+    args["key"] = key;
+    if (background) {
+      return Computer().compute(cryptoSecretboxOpenEasy, param: args);
+    } else {
+      return cryptoSecretboxOpenEasy(args);
+    }
+  }
+
+  static ChaChaEncryptionResult encryptChaCha(Uint8List source, Uint8List key) {
     final initPushResult =
         Sodium.cryptoSecretstreamXchacha20poly1305InitPush(key);
     final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
@@ -127,10 +141,10 @@ class CryptoUtil {
         source,
         null,
         Sodium.cryptoSecretstreamXchacha20poly1305TagFinal);
-    return ChaChaEncryptionResult(encryptedData, initPushResult.header);
+    return ChaChaEncryptionResult(encryptedData, header: initPushResult.header);
   }
 
-  static Uint8List decryptStream(
+  static Uint8List decryptChaCha(
       Uint8List source, Uint8List key, Uint8List header) {
     final pullState =
         Sodium.cryptoSecretstreamXchacha20poly1305InitPull(header, key);
@@ -139,20 +153,6 @@ class CryptoUtil {
     return pullResult.m;
   }
 
-  static Future<Uint8List> decrypt(
-      Uint8List cipher, Uint8List key, Uint8List nonce,
-      {bool background = false}) async {
-    final args = Map<String, dynamic>();
-    args["cipher"] = cipher;
-    args["nonce"] = nonce;
-    args["key"] = key;
-    if (background) {
-      return Computer().compute(cryptoSecretboxOpenEasy, param: args);
-    } else {
-      return cryptoSecretboxOpenEasy(args);
-    }
-  }
-
   static Future<ChaChaAttributes> encryptFile(
     String sourceFilePath,
     String destinationFilePath,

+ 1 - 1
lib/utils/file_downloader.dart

@@ -42,7 +42,7 @@ class DiffFetcher {
               file.thumbnailDecryptionHeader =
                   item["thumbnailDecryptionHeader"];
               file.metadataDecryptionHeader = item["metadataDecryptionHeader"];
-              final encodedMetadata = CryptoUtil.decryptStream(
+              final encodedMetadata = CryptoUtil.decryptChaCha(
                 Sodium.base642bin(item["metadata"]["encryptedData"]),
                 await decryptFileKey(file),
                 Sodium.base642bin(file.metadataDecryptionHeader),

+ 2 - 2
lib/utils/file_uploader.dart

@@ -65,7 +65,7 @@ class FileUploader {
         file.generatedID.toString() + "_thumbnail.encrypted";
     final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName;
     final encryptedThumbnail =
-        CryptoUtil.encryptStream(thumbnailData, fileAttributes.key.bytes);
+        CryptoUtil.encryptChaCha(thumbnailData, fileAttributes.key.bytes);
     io.File(encryptedThumbnailPath)
         .writeAsBytesSync(encryptedThumbnail.encryptedData);
 
@@ -73,7 +73,7 @@ class FileUploader {
     String thumbnailObjectKey =
         await putFile(thumbnailUploadURL, io.File(encryptedThumbnailPath));
 
-    final encryptedMetadata = CryptoUtil.encryptStream(
+    final encryptedMetadata = CryptoUtil.encryptChaCha(
         utf8.encode(jsonEncode(file.getMetadata())), fileAttributes.key.bytes);
 
     final encryptedFileKey = await CryptoUtil.encrypt(

+ 1 - 1
lib/utils/file_util.dart

@@ -207,7 +207,7 @@ Future<io.File> _downloadAndDecryptThumbnail(File file) async {
   return Dio().download(file.getThumbnailUrl(), temporaryPath).then((_) async {
     final encryptedFile = io.File(temporaryPath);
     final thumbnailDecryptionKey = await decryptFileKey(file);
-    final data = CryptoUtil.decryptStream(
+    final data = CryptoUtil.decryptChaCha(
       encryptedFile.readAsBytesSync(),
       thumbnailDecryptionKey,
       Sodium.base642bin(file.thumbnailDecryptionHeader),