π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify multipart checksum state
Neeraj Guptaac6b96e03b0e
diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dartindex d0fce4fb58..3b747b353a 100644--- a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart@@ -137,12 +137,12 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(final logger = Logger("ChaChaEncryptWithMD5");final sourceFile = File(args["sourceFilePath"]);final destinationFile = File(args["destinationFilePath"]);- final int? multiPartChunkSizeInBytes = args["multiPartChunkSizeInBytes"];+ final int? partSize = args["multiPartChunkSizeInBytes"];final sourceFileLength = await sourceFile.length();logger.info("Encrypting file of size $sourceFileLength");- if (multiPartChunkSizeInBytes != null) {- logger.info("Using multipart chunk size: $multiPartChunkSizeInBytes bytes");+ if (partSize != null) {+ logger.info("Using multipart chunk size: $partSize bytes");}final inputFile = sourceFile.openSync(mode: FileMode.read);@@ -158,22 +158,17 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(key,);- AccumulatorSink<Digest>? fullAccumulator;- ChunkedConversionSink<List<int>>? fullMd5Sink;- AccumulatorSink<Digest>? partAccumulator;- ChunkedConversionSink<List<int>>? partMd5Sink;+ var md5Accumulator = AccumulatorSink<Digest>();+ var md5Sink = md5.startChunkedConversion(md5Accumulator);++ String finishMd5() {+ md5Sink.close();+ return base64.encode(md5Accumulator.events.single.bytes);+ }final List<String> partMd5s = [];var bytesInPart = 0;- if (multiPartChunkSizeInBytes == null) {- fullAccumulator = AccumulatorSink<Digest>();- fullMd5Sink = md5.startChunkedConversion(fullAccumulator);- } else {- partAccumulator = AccumulatorSink<Digest>();- partMd5Sink = md5.startChunkedConversion(partAccumulator);- }-var bytesRead = 0;var totalEncryptedBytes = 0;var tag = Sodium.cryptoSecretstreamXchacha20poly1305TagMessage;@@ -235,19 +230,18 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(}totalEncryptedBytes += encryptedData.length;+ outSink.add(encryptedData);- if (multiPartChunkSizeInBytes == null) {- fullMd5Sink!.add(encryptedData);- outSink.add(encryptedData);+ if (partSize == null) {+ md5Sink.add(encryptedData);} else {- outSink.add(encryptedData);var offset = 0;while (offset < encryptedData.length) {- final bytesToPartBoundary = multiPartChunkSizeInBytes - bytesInPart;+ final bytesToPartBoundary = partSize - bytesInPart;final end = offset + bytesToPartBoundary < encryptedData.length? offset + bytesToPartBoundary: encryptedData.length;- partMd5Sink!.add(+ md5Sink.add(offset == 0 && end == encryptedData.length? encryptedData: Uint8List.sublistView(encryptedData, offset, end),@@ -255,29 +249,23 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(bytesInPart += end - offset;offset = end;- if (bytesInPart == multiPartChunkSizeInBytes) {- partMd5Sink.close();- final digest = partAccumulator!.events.single;- partMd5s.add(base64.encode(digest.bytes));+ if (bytesInPart == partSize) {+ partMd5s.add(finishMd5());- logger.info(- "Part ${partMd5s.length}: $multiPartChunkSizeInBytes bytes",- );+ logger.info("Part ${partMd5s.length}: $partSize bytes");bytesInPart = 0;if (offset < encryptedData.length ||tag != Sodium.cryptoSecretstreamXchacha20poly1305TagFinal) {- partAccumulator = AccumulatorSink<Digest>();- partMd5Sink = md5.startChunkedConversion(partAccumulator);+ md5Accumulator = AccumulatorSink<Digest>();+ md5Sink = md5.startChunkedConversion(md5Accumulator);}}}if (tag == Sodium.cryptoSecretstreamXchacha20poly1305TagFinal &&bytesInPart > 0) {- partMd5Sink!.close();- final digest = partAccumulator!.events.single;- partMd5s.add(base64.encode(digest.bytes));+ partMd5s.add(finishMd5());}}}@@ -285,10 +273,8 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(await inputFile.close();String? finalFileMd5;- if (multiPartChunkSizeInBytes == null && fullMd5Sink != null) {- fullMd5Sink.close();- final digest = fullAccumulator!.events.single;- finalFileMd5 = base64.encode(digest.bytes);+ if (partSize == null) {+ finalFileMd5 = finishMd5();logger.info("File MD5: $finalFileMd5");}@@ -299,9 +285,7 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification((DateTime.now().millisecondsSinceEpoch -encryptionStartTime.millisecondsSinceEpoch) /1000;- final partsInfo = multiPartChunkSizeInBytes != null- ? " Parts: ${partMd5s.length}"- : "";+ final partsInfo = partSize != null ? " Parts: ${partMd5s.length}" : "";debugPrint("FileEncryption: Time: ${encryptionTimeSeconds}s ""Total encrypted: $totalEncryptedBytes bytes$partsInfo",@@ -311,10 +295,8 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(key: key,header: initPushResult.header,fileMd5: finalFileMd5,- partMd5s: multiPartChunkSizeInBytes != null && partMd5s.isNotEmpty- ? partMd5s- : null,- partSize: multiPartChunkSizeInBytes,+ partMd5s: partSize == null ? null : partMd5s,+ partSize: partSize,);} catch (e) {try {