Skip to content

Commit 094d029

Browse files
authored
Add a convenience method for encrypting a stream (#207)
feat(crypto-module): add a convenience `encryptStream(from:)` method in CryptoModule
1 parent 56b8f5e commit 094d029

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

.pubnub.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---
22
name: swift
33
scm: github.com/pubnub/swift
4-
version: "9.1.0"
4+
version: "9.2.0"
55
schema: 1
66
changelog:
7+
- date: 2025-05-15
8+
version: 9.2.0
9+
changes:
10+
- type: feature
11+
text: "The `encryptStream(from:)` convenience method is designed to encrypt a local file. It eliminates the need for the caller to provide an `InputStream` and its content length - these are handled internally. You only need to pass a local file URL."
712
- date: 2025-05-13
813
version: 9.1.0
914
changes:
@@ -683,7 +688,7 @@ sdks:
683688
- distribution-type: source
684689
distribution-repository: GitHub release
685690
package-name: PubNub
686-
location: https://github.com/pubnub/swift/archive/refs/tags/9.1.0.zip
691+
location: https://github.com/pubnub/swift/archive/refs/tags/9.2.0.zip
687692
supported-platforms:
688693
supported-operating-systems:
689694
macOS:

PubNub.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,7 +4011,7 @@
40114011
"@loader_path/Frameworks",
40124012
);
40134013
MACOSX_DEPLOYMENT_TARGET = 10.15;
4014-
MARKETING_VERSION = 9.1.0;
4014+
MARKETING_VERSION = 9.2.0;
40154015
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
40164016
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
40174017
MTL_FAST_MATH = YES;
@@ -4062,7 +4062,7 @@
40624062
"@loader_path/Frameworks",
40634063
);
40644064
MACOSX_DEPLOYMENT_TARGET = 10.15;
4065-
MARKETING_VERSION = 9.1.0;
4065+
MARKETING_VERSION = 9.2.0;
40664066
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
40674067
MTL_ENABLE_DEBUG_INFO = NO;
40684068
MTL_FAST_MATH = YES;
@@ -4170,7 +4170,7 @@
41704170
"@loader_path/Frameworks",
41714171
);
41724172
MACOSX_DEPLOYMENT_TARGET = 10.15;
4173-
MARKETING_VERSION = 9.1.0;
4173+
MARKETING_VERSION = 9.2.0;
41744174
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
41754175
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
41764176
MTL_FAST_MATH = YES;
@@ -4223,7 +4223,7 @@
42234223
"@loader_path/Frameworks",
42244224
);
42254225
MACOSX_DEPLOYMENT_TARGET = 10.15;
4226-
MARKETING_VERSION = 9.1.0;
4226+
MARKETING_VERSION = 9.2.0;
42274227
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
42284228
MTL_ENABLE_DEBUG_INFO = NO;
42294229
MTL_FAST_MATH = YES;
@@ -4344,7 +4344,7 @@
43444344
"@loader_path/Frameworks",
43454345
);
43464346
MACOSX_DEPLOYMENT_TARGET = 10.15;
4347-
MARKETING_VERSION = 9.1.0;
4347+
MARKETING_VERSION = 9.2.0;
43484348
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
43494349
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
43504350
MTL_FAST_MATH = YES;
@@ -4396,7 +4396,7 @@
43964396
"@loader_path/Frameworks",
43974397
);
43984398
MACOSX_DEPLOYMENT_TARGET = 10.15;
4399-
MARKETING_VERSION = 9.1.0;
4399+
MARKETING_VERSION = 9.2.0;
44004400
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
44014401
MTL_ENABLE_DEBUG_INFO = NO;
44024402
MTL_FAST_MATH = YES;
@@ -4876,7 +4876,7 @@
48764876
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
48774877
);
48784878
MACOSX_DEPLOYMENT_TARGET = 10.15;
4879-
MARKETING_VERSION = 9.1.0;
4879+
MARKETING_VERSION = 9.2.0;
48804880
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
48814881
OTHER_CFLAGS = "$(inherited)";
48824882
OTHER_LDFLAGS = "$(inherited)";
@@ -4919,7 +4919,7 @@
49194919
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
49204920
);
49214921
MACOSX_DEPLOYMENT_TARGET = 10.15;
4922-
MARKETING_VERSION = 9.1.0;
4922+
MARKETING_VERSION = 9.2.0;
49234923
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
49244924
OTHER_CFLAGS = "$(inherited)";
49254925
OTHER_LDFLAGS = "$(inherited)";

PubNubSwift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'PubNubSwift'
3-
s.version = '9.1.0'
3+
s.version = '9.2.0'
44
s.homepage = 'https://github.com/pubnub/swift'
55
s.documentation_url = 'https://www.pubnub.com/docs/swift-native/pubnub-swift-sdk'
66
s.authors = { 'PubNub, Inc.' => '[email protected]' }

