From 83ba71f9d746bd172408cfa5e04b0eb8ef7e9ae8 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Fri, 29 Sep 2023 11:09:18 +0200 Subject: [PATCH] Prevent from appending CryptorHeader for LegacyCryptor used as an encoder --- .../PubNub/Helpers/Crypto/CryptorModule.swift | 17 +++++++++++------ .../Helpers/Crypto/Cryptors/LegacyCryptor.swift | 8 ++++---- .../Helpers/Crypto/Header/CryptorHeader.swift | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Sources/PubNub/Helpers/Crypto/CryptorModule.swift b/Sources/PubNub/Helpers/Crypto/CryptorModule.swift index e904c039..f8f713d0 100644 --- a/Sources/PubNub/Helpers/Crypto/CryptorModule.swift +++ b/Sources/PubNub/Helpers/Crypto/CryptorModule.swift @@ -53,10 +53,14 @@ public struct CryptorModule { public func encrypt(data: Data) -> Result { defaultCryptor.encrypt(data: data).map { - CryptorHeader.v1( - cryptorId: defaultCryptor.id, - data: $0.metadata - ).asData() + $0.data + if defaultCryptor.id == LegacyCryptor.ID { + return $0.data + } else { + return CryptorHeader.v1( + cryptorId: defaultCryptor.id, + data: $0.metadata + ).asData() + $0.data + } }.mapError { PubNubError(.encryptionError, underlying: $0) } @@ -99,10 +103,11 @@ public struct CryptorModule { stream: stream, contentLength: contentLength ).map { - let header = CryptorHeader.v1( + let header = defaultCryptor.id != LegacyCryptor.ID ? CryptorHeader.v1( cryptorId: defaultCryptor.id, data: $0.metadata - ) + ) : .none + let multipartInputStream = MultipartInputStream( inputStreams: [InputStream(data: header.asData()), $0.stream] ) diff --git a/Sources/PubNub/Helpers/Crypto/Cryptors/LegacyCryptor.swift b/Sources/PubNub/Helpers/Crypto/Cryptors/LegacyCryptor.swift index a40bc26e..6a621079 100644 --- a/Sources/PubNub/Helpers/Crypto/Cryptors/LegacyCryptor.swift +++ b/Sources/PubNub/Helpers/Crypto/Cryptors/LegacyCryptor.swift @@ -31,9 +31,9 @@ import CommonCrypto public struct LegacyCryptor: Cryptor { private let key: Data private let withRandomIV: Bool - - static let legacyCryptorId: CryptorId = [0x00, 0x00, 0x00, 0x00] - + + static let ID: CryptorId = [0x00, 0x00, 0x00, 0x00] + public init(key: String, withRandomIV: Bool = true) { let hash = CryptorUtils.SHA256.hash(from: key.data(using: .utf8) ?? Data()) let hexStrData = CryptorUtils.hexFrom(hash).lowercased(with: .current).data(using: .utf8) ?? Data() @@ -42,7 +42,7 @@ public struct LegacyCryptor: Cryptor { } public var id: CryptorId { - Self.legacyCryptorId + Self.ID } public func encrypt(data: Data) -> Result { diff --git a/Sources/PubNub/Helpers/Crypto/Header/CryptorHeader.swift b/Sources/PubNub/Helpers/Crypto/Header/CryptorHeader.swift index 5d499774..f2d3aab0 100644 --- a/Sources/PubNub/Helpers/Crypto/Header/CryptorHeader.swift +++ b/Sources/PubNub/Helpers/Crypto/Header/CryptorHeader.swift @@ -50,7 +50,7 @@ enum CryptorHeader: Equatable { func cryptorId() -> CryptorId { switch self { case .none: - return LegacyCryptor.legacyCryptorId + return LegacyCryptor.ID case .v1(let cryptorId, _): return cryptorId }