From 70234bc6bb434ed94cf186832f833fc10c87d2f2 Mon Sep 17 00:00:00 2001 From: Sergey Lem Date: Mon, 25 Jun 2018 13:18:18 +0100 Subject: [PATCH] Add custom wrapper for SHA1 and custom modulemap for framework including module maps for CommonCrypto and zlib --- Sources/Starscream/Compression.swift | 2 +- Sources/Starscream/WebSocket.swift | 7 +-- .../modulemap/CommonCrypto/NSString+SHA1.h | 29 ++++++++++ .../modulemap/CommonCrypto/NSString+SHA1.m | 35 ++++++++++++ Sources/modulemap/Starscream.modulemap | 16 ++++++ Sources/modulemap/zlib/include.h | 1 + Starscream.podspec | 9 ++- Starscream.xcodeproj/project.pbxproj | 55 +++++++++++++------ zlib/include.h | 2 - zlib/module.modulemap | 1 - zlib/module.private.modulemap | 9 --- 11 files changed, 125 insertions(+), 41 deletions(-) create mode 100644 Sources/modulemap/CommonCrypto/NSString+SHA1.h create mode 100644 Sources/modulemap/CommonCrypto/NSString+SHA1.m create mode 100644 Sources/modulemap/Starscream.modulemap create mode 100644 Sources/modulemap/zlib/include.h delete mode 100644 zlib/include.h delete mode 100644 zlib/module.modulemap delete mode 100644 zlib/module.private.modulemap diff --git a/Sources/Starscream/Compression.swift b/Sources/Starscream/Compression.swift index f20ff75a..b6bf45ac 100644 --- a/Sources/Starscream/Compression.swift +++ b/Sources/Starscream/Compression.swift @@ -27,7 +27,7 @@ ////////////////////////////////////////////////////////////////////////////////////////////////// import Foundation -import SSCZLib +import Starscream.SSCZLib class Decompressor { private var strm = z_stream() diff --git a/Sources/Starscream/WebSocket.swift b/Sources/Starscream/WebSocket.swift index 788a34b2..124b7784 100644 --- a/Sources/Starscream/WebSocket.swift +++ b/Sources/Starscream/WebSocket.swift @@ -21,7 +21,7 @@ import Foundation import CoreFoundation -import SSCommonCrypto +import Starscream.SSCommonCrypto public let WebsocketDidConnectNotification = "WebsocketDidConnectNotification" public let WebsocketDidDisconnectNotification = "WebsocketDidDisconnectNotification" @@ -1318,10 +1318,7 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega private extension String { func sha1Base64() -> String { - let data = self.data(using: String.Encoding.utf8)! - var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH)) - data.withUnsafeBytes { _ = CC_SHA1($0, CC_LONG(data.count), &digest) } - return Data(bytes: digest).base64EncodedString() + return (self as NSString).ss_SHA1Base64Digest() } } diff --git a/Sources/modulemap/CommonCrypto/NSString+SHA1.h b/Sources/modulemap/CommonCrypto/NSString+SHA1.h new file mode 100644 index 00000000..4a2206e3 --- /dev/null +++ b/Sources/modulemap/CommonCrypto/NSString+SHA1.h @@ -0,0 +1,29 @@ +////////////////////////////////////////////////////////////////////////////////////////////////// +// +// NSString+SHA1.h +// Starscream +// +// Created by Sergey Lem on 6/25/18. +// Copyright (c) 2014-2016 Dalton Cherry. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +////////////////////////////////////////////////////////////////////////////////////////////////// + +#import + +@interface NSString (SHA1) + +- (NSString *)ss_SHA1Base64Digest; + +@end diff --git a/Sources/modulemap/CommonCrypto/NSString+SHA1.m b/Sources/modulemap/CommonCrypto/NSString+SHA1.m new file mode 100644 index 00000000..c50bc9ca --- /dev/null +++ b/Sources/modulemap/CommonCrypto/NSString+SHA1.m @@ -0,0 +1,35 @@ +////////////////////////////////////////////////////////////////////////////////////////////////// +// +// NSString+SHA1.m +// Starscream +// +// Created by Sergey Lem on 6/25/18. +// Copyright (c) 2014-2016 Dalton Cherry. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +////////////////////////////////////////////////////////////////////////////////////////////////// + +#import "NSString+SHA1.h" +#import + +@implementation NSString (SHA1) + +- (NSString *)ss_SHA1Base64Digest { + NSData *stringData = [self dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *digest = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH]; + CC_SHA1(stringData.bytes, stringData.length, digest.mutableBytes); + return [digest base64EncodedDataWithOptions:0]; +} + +@end diff --git a/Sources/modulemap/Starscream.modulemap b/Sources/modulemap/Starscream.modulemap new file mode 100644 index 00000000..cbaddbfa --- /dev/null +++ b/Sources/modulemap/Starscream.modulemap @@ -0,0 +1,16 @@ +framework module Starscream { + umbrella header "Starscream.h" + + export * + + explicit module SSCZLib [system] { + header "include.h" + link "z" + export * + } + explicit module SSCommonCrypto [system] { + private header "NSString+SHA1.h" + link "CommonCrypto" + export * + } +} diff --git a/Sources/modulemap/zlib/include.h b/Sources/modulemap/zlib/include.h new file mode 100644 index 00000000..4470a1fd --- /dev/null +++ b/Sources/modulemap/zlib/include.h @@ -0,0 +1 @@ +#include diff --git a/Starscream.podspec b/Starscream.podspec index 3d81d07e..76b5e3fb 100644 --- a/Starscream.podspec +++ b/Starscream.podspec @@ -11,11 +11,10 @@ Pod::Spec.new do |s| s.osx.deployment_target = '10.10' s.tvos.deployment_target = '9.0' s.watchos.deployment_target = '2.0' - s.source_files = 'Sources/*.swift' - s.libraries = 'z' + s.source_files = 'Sources/**/*.{h,m,swift}' + s.module_map = 'Sources/modulemap/Starscream.modulemap' + s.private_header_files = 'Sources/modulemap/**/*.h' s.pod_target_xcconfig = { - 'SWIFT_VERSION' => '4.1', - 'SWIFT_INCLUDE_PATHS' => '$(PODS_ROOT)/Starscream/zlib' + 'SWIFT_VERSION' => '4.1' } - s.preserve_paths = 'zlib/*' end diff --git a/Starscream.xcodeproj/project.pbxproj b/Starscream.xcodeproj/project.pbxproj index c3da4a1c..ad68c3b4 100644 --- a/Starscream.xcodeproj/project.pbxproj +++ b/Starscream.xcodeproj/project.pbxproj @@ -12,12 +12,9 @@ 335FA1FC1F5DF71D00F6D2EC /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D88EAF811ED4DFD3004FE2C3 /* libz.tbd */; }; 33CCF08A1F5DDC030099B092 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D88EAF811ED4DFD3004FE2C3 /* libz.tbd */; }; 33CCF08C1F5DDC030099B092 /* Starscream.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C1360001C473BEF00AA3A01 /* Starscream.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 33CCF08D1F5DDC030099B092 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = D85927D71ED76F25003460CB /* include.h */; }; - 742D12982157CF56006026D7 /* Starscream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33CCF0921F5DDC030099B092 /* Starscream.framework */; }; - DD52B623663980FECD3F6690 /* Compression.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD52B83305DE12CC7F8887D6 /* Compression.swift */; }; - DD52B7C033385CD7CF246CC5 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD52B820CB852287AC065D9C /* WebSocket.swift */; }; - DD52BC079AD583D2DA35D7E7 /* SSLClientCertificate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD52B329FF434097A6C8F66E /* SSLClientCertificate.swift */; }; - DD52BED25D3DBBB3BC28471B /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD52B3F585852EF29B21F0DB /* SSLSecurity.swift */; }; + 33CCF08D1F5DDC030099B092 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = D85927D71ED76F25003460CB /* include.h */; settings = {ATTRIBUTES = (Private, ); }; }; + BB8A648020E1105C00527BA5 /* NSString+SHA1.h in Headers */ = {isa = PBXBuildFile; fileRef = BB8A647E20E1105C00527BA5 /* NSString+SHA1.h */; settings = {ATTRIBUTES = (Private, ); }; }; + BB8A648120E1105C00527BA5 /* NSString+SHA1.m in Sources */ = {isa = PBXBuildFile; fileRef = BB8A647F20E1105C00527BA5 /* NSString+SHA1.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -27,8 +24,9 @@ 5C13600C1C473BFE00AA3A01 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = SOURCE_ROOT; }; 6B3E7A0019D48C2F006071F7 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 742419BB1DC6BDBA003ACE43 /* StarscreamTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StarscreamTests.swift; path = StarscreamTests/StarscreamTests.swift; sourceTree = ""; }; - BBC59F4D20D2D71500713D9C /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; - D85927D61ED761A0003460CB /* module.private.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.private.modulemap; sourceTree = ""; }; + BB8A647E20E1105C00527BA5 /* NSString+SHA1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+SHA1.h"; sourceTree = ""; }; + BB8A647F20E1105C00527BA5 /* NSString+SHA1.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+SHA1.m"; sourceTree = ""; }; + BBC59F4D20D2D71500713D9C /* Starscream.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = Starscream.modulemap; sourceTree = ""; }; D85927D71ED76F25003460CB /* include.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = include.h; sourceTree = ""; }; D88EAF811ED4DFD3004FE2C3 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; D88EAF831ED4E7D8004FE2C3 /* CompressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompressionTests.swift; sourceTree = ""; }; @@ -58,7 +56,6 @@ 6B3E79DC19D48B7F006071F7 = { isa = PBXGroup; children = ( - D85927D51ED761A0003460CB /* zlib */, 6B3E79E819D48B7F006071F7 /* Sources */, 6B3E79FF19D48C2F006071F7 /* Tests */, 6B3E79E719D48B7F006071F7 /* Products */, @@ -78,6 +75,7 @@ 6B3E79E819D48B7F006071F7 /* Sources */ = { isa = PBXGroup; children = ( + BB8A648320E1123400527BA5 /* modulemap */, 5C1360001C473BEF00AA3A01 /* Starscream.h */, 6B3E79E919D48B7F006071F7 /* Supporting Files */, DD52B820CB852287AC065D9C /* WebSocket.swift */, @@ -106,16 +104,33 @@ path = Tests; sourceTree = ""; }; - D85927D51ED761A0003460CB /* zlib */ = { + BB8A647D20E1102900527BA5 /* CommonCrypto */ = { + isa = PBXGroup; + children = ( + BB8A647E20E1105C00527BA5 /* NSString+SHA1.h */, + BB8A647F20E1105C00527BA5 /* NSString+SHA1.m */, + ); + path = CommonCrypto; + sourceTree = ""; + }; + BB8A648220E111F200527BA5 /* zlib */ = { isa = PBXGroup; children = ( - D85927D61ED761A0003460CB /* module.private.modulemap */, - BBC59F4D20D2D71500713D9C /* module.modulemap */, D85927D71ED76F25003460CB /* include.h */, ); path = zlib; sourceTree = ""; }; + BB8A648320E1123400527BA5 /* modulemap */ = { + isa = PBXGroup; + children = ( + BBC59F4D20D2D71500713D9C /* Starscream.modulemap */, + BB8A647D20E1102900527BA5 /* CommonCrypto */, + BB8A648220E111F200527BA5 /* zlib */, + ); + path = modulemap; + sourceTree = ""; + }; D88EAF801ED4DFD3004FE2C3 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -131,6 +146,7 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + BB8A648020E1105C00527BA5 /* NSString+SHA1.h in Headers */, 33CCF08C1F5DDC030099B092 /* Starscream.h in Headers */, 33CCF08D1F5DDC030099B092 /* include.h in Headers */, ); @@ -188,6 +204,9 @@ 335FA1F41F5DF71D00F6D2EC = { LastSwiftMigration = 0900; }; + 33CCF0841F5DDC030099B092 = { + LastSwiftMigration = 0940; + }; }; }; buildConfigurationList = 6B3E79E019D48B7F006071F7 /* Build configuration list for PBXProject "Starscream" */; @@ -239,10 +258,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - DD52B7C033385CD7CF246CC5 /* WebSocket.swift in Sources */, - DD52BC079AD583D2DA35D7E7 /* SSLClientCertificate.swift in Sources */, - DD52BED25D3DBBB3BC28471B /* SSLSecurity.swift in Sources */, - DD52B623663980FECD3F6690 /* Compression.swift in Sources */, + 33CCF0861F5DDC030099B092 /* Compression.swift in Sources */, + 33CCF0871F5DDC030099B092 /* WebSocket.swift in Sources */, + BB8A648120E1105C00527BA5 /* NSString+SHA1.m in Sources */, + 33CCF0881F5DDC030099B092 /* SSLSecurity.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -316,6 +335,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; + MODULEMAP_FILE = $SRCROOT/Sources/modulemap/Starscream.modulemap; OTHER_LDFLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = "com.vluxe.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -349,6 +369,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; + MODULEMAP_FILE = $SRCROOT/Sources/modulemap/Starscream.modulemap; OTHER_LDFLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = "com.vluxe.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -416,7 +437,6 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = ""; SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvos appletvsimulator watchsimulator watchos"; - SWIFT_INCLUDE_PATHS = $SRCROOT/zlib; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; @@ -469,7 +489,6 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = ""; SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvos appletvsimulator watchsimulator watchos"; - SWIFT_INCLUDE_PATHS = $SRCROOT/zlib; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; VALIDATE_PRODUCT = YES; diff --git a/zlib/include.h b/zlib/include.h deleted file mode 100644 index cb3747f2..00000000 --- a/zlib/include.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -#include diff --git a/zlib/module.modulemap b/zlib/module.modulemap deleted file mode 100644 index 8b137891..00000000 --- a/zlib/module.modulemap +++ /dev/null @@ -1 +0,0 @@ - diff --git a/zlib/module.private.modulemap b/zlib/module.private.modulemap deleted file mode 100644 index ed0676f3..00000000 --- a/zlib/module.private.modulemap +++ /dev/null @@ -1,9 +0,0 @@ -module SSCZLib [system] { - header "include.h" - link "z" - export * -} -module SSCommonCrypto [system] { - header "include.h" - export * -}