From 7726ea6e60d764e2ccc5f1cca0f2fb2c785dc94c Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Thu, 28 Sep 2023 11:14:09 +0200 Subject: [PATCH] Method for decrypting an InputStream from CryptorModule now takes one less parameter --- Sources/PubNub/Errors/ErrorDescription.swift | 4 ++-- Sources/PubNub/Helpers/Crypto/CryptorModule.swift | 12 ++++++++---- Sources/PubNub/Networking/HTTPFileTask.swift | 10 ++++------ .../Crypto/PubNubCryptoModuleContractTestSteps.swift | 12 +++++++++--- Tests/PubNubTests/Helpers/CryptoTests.swift | 6 ++---- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Sources/PubNub/Errors/ErrorDescription.swift b/Sources/PubNub/Errors/ErrorDescription.swift index 34d51f4f..c5e0e669 100644 --- a/Sources/PubNub/Errors/ErrorDescription.swift +++ b/Sources/PubNub/Errors/ErrorDescription.swift @@ -295,9 +295,9 @@ extension PubNubError.Reason: CustomStringConvertible, LocalizedError { case .serviceNotEnabled: return "The PubNub Service that you're attempting to use has not be enabled for your keyset." case .encryptionError: - return "Failure performing an encryption operation" + return "Failure to perform encryption" case .decryptionError: - return "Failure performing a decryption operation" + return "Failure to perform decryption" case .unknownCryptorError: return "Unknown Cryptor error" } diff --git a/Sources/PubNub/Helpers/Crypto/CryptorModule.swift b/Sources/PubNub/Helpers/Crypto/CryptorModule.swift index 06eec301..e904c039 100644 --- a/Sources/PubNub/Helpers/Crypto/CryptorModule.swift +++ b/Sources/PubNub/Helpers/Crypto/CryptorModule.swift @@ -30,6 +30,11 @@ import Foundation public struct EncryptedStreamResult { public let stream: InputStream public let contentLength: Int + + public init(stream: InputStream, contentLength: Int) { + self.stream = stream + self.contentLength = contentLength + } } public struct CryptorModule { @@ -112,12 +117,11 @@ public struct CryptorModule { @discardableResult public func decrypt( - stream: InputStream, - contentLength: Int, + stream streamData: EncryptedStreamResult, to outputPath: URL ) -> Result { do { - let finder = CryptorHeaderWithinStreamFinder(stream: stream) + let finder = CryptorHeaderWithinStreamFinder(stream: streamData.stream) let readHeaderResponse = try finder.findHeader() guard let cryptor = cryptor(matching: readHeaderResponse.header) else { @@ -132,7 +136,7 @@ public struct CryptorModule { return cryptor.decrypt( data: EncryptedStreamData( stream: readHeaderResponse.continuationStream, - contentLength: contentLength - readHeaderResponse.header.length(), + contentLength: streamData.contentLength - readHeaderResponse.header.length(), metadata: readHeaderResponse.header.metadataIfAny() ), outputPath: outputPath diff --git a/Sources/PubNub/Networking/HTTPFileTask.swift b/Sources/PubNub/Networking/HTTPFileTask.swift index 2f2caece..40359d56 100644 --- a/Sources/PubNub/Networking/HTTPFileTask.swift +++ b/Sources/PubNub/Networking/HTTPFileTask.swift @@ -264,9 +264,8 @@ public class HTTPFileDownloadTask: HTTPFileTask { throw PubNubError(.streamCouldNotBeInitialized, additional: [encryptedURL.absoluteString]) } - _ = cryptorModule.decrypt( - stream: inputStream, - contentLength: encryptedURL.sizeOf, + cryptorModule.decrypt( + stream: EncryptedStreamResult(stream: inputStream, contentLength: encryptedURL.sizeOf), to: outpuURL ) } @@ -332,9 +331,8 @@ public class HTTPFileDownloadTask: HTTPFileTask { throw PubNubError(.streamCouldNotBeInitialized, additional: [url.absoluteString]) } - _ = cryptorModule.decrypt( - stream: stream, - contentLength: url.sizeOf, + cryptorModule.decrypt( + stream: EncryptedStreamResult(stream: stream, contentLength: url.sizeOf), to: destinationURL ) } else { diff --git a/Tests/PubNubContractTest/Steps/Crypto/PubNubCryptoModuleContractTestSteps.swift b/Tests/PubNubContractTest/Steps/Crypto/PubNubCryptoModuleContractTestSteps.swift index a4807eb5..bf6b8bd7 100644 --- a/Tests/PubNubContractTest/Steps/Crypto/PubNubCryptoModuleContractTestSteps.swift +++ b/Tests/PubNubContractTest/Steps/Crypto/PubNubCryptoModuleContractTestSteps.swift @@ -64,7 +64,8 @@ public class PubNubCryptoModuleContractTestSteps: PubNubContractTestCase { let outputUrl = self.generateTestOutputUrl() let cryptorModule = self.createCryptorModule(cryptorKind, key: cipherKey, withRandomIV: withRandomIV) - let decryptingRes = cryptorModule.decrypt(stream: inputStream, contentLength: localUrl.sizeOf, to: outputUrl) + let encryptedStreamData = EncryptedStreamResult(stream: inputStream, contentLength: localUrl.sizeOf) + let decryptingRes = cryptorModule.decrypt(stream: encryptedStreamData, to: outputUrl) Then("I receive '(.*)'") { thenArgs, _ in switch thenArgs?.first ?? "" { @@ -100,7 +101,8 @@ public class PubNubCryptoModuleContractTestSteps: PubNubContractTestCase { let outputURL = self.generateTestOutputUrl() Then("Successfully decrypt an encrypted file with legacy code") { _, _ in - cryptorModule.decrypt(stream: res.stream, contentLength: res.contentLength, to: outputURL) + cryptorModule.decrypt(stream: res, to: outputURL) + let expectedData = try! Data(contentsOf: localFileUrl) let receivedData = try! Data(contentsOf: outputURL) XCTAssertEqual(expectedData, receivedData) @@ -129,7 +131,11 @@ public class PubNubCryptoModuleContractTestSteps: PubNubContractTestCase { let localFileUrl = self.localUrl(for: fileName) let stream = InputStream(url: localFileUrl)! let outputUrl = self.generateTestOutputUrl() - cryptorModule.decrypt(stream: stream, contentLength: localFileUrl.sizeOf, to: outputUrl) + + cryptorModule.decrypt( + stream: EncryptedStreamResult(stream: stream, contentLength: localFileUrl.sizeOf), + to: outputUrl + ) Then("Decrypted file content equal to the '(.*)' file content") { thenArgs, _ in let expectedFileName = thenArgs?.first ?? "" diff --git a/Tests/PubNubTests/Helpers/CryptoTests.swift b/Tests/PubNubTests/Helpers/CryptoTests.swift index 83af5c3f..50a8bfbb 100644 --- a/Tests/PubNubTests/Helpers/CryptoTests.swift +++ b/Tests/PubNubTests/Helpers/CryptoTests.swift @@ -172,8 +172,7 @@ class CryptoTests: XCTestCase { try? FileManager.default.removeItem(at: outputPath) cryptorModule.decrypt( - stream: InputStream(data: ecrypted), - contentLength: ecrypted.count, + stream: EncryptedStreamResult(stream: InputStream(data: ecrypted), contentLength: ecrypted.count), to: outputPath ) @@ -214,8 +213,7 @@ class CryptoTests: XCTestCase { try? FileManager.default.removeItem(at: decryptedURL) cryptorModule.decrypt( - stream: encryptedStreamResult.stream, - contentLength: encryptedStreamResult.contentLength, + stream: encryptedStreamResult, to: decryptedURL )