Sources/PubNub/Helpers/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public enum Constant {
5757

5858
static let pubnubSwiftSDKName: String = "PubNubSwift"
5959

60-
static let pubnubSwiftSDKVersion: String = "9.1.0"
60+
static let pubnubSwiftSDKVersion: String = "9.2.0"
6161

6262
static let appBundleId: String = {
6363
if let info = Bundle.main.infoDictionary,

Sources/PubNub/Helpers/Crypto/CryptoModule.swift

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,45 @@ public struct CryptoModule {
207207
}
208208
}
209209

210+
/// Encrypts the given local file URL and returns the result as an `EncryptedStreamResult`
211+
///
212+
/// - Parameters:
213+
/// - from: The local file URL of the stream to encrypt
214+
/// - Returns:
215+
/// - **Success**: An `EncryptedStreamResult` containing the encrypted input stream and its total content length
216+
/// - **Failure**: `PubNubError` describing the reason of failure
217+
public func encryptStream(from localFileURL: URL) -> Result<EncryptedStreamResult, PubNubError> {
218+
guard let inputStream = InputStream(url: localFileURL) else {
219+
PubNub.log.debug(
220+
"Cannot create InputStream from \(localFileURL). Ensure that the file exists at the specified path",
221+
category: .crypto
222+
)
223+
return .failure(PubNubError(
224+
.fileMissingAtPath,
225+
additional: ["Cannot create InputStream from \(localFileURL)"]
226+
))
227+
}
228+
229+
let streamEncryptionResult = performStreamEncryption(
230+
stream: inputStream,
231+
contentLength: localFileURL.sizeOf
232+
)
233+
234+
switch streamEncryptionResult {
235+
case .success:
236+
PubNub.log.debug("File encrypted successfully")
237+
case let .failure(error):
238+
PubNub.log.debug("Encryption of file failed due to \(error)")
239+
}
240+
241+
return streamEncryptionResult.map {
242+
EncryptedStreamResult(
243+
stream: $0,
244+
contentLength: $0.length
245+
)
246+
}
247+
}
248+
210249
private func performStreamEncryption(stream: InputStream, contentLength: Int) -> Result<MultipartInputStream, PubNubError> {
211250
guard contentLength > 0 else {
212251
return .failure(PubNubError(
@@ -275,35 +314,35 @@ public struct CryptoModule {
275314
return streamDecryptionResult
276315
}
277316

278-
/// Decrypts the stream from the given URL and writes it to the output path
317+
/// Decrypts the stream from the given local file URL and writes it to the output path
279318
///
280319
/// - Parameters:
281-
/// - url: URL of the encrypted stream
282-
/// - outputPath: Path to write the decrypted stream
320+
/// - from: The local file URL of the encrypted stream
321+
/// - to: The path to write the decrypted stream
283322
/// - Returns:
284323
/// - **Success**: A decrypted `InputStream` object
285324
/// - **Failure**: `PubNubError` describing the reason of failure
286325
@discardableResult
287-
public func decryptStream(from url: URL, to outputPath: URL) -> Result<InputStream, PubNubError> {
326+
public func decryptStream(from localFileURL: URL, to outputPath: URL) -> Result<InputStream, PubNubError> {
288327
PubNub.log.debug(
289328
"Decrypting file",
290329
category: .crypto
291330
)
292331

293-
guard let inputStream = InputStream(url: url) else {
332+
guard let inputStream = InputStream(url: localFileURL) else {
294333
PubNub.log.debug(
295-
"Cannot create InputStream from \(url). Ensure that the file exists at the specified path",
334+
"Cannot create InputStream from \(localFileURL). Ensure that the file exists at the specified path",
296335
category: .crypto
297336
)
298337
return .failure(PubNubError(
299338
.decryptionFailure,
300-
additional: ["File doesn't exist at \(url) path"]
339+
additional: ["File doesn't exist at \(localFileURL) path"]
301340
))
302341
}
303342

304343
let streamDecryptionResult = performStreamDecryption(
305344
stream: inputStream,
306-
contentLength: url.sizeOf,
345+
contentLength: localFileURL.sizeOf,
307346
to: outputPath
308347
)
309348

@@ -317,8 +356,7 @@ public struct CryptoModule {
317356
return streamDecryptionResult
318357
}
319358

320-
@discardableResult
321-
public func performStreamDecryption(
359+
private func performStreamDecryption(
322360
stream: InputStream,
323361
contentLength: Int,
324362
to outputPath: URL

Sources/PubNub/Networking/HTTPFileTask.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,14 @@ public class HTTPFileDownloadTask: HTTPFileTask {
242242

243243
func decrypt(_ encryptedURL: URL, to outpuURL: URL, using cryptoModule: CryptoModule) throws {
244244
// If we were provided a Crypto object we should try and decrypt the file
245-
246245
guard let inputStream = InputStream(url: encryptedURL) else {
247246
throw PubNubError(.streamCouldNotBeInitialized, additional: [encryptedURL.absoluteString])
248247
}
249-
250-
cryptoModule.decrypt(
248+
_ = try cryptoModule.decrypt(
251249
stream: inputStream,
252250
contentLength: encryptedURL.sizeOf,
253251
to: outpuURL
254-
)
252+
).get()
255253
}
256254

257255
open func temporaryURL() -> URL {
@@ -314,12 +312,11 @@ public class HTTPFileDownloadTask: HTTPFileTask {
314312
guard let stream = InputStream(url: url) else {
315313
throw PubNubError(.streamCouldNotBeInitialized, additional: [url.absoluteString])
316314
}
317-
318-
cryptoModule.decrypt(
315+
_ = try cryptoModule.decrypt(
319316
stream: stream,
320317
contentLength: url.sizeOf,
321318
to: destinationURL
322-
)
319+
).get()
323320
} else {
324321
try fileManager.moveItem(at: url, to: destinationURL)
325322
}

0 commit comments

Comments
 (0)