Browse Source

Decrypt keys of files within collections using the collectionKey

Vishnu Mohandas 4 years ago
parent
commit
8f8594f07f
2 changed files with 31 additions and 15 deletions
  1. 20 11
      lib/services/collections_service.dart
  2. 11 4
      lib/utils/file_util.dart

+ 20 - 11
lib/services/collections_service.dart

@@ -15,7 +15,9 @@ class CollectionsService {
 
 
   CollectionsDB _db;
   CollectionsDB _db;
   Configuration _config;
   Configuration _config;
-  Map<String, Collection> _localCollections;
+  final _localCollections = Map<String, Collection>();
+  final _collectionIDToCollection = Map<int, Collection>();
+  final _cachedKeys = Map<int, Uint8List>();
 
 
   CollectionsService._privateConstructor() {
   CollectionsService._privateConstructor() {
     _db = CollectionsDB.instance;
     _db = CollectionsDB.instance;
@@ -39,6 +41,7 @@ class CollectionsService {
             Sodium.base642bin(collection.pathDecryptionNonce)));
             Sodium.base642bin(collection.pathDecryptionNonce)));
         _localCollections[path] = collection;
         _localCollections[path] = collection;
       }
       }
+      _collectionIDToCollection[collection.id] = collection;
     }
     }
   }
   }
 
 
@@ -63,17 +66,23 @@ class CollectionsService {
     });
     });
   }
   }
 
 
-  Uint8List getCollectionKey(Collection collection) {
-    final encryptedKey = Sodium.base642bin(collection.encryptedKey);
-    if (collection.ownerID == _config.getUserID()) {
-      return CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
-          Sodium.base642bin(collection.keyDecryptionNonce));
-    } else {
-      return CryptoUtil.openSealSync(
-          encryptedKey,
-          Sodium.base642bin(_config.getKeyAttributes().publicKey),
-          _config.getSecretKey());
+  Uint8List getCollectionKey(int collectionID) {
+    if (!_cachedKeys.containsKey(collectionID)) {
+      final collection = _collectionIDToCollection[collectionID];
+      final encryptedKey = Sodium.base642bin(collection.encryptedKey);
+      var key;
+      if (collection.ownerID == _config.getUserID()) {
+        key = CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
+            Sodium.base642bin(collection.keyDecryptionNonce));
+      } else {
+        key = CryptoUtil.openSealSync(
+            encryptedKey,
+            Sodium.base642bin(_config.getKeyAttributes().publicKey),
+            _config.getSecretKey());
+      }
+      _cachedKeys[collection.id] = key;
     }
     }
+    return _cachedKeys[collectionID];
   }
   }
 
 
   Future<List<Collection>> getCollections(int sinceTime) {
   Future<List<Collection>> getCollections(int sinceTime) {

+ 11 - 4
lib/utils/file_util.dart

@@ -17,6 +17,7 @@ import 'package:photos/core/constants.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/models/file.dart';
 import 'package:photos/models/file.dart';
 import 'package:photos/models/file_type.dart';
 import 'package:photos/models/file_type.dart';
+import 'package:photos/services/collections_service.dart';
 
 
 import 'crypto_util.dart';
 import 'crypto_util.dart';
 
 
@@ -217,8 +218,14 @@ Future<io.File> _downloadAndDecryptThumbnail(File file) async {
 }
 }
 
 
 Uint8List decryptFileKey(File file) {
 Uint8List decryptFileKey(File file) {
-  return CryptoUtil.decryptSync(
-      Sodium.base642bin(file.encryptedKey),
-      Configuration.instance.getKey(),
-      Sodium.base642bin(file.keyDecryptionNonce));
+  final encryptedKey = Sodium.base642bin(file.encryptedKey);
+  final nonce = Sodium.base642bin(file.keyDecryptionNonce);
+  if (file.ownerID == Configuration.instance.getUserID()) {
+    return CryptoUtil.decryptSync(
+        encryptedKey, Configuration.instance.getKey(), nonce);
+  } else {
+    final collectionKey =
+        CollectionsService.instance.getCollectionKey(file.collectionID);
+    return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
+  }
 }
 }