Skip to content

Commit 1b73a19

Browse files
committed
Fixed progress reporting during file content decryption
1 parent afe6353 commit 1b73a19

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CryptoLib/Cryptor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ public class Cryptor {
231231
}
232232
let ciphertextChunk = try encryptSingleChunk(cleartextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
233233
ciphertextStream.write(ciphertextChunk, maxLength: ciphertextChunk.count)
234-
chunkNumber += 1
235234
progress.completedUnitCount += Int64(ciphertextChunk.count)
235+
chunkNumber += 1
236236
}
237237
}
238238

@@ -272,7 +272,7 @@ public class Cryptor {
272272
internal func decryptContent(from ciphertextStream: InputStream, to cleartextStream: OutputStream, ciphertextSize: Int?) throws {
273273
// create progress:
274274
let progress: Progress
275-
if let ciphertextSize = ciphertextSize, let cleartextSize = try? calculateCleartextSize(ciphertextSize) {
275+
if let ciphertextSize = ciphertextSize, let cleartextSize = try? calculateCleartextSize(ciphertextSize - Cryptor.fileHeaderSize) {
276276
progress = Progress(totalUnitCount: Int64(cleartextSize))
277277
} else {
278278
progress = Progress(totalUnitCount: -1)
@@ -292,8 +292,8 @@ public class Cryptor {
292292
}
293293
let cleartextChunk = try decryptSingleChunk(ciphertextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
294294
cleartextStream.write(cleartextChunk, maxLength: cleartextChunk.count)
295-
chunkNumber += 1
296295
progress.completedUnitCount += Int64(cleartextChunk.count)
296+
chunkNumber += 1
297297
}
298298
}
299299

CryptoLibTests/CryptorTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,18 @@ class CryptorTests: XCTestCase {
9999

100100
let ciphertextURL = tmpDirURL.appendingPathComponent(UUID().uuidString, isDirectory: false)
101101
let cleartextURL = tmpDirURL.appendingPathComponent(UUID().uuidString, isDirectory: false)
102+
let overallProgress = Progress(totalUnitCount: 2)
103+
let progressObserver = overallProgress.observe(\.fractionCompleted) { progress, _ in
104+
print("\(progress.localizedDescription ?? "") (\(progress.localizedAdditionalDescription ?? ""))")
105+
}
106+
overallProgress.becomeCurrent(withPendingUnitCount: 1)
102107
try cryptor.encryptContent(from: originalURL, to: ciphertextURL)
108+
overallProgress.resignCurrent()
109+
overallProgress.becomeCurrent(withPendingUnitCount: 1)
103110
try cryptor.decryptContent(from: ciphertextURL, to: cleartextURL)
111+
overallProgress.resignCurrent()
112+
progressObserver.invalidate()
113+
XCTAssertTrue(overallProgress.completedUnitCount >= overallProgress.totalUnitCount)
104114

105115
let cleartextData = try Data(contentsOf: cleartextURL)
106116
XCTAssertEqual(originalData, cleartextData)

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ SPEC CHECKSUMS:
1717

1818
PODFILE CHECKSUM: 026266d3146c90650716bef1229c141bb12d87b4
1919

20-
COCOAPODS: 1.9.1
20+
COCOAPODS: 1.9.3

0 commit comments

Comments
 (0)