Skip to content

Commit

Permalink
Method for decrypting an InputStream from CryptorModule now takes one…
Browse files Browse the repository at this point in the history
… less parameter
  • Loading branch information
jguz-pubnub committed Sep 28, 2023
1 parent b57f156 commit 7726ea6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Sources/PubNub/Errors/ErrorDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
12 changes: 8 additions & 4 deletions Sources/PubNub/Helpers/Crypto/CryptorModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -112,12 +117,11 @@ public struct CryptorModule {

@discardableResult
public func decrypt(
stream: InputStream,
contentLength: Int,
stream streamData: EncryptedStreamResult,
to outputPath: URL
) -> Result<InputStream, PubNubError> {
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 {
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions Sources/PubNub/Networking/HTTPFileTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ?? ""
Expand Down
6 changes: 2 additions & 4 deletions Tests/PubNubTests/Helpers/CryptoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 7726ea6

Please sign in to comment.