@@ -15,29 +15,31 @@ public class CryptorModule {
1515 public static func aesCbcCryptoModule( with key: String , withRandomIV: Bool = true ) -> CryptoModule {
1616 preconditionFailure ( " This method is no longer available " )
1717 }
18+
1819 public static func legacyCryptoModule( with key: String , withRandomIV: Bool = true ) -> CryptoModule {
1920 preconditionFailure ( " This method is no longer available " )
2021 }
2122}
2223
2324/// Object capable of encryption/decryption
2425public struct CryptoModule {
25- private let defaultCryptor : Cryptor
26- private let cryptors : [ Cryptor ]
26+ private let defaultCryptor : any Cryptor
27+ private let cryptors : [ any Cryptor ]
2728 private let legacyCryptorId : CryptorId = [ ]
2829
2930 typealias Base64EncodedString = String
3031
3132 /// Initializes `CryptoModule` with custom ``Cryptor`` objects capable of encryption and decryption
3233 ///
33- /// Use this constructor if you would like to provide **custom** objects for decryption and encryption and don't want to use PubNub's built-in `Cryptors`.
34- /// Otherwise, refer to convenience static factory methods such as ``aesCbcCryptoModule(with:withRandomIV:)``
35- /// and ``legacyCryptoModule(with:withRandomIV:)`` that return `CryptoModule` configured for you.
34+ /// Use this constructor if you would like to provide **custom** objects for decryption and encryption
35+ /// and don't want to use PubNub's built-in `Cryptors`. Otherwise, refer to convenience static factory methods
36+ /// such as ``aesCbcCryptoModule(with:withRandomIV:)``and ``legacyCryptoModule(with:withRandomIV:)``
37+ /// that return `CryptoModule` configured for you.
3638 ///
3739 /// - Parameters:
3840 /// - default: Primary ``Cryptor`` instance used for encryption and decryption
3941 /// - cryptors: An optional list of ``Cryptor`` instances which older messages/files were encoded
40- public init ( default cryptor: Cryptor , cryptors: [ Cryptor ] = [ ] ) {
42+ public init ( default cryptor: any Cryptor , cryptors: [ any Cryptor ] = [ ] ) {
4143 self . defaultCryptor = cryptor
4244 self . cryptors = cryptors
4345 }
@@ -46,13 +48,15 @@ public struct CryptoModule {
4648 ///
4749 /// - Parameters:
4850 /// - data: Data to encrypt
49- /// - Returns: A success, storing encrypted `Data` if operation succeeds. Otherwise, a failure storing `PubNubError` is returned
51+ /// - Returns:
52+ /// - **Success**: An encrypted `Data` object
53+ /// - **Failure**: `PubNubError` describing the reason of failure
5054 public func encrypt( data: Data ) -> Result < Data , PubNubError > {
5155 guard !data. isEmpty else {
5256 return . failure( PubNubError (
5357 . encryptionFailure,
54- additional: [ " Cannot encrypt empty Data " ] )
55- )
58+ additional: [ " Cannot encrypt empty Data " ]
59+ ) )
5660 }
5761 return defaultCryptor. encrypt ( data: data) . map {
5862 if defaultCryptor. id == LegacyCryptor . ID {
@@ -71,13 +75,15 @@ public struct CryptoModule {
7175 ///
7276 /// - Parameters:
7377 /// - data: Data to decrypt
74- /// - Returns: A success, storing decrypted `Data` if operation succeeds. Otherwise, a failure storing `PubNubError` is returned
78+ /// - Returns:
79+ /// - **Success**: A decrypted `Data` object
80+ /// - **Failure**: `PubNubError` describing the reason of failure
7581 public func decrypt( data: Data ) -> Result < Data , PubNubError > {
7682 guard !data. isEmpty else {
7783 return . failure( PubNubError (
7884 . decryptionFailure,
79- additional: [ " Cannot decrypt empty Data in \( String ( describing: self ) ) " ] )
80- )
85+ additional: [ " Cannot decrypt empty Data in \( String ( describing: self ) ) " ]
86+ ) )
8187 }
8288 do {
8389 let header = try CryptorHeader . from ( data: data)
@@ -87,7 +93,7 @@ public struct CryptoModule {
8793 . unknownCryptorFailure,
8894 additional: [
8995 " Could not find matching Cryptor for \( header. cryptorId ( ) ) while decrypting Data. " +
90- " Ensure the corresponding instance is registered in \( String ( describing: Self . self) ) "
96+ " Ensure the corresponding instance is registered in \( String ( describing: Self . self) ) "
9197 ]
9298 ) )
9399 }
@@ -115,8 +121,8 @@ public struct CryptoModule {
115121 if $0. isEmpty {
116122 return . failure( PubNubError (
117123 . decryptionFailure,
118- additional: [ " Decrypting resulted with empty Data " ] )
119- )
124+ additional: [ " Decrypting resulted with empty Data " ]
125+ ) )
120126 }
121127 return . success( $0)
122128 }
@@ -129,8 +135,8 @@ public struct CryptoModule {
129135 return . failure( PubNubError (
130136 . decryptionFailure,
131137 underlying: error,
132- additional: [ " Cannot decrypt InputStream " ] )
133- )
138+ additional: [ " Cannot decrypt InputStream " ]
139+ ) )
134140 }
135141 }
136142
@@ -139,7 +145,9 @@ public struct CryptoModule {
139145 /// - Parameters:
140146 /// - stream: Stream to encrypt
141147 /// - contentLength: Content length of encoded stream
142- /// - Returns: A success, storing an `InputStream` value if operation succeeds. Otherwise, a failure storing `PubNubError` is returned
148+ /// - Returns:
149+ /// - **Success**: An `InputStream` value
150+ /// - **Failure**: `PubNubError` describing the reason of failure
143151 public func encrypt( stream: InputStream , contentLength: Int ) -> Result < InputStream , PubNubError > {
144152 guard contentLength > 0 else {
145153 return . failure( PubNubError (
@@ -179,7 +187,9 @@ public struct CryptoModule {
179187 /// - stream: Stream to decrypt
180188 /// - contentLength: Content length of encrypted stream
181189 /// - to: URL where the stream should be decrypted to
182- /// - Returns: A success, storing a decrypted `InputStream` value if operation succeeds. Otherwise, a failure storing `PubNubError` is returned
190+ /// - Returns:
191+ /// - **Success**: A decrypted `InputStream` object
192+ /// - **Failure**: `PubNubError` describing the reason of failure
183193 @discardableResult
184194 public func decrypt(
185195 stream: InputStream ,
@@ -203,7 +213,7 @@ public struct CryptoModule {
203213 . unknownCryptorFailure,
204214 additional: [
205215 " Could not find matching Cryptor for \( readHeaderResp. header. cryptorId ( ) ) while decrypting InputStream. " +
206- " Ensure the corresponding instance is registered in \( String ( describing: Self . self) ) "
216+ " Ensure the corresponding instance is registered in \( String ( describing: Self . self) ) "
207217 ]
208218 ) )
209219 }
@@ -218,8 +228,8 @@ public struct CryptoModule {
218228 if outputPath. sizeOf == 0 {
219229 return . failure( PubNubError (
220230 . decryptionFailure,
221- additional: [ " Decrypting resulted with an empty File " ] )
222- )
231+ additional: [ " Decrypting resulted with an empty File " ]
232+ ) )
223233 }
224234 return . success( $0)
225235 }
@@ -237,7 +247,7 @@ public struct CryptoModule {
237247 }
238248 }
239249
240- private func cryptor( matching header: CryptorHeader ) -> Cryptor ? {
250+ private func cryptor( matching header: CryptorHeader ) -> ( any Cryptor ) ? {
241251 header. cryptorId ( ) == defaultCryptor. id ? defaultCryptor : cryptors. first ( where: {
242252 $0. id == header. cryptorId ( )
243253 } )
@@ -246,7 +256,6 @@ public struct CryptoModule {
246256
247257/// Convenience methods for creating `CryptoModule`
248258public extension CryptoModule {
249-
250259 /// Returns **recommended** `CryptoModule` for encryption/decryption
251260 ///
252261 /// - Parameters:
@@ -279,7 +288,9 @@ extension CryptoModule: Equatable {
279288
280289extension CryptoModule : Hashable {
281290 public func hash( into hasher: inout Hasher ) {
282- hasher. combine ( cryptors. map { $0. id } )
291+ for cryptor in cryptors {
292+ hasher. combine ( cryptor)
293+ }
283294 }
284295}
285296
@@ -289,7 +300,7 @@ extension CryptoModule: CustomStringConvertible {
289300 }
290301}
291302
292- internal extension CryptoModule {
303+ extension CryptoModule {
293304 func encrypt( string: String ) -> Result < Base64EncodedString , PubNubError > {
294305 guard let data = string. data ( using: . utf8) else {
295306 return . failure( PubNubError (
@@ -309,8 +320,8 @@ internal extension CryptoModule {
309320 } else {
310321 return . failure( PubNubError (
311322 . decryptionFailure,
312- additional: [ " Cannot create String from provided Data " ] )
313- )
323+ additional: [ " Cannot create String from provided Data " ]
324+ ) )
314325 }
315326 }
316327 }
0 commit comments