瀏覽代碼

Move computer initialization to CryptoUtil

Vishnu Mohandas 4 年之前
父節點
當前提交
b6e5e810b3
共有 2 個文件被更改,包括 13 次插入7 次删除
  1. 2 2
      lib/main.dart
  2. 11 5
      lib/utils/crypto_util.dart

+ 2 - 2
lib/main.dart

@@ -1,7 +1,6 @@
 import 'dart:async';
 
 import 'package:background_fetch/background_fetch.dart';
-import 'package:computer/computer.dart';
 import 'package:flutter/material.dart';
 import 'package:in_app_purchase/in_app_purchase.dart';
 import 'package:path_provider/path_provider.dart';
@@ -12,6 +11,7 @@ import 'package:photos/services/collections_service.dart';
 import 'package:photos/services/memories_service.dart';
 import 'package:photos/services/sync_service.dart';
 import 'package:photos/ui/home_widget.dart';
+import 'package:photos/utils/crypto_util.dart';
 import 'package:sentry/sentry.dart';
 import 'package:super_logging/super_logging.dart';
 import 'package:logging/logging.dart';
@@ -48,8 +48,8 @@ void _main() async {
 
 Future _init() async {
   _logger.info("Initializing...");
-  Computer().turnOn(workersCount: 4);
   InAppPurchaseConnection.enablePendingPurchases();
+  CryptoUtil.init();
   await Configuration.instance.init();
   await BillingService.instance.init();
   await CollectionsService.instance.init();

+ 11 - 5
lib/utils/crypto_util.dart

@@ -96,6 +96,12 @@ void chachaDecrypt(Map<String, dynamic> args) {
 }
 
 class CryptoUtil {
+  static Computer _computer = Computer();
+
+  static init() {
+    _computer.turnOn(workersCount: 4);
+  }
+
   static EncryptionResult encryptSync(Uint8List source, Uint8List key) {
     final nonce = Sodium.randombytesBuf(Sodium.cryptoSecretboxNoncebytes);
 
@@ -117,7 +123,7 @@ class CryptoUtil {
     args["cipher"] = cipher;
     args["nonce"] = nonce;
     args["key"] = key;
-    return Computer().compute(cryptoSecretboxOpenEasy, param: args);
+    return _computer.compute(cryptoSecretboxOpenEasy, param: args);
   }
 
   static Uint8List decryptSync(
@@ -162,7 +168,7 @@ class CryptoUtil {
     args["sourceFilePath"] = sourceFilePath;
     args["destinationFilePath"] = destinationFilePath;
     args["key"] = key;
-    return Computer().compute(chachaEncryptFile, param: args);
+    return _computer.compute(chachaEncryptFile, param: args);
   }
 
   static Future<void> decryptFile(
@@ -176,7 +182,7 @@ class CryptoUtil {
     args["destinationFilePath"] = destinationFilePath;
     args["header"] = header;
     args["key"] = key;
-    return Computer().compute(chachaDecrypt, param: args);
+    return _computer.compute(chachaDecrypt, param: args);
   }
 
   static Uint8List generateKey() {
@@ -203,14 +209,14 @@ class CryptoUtil {
     args["input"] = input;
     args["opsLimit"] = Sodium.cryptoPwhashOpslimitSensitive;
     args["memLimit"] = Sodium.cryptoPwhashMemlimitModerate;
-    return utf8.decode(await Computer().compute(cryptoPwhashStr, param: args));
+    return utf8.decode(await _computer.compute(cryptoPwhashStr, param: args));
   }
 
   static Future<bool> verifyHash(Uint8List input, String hash) async {
     final args = Map<String, dynamic>();
     args["input"] = input;
     args["hash"] = utf8.encode(hash);
-    return await Computer().compute(cryptoPwhashStrVerify, param: args);
+    return await _computer.compute(cryptoPwhashStrVerify, param: args);
   }
 
   static Future<KeyPair> generateKeyPair() async {