瀏覽代碼

Explicitly call the AESCrypt password as password

Vishnu Mohandas 4 年之前
父節點
當前提交
87fd87987a

+ 11 - 11
lib/db/files_db.dart

@@ -31,8 +31,8 @@ class FilesDB {
   static final columnCreationTime = 'creation_time';
   static final columnModificationTime = 'modification_time';
   static final columnUpdationTime = 'updation_time';
-  static final columnEncryptedKey = 'encrypted_key';
-  static final columnEncryptedKeyIV = 'encrypted_key_iv';
+  static final columnEncryptedPassword = 'encrypted_password';
+  static final columnEncryptedPasswordIV = 'encrypted_password_iv';
 
   // make this a singleton class
   FilesDB._privateConstructor();
@@ -74,8 +74,8 @@ class FilesDB {
             $columnCreationTime TEXT NOT NULL,
             $columnModificationTime TEXT NOT NULL,
             $columnUpdationTime TEXT,
-            $columnEncryptedKey TEXT,
-            $columnEncryptedKeyIV TEXT
+            $columnEncryptedPassword TEXT,
+            $columnEncryptedPasswordIV TEXT
           )
           ''');
   }
@@ -188,7 +188,7 @@ class FilesDB {
       table,
       where: '''$columnLocalID=? AND ($columnTitle=? OR $columnTitle=?) AND 
           $columnDeviceFolder=? AND $columnCreationTime=? AND 
-          $columnModificationTime=? AND $columnEncryptedKey AND $columnEncryptedKeyIV''',
+          $columnModificationTime=? AND $columnEncryptedPassword AND $columnEncryptedPasswordIV''',
       whereArgs: [
         localID,
         title,
@@ -232,8 +232,8 @@ class FilesDB {
     final values = new Map<String, dynamic>();
     values[columnUploadedFileID] = uploadedID;
     values[columnUpdationTime] = updationTime;
-    values[columnEncryptedKey] = encryptedKey;
-    values[columnEncryptedKeyIV] = iv;
+    values[columnEncryptedPassword] = encryptedKey;
+    values[columnEncryptedPasswordIV] = iv;
     return await db.update(
       table,
       values,
@@ -385,8 +385,8 @@ class FilesDB {
     row[columnCreationTime] = file.creationTime;
     row[columnModificationTime] = file.modificationTime;
     row[columnUpdationTime] = file.updationTime;
-    row[columnEncryptedKey] = file.encryptedKey;
-    row[columnEncryptedKeyIV] = file.encryptedKeyIV;
+    row[columnEncryptedPassword] = file.encryptedPassword;
+    row[columnEncryptedPasswordIV] = file.encryptedPasswordIV;
     return row;
   }
 
@@ -409,8 +409,8 @@ class FilesDB {
     file.updationTime = row[columnUpdationTime] == null
         ? -1
         : int.parse(row[columnUpdationTime]);
-    file.encryptedKey = row[columnEncryptedKey];
-    file.encryptedKeyIV = row[columnEncryptedKeyIV];
+    file.encryptedPassword = row[columnEncryptedPassword];
+    file.encryptedPasswordIV = row[columnEncryptedPasswordIV];
     return file;
   }
 }

+ 2 - 2
lib/face_search_manager.dart

@@ -60,8 +60,8 @@ class FaceSearchManager {
             file.deviceFolder,
             file.creationTime,
             file.modificationTime,
-            file.encryptedKey,
-            file.encryptedKeyIV,
+            file.encryptedPassword,
+            file.encryptedPasswordIV,
             alternateTitle: getHEICFileNameForJPG(file)));
       } catch (e) {
         // Not available locally

+ 4 - 7
lib/file_downloader.dart

@@ -34,15 +34,12 @@ class DiffFetcher {
               file.ownerID = fileItem["ownerID"];
               file.updationTime = fileItem["updationTime"];
               file.isEncrypted = true;
-              file.encryptedKey = fileItem["encryptedKey"];
-              file.encryptedKeyIV = fileItem["encryptedKeyIV"];
-              final key = CryptoUtil.aesDecrypt(
-                  base64.decode(file.encryptedKey),
-                  Configuration.instance.getKey(),
-                  base64.decode(file.encryptedKeyIV));
+              file.encryptedPassword = fileItem["encryptedPassword"];
+              file.encryptedPasswordIV = fileItem["encryptedPasswordIV"];
               Map<String, dynamic> metadata = jsonDecode(utf8.decode(
                   await CryptoUtil.decryptDataToData(
-                      fileItem["metadata"], key)));
+                      base64.decode(fileItem["encryptedMetadata"]),
+                      file.getPassword())));
               file.applyMetadata(metadata);
               files.add(file);
             }

+ 11 - 12
lib/file_uploader.dart

@@ -41,19 +41,18 @@ class FileUploader {
   }
 
   Future<File> encryptAndUploadFile(File file) async {
-    final key = CryptoUtil.getSecureRandomBytes(length: 32);
-    final base64EncodedKey = base64.encode(key);
+    final password = CryptoUtil.getSecureRandomString(length: 32);
     final iv = CryptoUtil.getSecureRandomBytes(length: 16);
     final base64EncodedIV = base64.encode(iv);
-    final encryptedKey =
-        CryptoUtil.aesEncrypt(key, Configuration.instance.getKey(), iv);
+    final encryptedKey = CryptoUtil.aesEncrypt(
+        utf8.encode(password), Configuration.instance.getKey(), iv);
     final base64EncodedEncryptedKey = base64.encode(encryptedKey);
 
     final encryptedFileName = file.generatedID.toString() + ".aes";
     final tempDirectory = Configuration.instance.getTempDirectory();
     final encryptedFilePath = tempDirectory + encryptedFileName;
     await CryptoUtil.encryptDataToFile(
-        await getBytesFromDisk(file), encryptedFilePath, key);
+        await getBytesFromDisk(file), encryptedFilePath, password);
 
     final fileUploadURL = await getUploadURL();
     String fileObjectKey =
@@ -65,7 +64,7 @@ class FileUploader {
         file.generatedID.toString() + "_thumbnail.aes";
     final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName;
     await CryptoUtil.encryptDataToFile(
-        thumbnailData, encryptedThumbnailPath, key);
+        thumbnailData, encryptedThumbnailPath, password);
 
     final thumbnailUploadURL = await getUploadURL();
     String thumbnailObjectKey =
@@ -73,13 +72,13 @@ class FileUploader {
 
     final metadata = jsonEncode(file.getMetadata());
     final encryptedMetadata =
-        await CryptoUtil.encryptDataToData(utf8.encode(metadata), key);
+        await CryptoUtil.encryptDataToData(utf8.encode(metadata), password);
     final data = {
       "fileObjectKey": fileObjectKey,
       "thumbnailObjectKey": thumbnailObjectKey,
-      "metadata": encryptedMetadata,
-      "encryptedKey": base64EncodedEncryptedKey,
-      "encryptedKeyIV": base64EncodedIV,
+      "encryptedMetadata": base64.encode(encryptedMetadata),
+      "encryptedPassword": base64EncodedEncryptedKey,
+      "encryptedPasswordIV": base64EncodedIV,
     };
     return _dio
         .post(
@@ -95,8 +94,8 @@ class FileUploader {
       file.uploadedFileID = data["id"];
       file.updationTime = data["updationTime"];
       file.ownerID = data["ownerID"];
-      file.encryptedKey = base64EncodedEncryptedKey;
-      file.encryptedKeyIV = base64EncodedIV;
+      file.encryptedPassword = base64EncodedEncryptedKey;
+      file.encryptedPasswordIV = base64EncodedIV;
       return file;
     });
   }

+ 1 - 1
lib/folder_service.dart

@@ -71,7 +71,7 @@ class FolderSharingService {
         var existingPhoto =
             await FilesDB.instance.getMatchingRemoteFile(file.uploadedFileID);
         await FilesDB.instance.update(existingPhoto.generatedID,
-            file.uploadedFileID, file.updationTime, file.encryptedKey, file.encryptedKeyIV);
+            file.uploadedFileID, file.updationTime, file.encryptedPassword, file.encryptedPasswordIV);
       } catch (e) {
         await FilesDB.instance.insert(file);
       }

+ 8 - 9
lib/models/file.dart

@@ -1,5 +1,4 @@
 import 'dart:convert';
-import 'dart:typed_data';
 
 import 'package:photo_manager/photo_manager.dart';
 import 'package:path/path.dart';
@@ -22,8 +21,8 @@ class File {
   int updationTime;
   Location location;
   FileType fileType;
-  String encryptedKey;
-  String encryptedKeyIV;
+  String encryptedPassword;
+  String encryptedPasswordIV;
 
   File();
 
@@ -37,8 +36,8 @@ class File {
     creationTime = json["creationTime"];
     modificationTime = json["modificationTime"];
     updationTime = json["updationTime"];
-    encryptedKey = json["encryptedKey"];
-    encryptedKeyIV = json["encryptedKeyIV"];
+    encryptedPassword = json["encryptedPassword"];
+    encryptedPasswordIV = json["encryptedPasswordIV"];
   }
 
   static Future<File> fromAsset(
@@ -138,12 +137,12 @@ class File {
         Configuration.instance.getToken();
   }
 
-  Uint8List getKey() {
-    if (encryptedKey == null) {
+  String getPassword() {
+    if (encryptedPassword == null) {
       return null;
     }
-    return CryptoUtil.aesDecrypt(base64.decode(encryptedKey),
-        Configuration.instance.getKey(), base64.decode(encryptedKeyIV));
+    return utf8.decode(CryptoUtil.aesDecrypt(base64.decode(encryptedPassword),
+        Configuration.instance.getKey(), base64.decode(encryptedPasswordIV)));
   }
 
   @override

+ 4 - 4
lib/photo_sync_manager.dart

@@ -210,7 +210,7 @@ class PhotoSyncManager {
           uploadedFile = await _uploader.uploadFile(file);
         }
         await _db.update(file.generatedID, uploadedFile.uploadedFileID,
-            uploadedFile.updationTime, file.encryptedKey, file.encryptedKeyIV);
+            uploadedFile.updationTime, file.encryptedPassword, file.encryptedPasswordIV);
         _prefs.setInt(_syncTimeKey, uploadedFile.updationTime);
         Bus.instance.fire(PhotoUploadEvent(
             completed: i + 1, total: photosToBeUploaded.length));
@@ -230,11 +230,11 @@ class PhotoSyncManager {
             file.deviceFolder,
             file.creationTime,
             file.modificationTime,
-            file.encryptedKey,
-            file.encryptedKeyIV,
+            file.encryptedPassword,
+            file.encryptedPasswordIV,
             alternateTitle: getHEICFileNameForJPG(file));
         await _db.update(existingPhoto.generatedID, file.uploadedFileID,
-            file.updationTime, file.encryptedKey, file.encryptedKeyIV);
+            file.updationTime, file.encryptedPassword, file.encryptedPasswordIV);
       } catch (e) {
         file.localID = null; // File uploaded from a different device
         await _db.insert(file);

+ 26 - 21
lib/utils/crypto_util.dart

@@ -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;

+ 2 - 2
lib/utils/file_util.dart

@@ -150,7 +150,7 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
   )
       .then((_) async {
     final data =
-        await CryptoUtil.decryptFileToData(temporaryPath, file.getKey());
+        await CryptoUtil.decryptFileToData(temporaryPath, file.getPassword());
     io.File(temporaryPath).deleteSync();
     return cacheManager.putFile(file.getDownloadUrl(), data);
   });
@@ -163,7 +163,7 @@ Future<io.File> _downloadAndDecryptThumbnail(File file) async {
   Dio dio = Dio();
   return dio.download(file.getThumbnailUrl(), temporaryPath).then((_) async {
     final data =
-        await CryptoUtil.decryptFileToData(temporaryPath, file.getKey());
+        await CryptoUtil.decryptFileToData(temporaryPath, file.getPassword());
     io.File(temporaryPath).deleteSync();
     return ThumbnailCacheManager().putFile(file.getThumbnailUrl(), data);
   });