From efd52fc46e9f0853bf59c9e25d73d8345c23e291 Mon Sep 17 00:00:00 2001 From: onevcat Date: Thu, 28 Dec 2017 15:34:14 +0900 Subject: [PATCH] Revert MD5 implementation without dependency --- Kingfisher.podspec | 14 +- Kingfisher.xcodeproj/project.pbxproj | 14 - .../CommonCrypto/appleTVOS/module.modulemap | 5 - .../appleTVSimulator/module.modulemap | 6 - .../CommonCrypto/iPhoneOS/module.modulemap | 6 - .../iPhoneSimulator/module.modulemap | 6 - Sources/CommonCrypto/macOSX/module.modulemap | 6 - Sources/CommonCrypto/watchOS/module.modulemap | 6 - .../watchSimulator/module.modulemap | 6 - Sources/String+MD5.swift | 294 ++++++++++++++++-- 10 files changed, 263 insertions(+), 100 deletions(-) delete mode 100644 Sources/CommonCrypto/appleTVOS/module.modulemap delete mode 100644 Sources/CommonCrypto/appleTVSimulator/module.modulemap delete mode 100644 Sources/CommonCrypto/iPhoneOS/module.modulemap delete mode 100644 Sources/CommonCrypto/iPhoneSimulator/module.modulemap delete mode 100644 Sources/CommonCrypto/macOSX/module.modulemap delete mode 100644 Sources/CommonCrypto/watchOS/module.modulemap delete mode 100644 Sources/CommonCrypto/watchSimulator/module.modulemap diff --git a/Kingfisher.podspec b/Kingfisher.podspec index 0144a8241..71217bbc1 100644 --- a/Kingfisher.podspec +++ b/Kingfisher.podspec @@ -46,20 +46,10 @@ Pod::Spec.new do |s| ] s.ios.exclude_files = "Sources/NSButton+Kingfisher.swift" s.tvos.exclude_files = "Sources/NSButton+Kingfisher.swift" - - s.resources = ['Sources/CommonCrypto'] s.requires_arc = true s.framework = "CFNetwork" - s.pod_target_xcconfig = { - 'SWIFT_VERSION' => '4.0', - 'SWIFT_INCLUDE_PATHS[sdk=iphoneos*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/iPhoneOS', - 'SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/iPhoneSimulator', - 'SWIFT_INCLUDE_PATHS[sdk=appletvos*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/appleTVOS', - 'SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/appleTVSimulator', - 'SWIFT_INCLUDE_PATHS[sdk=macosx*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/macOSX', - 'SWIFT_INCLUDE_PATHS[sdk=watchos*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/watchOS', - 'SWIFT_INCLUDE_PATHS[sdk=watchsimulator*]' => '$(PODS_TARGET_SRCROOT)/Sources/CommonCrypto/watchSimulator', - } + s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' } + end diff --git a/Kingfisher.xcodeproj/project.pbxproj b/Kingfisher.xcodeproj/project.pbxproj index b6fcbc5c3..6db27cce3 100644 --- a/Kingfisher.xcodeproj/project.pbxproj +++ b/Kingfisher.xcodeproj/project.pbxproj @@ -2523,7 +2523,6 @@ PRODUCT_NAME = Kingfisher; SDKROOT = macosx; SKIP_INSTALL = YES; - SWIFT_INCLUDE_PATHS = Sources/CommonCrypto/macOSX; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; VERSIONING_SYSTEM = "apple-generic"; @@ -2554,7 +2553,6 @@ PRODUCT_NAME = Kingfisher; SDKROOT = macosx; SKIP_INSTALL = YES; - SWIFT_INCLUDE_PATHS = Sources/CommonCrypto/macOSX; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; @@ -2755,8 +2753,6 @@ SDKROOT = appletvos; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=appletvos*]" = Sources/CommonCrypto/appleTVOS; - "SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]" = Sources/CommonCrypto/appleTVSimulator; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; @@ -2790,8 +2786,6 @@ SDKROOT = appletvos; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=appletvos*]" = Sources/CommonCrypto/appleTVOS; - "SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]" = Sources/CommonCrypto/appleTVSimulator; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; @@ -2824,8 +2818,6 @@ SDKROOT = watchos; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=watchos*]" = Sources/CommonCrypto/watchOS; - "SWIFT_INCLUDE_PATHS[sdk=watchsimulator*]" = Sources/CommonCrypto/watchSimulator; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = 4; @@ -2856,8 +2848,6 @@ SDKROOT = watchos; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=watchos*]" = Sources/CommonCrypto/watchOS; - "SWIFT_INCLUDE_PATHS[sdk=watchsimulator*]" = Sources/CommonCrypto/watchSimulator; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; @@ -3110,8 +3100,6 @@ PRODUCT_NAME = Kingfisher; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=iphoneos*]" = Sources/CommonCrypto/iPhoneOS; - "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]" = Sources/CommonCrypto/iPhoneSimulator; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; @@ -3141,8 +3129,6 @@ PRODUCT_NAME = Kingfisher; SKIP_INSTALL = YES; SWIFT_INCLUDE_PATHS = ""; - "SWIFT_INCLUDE_PATHS[sdk=iphoneos*]" = Sources/CommonCrypto/iPhoneOS; - "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]" = Sources/CommonCrypto/iPhoneSimulator; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_SWIFT3_OBJC_INFERENCE = Off; SWIFT_VERSION = 4.0; diff --git a/Sources/CommonCrypto/appleTVOS/module.modulemap b/Sources/CommonCrypto/appleTVOS/module.modulemap deleted file mode 100644 index b3104faf8..000000000 --- a/Sources/CommonCrypto/appleTVOS/module.modulemap +++ /dev/null @@ -1,5 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} diff --git a/Sources/CommonCrypto/appleTVSimulator/module.modulemap b/Sources/CommonCrypto/appleTVSimulator/module.modulemap deleted file mode 100644 index 34bb679f5..000000000 --- a/Sources/CommonCrypto/appleTVSimulator/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/CommonCrypto/iPhoneOS/module.modulemap b/Sources/CommonCrypto/iPhoneOS/module.modulemap deleted file mode 100644 index 6cfd5f496..000000000 --- a/Sources/CommonCrypto/iPhoneOS/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/CommonCrypto/iPhoneSimulator/module.modulemap b/Sources/CommonCrypto/iPhoneSimulator/module.modulemap deleted file mode 100644 index 68285658c..000000000 --- a/Sources/CommonCrypto/iPhoneSimulator/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/CommonCrypto/macOSX/module.modulemap b/Sources/CommonCrypto/macOSX/module.modulemap deleted file mode 100644 index e584c83a8..000000000 --- a/Sources/CommonCrypto/macOSX/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/CommonCrypto/watchOS/module.modulemap b/Sources/CommonCrypto/watchOS/module.modulemap deleted file mode 100644 index 6f23a9dd2..000000000 --- a/Sources/CommonCrypto/watchOS/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/CommonCrypto/watchSimulator/module.modulemap b/Sources/CommonCrypto/watchSimulator/module.modulemap deleted file mode 100644 index def0d7058..000000000 --- a/Sources/CommonCrypto/watchSimulator/module.modulemap +++ /dev/null @@ -1,6 +0,0 @@ - -module CCommonCrypto [system] { - header "/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h" - export * -} - diff --git a/Sources/String+MD5.swift b/Sources/String+MD5.swift index beb5767bb..5563fa137 100755 --- a/Sources/String+MD5.swift +++ b/Sources/String+MD5.swift @@ -2,32 +2,24 @@ // String+MD5.swift // Kingfisher // -// Copyright (c) 2017 Wei Wang -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// adding CommonCrypto to a Swift framework See: +// To date, adding CommonCrypto to a Swift framework is problematic. See: // http://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework +// We're using a subset and modified version of CryptoSwift as an alternative. +// The following is an altered source version that only includes MD5. The original software can be found at: +// https://github.com/krzyzanowskim/CryptoSwift +// This is the original copyright notice: +/* +Copyright (C) 2014 Marcin Krzyżanowski +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: +- The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required. +- Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +- This notice may not be removed or altered from any source or binary distribution. +*/ import Foundation -import CCommonCrypto public struct StringProxy { fileprivate let base: String @@ -45,20 +37,256 @@ extension String: KingfisherCompatible { extension StringProxy { var md5: String { - guard let cStr = base.cString(using: .utf8) else { + if let data = base.data(using: .utf8, allowLossyConversion: true) { + + let message = data.withUnsafeBytes { bytes -> [UInt8] in + return Array(UnsafeBufferPointer(start: bytes, count: data.count)) + } + + let MD5Calculator = MD5(message) + let MD5Data = MD5Calculator.calculate() + + var MD5String = String() + for c in MD5Data { + MD5String += String(format: "%02x", c) + } + return MD5String + + } else { return base } - let bytesLength = CUnsignedInt(base.lengthOfBytes(using: .utf8)) - let md5DigestLenth = Int(CC_MD5_DIGEST_LENGTH) - let md5StringPointer = UnsafeMutablePointer.allocate(capacity: md5DigestLenth) - defer { - md5StringPointer.deallocate(capacity: md5DigestLenth) + } +} + + +/** array of bytes, little-endian representation */ +func arrayOfBytes(_ value: T, length: Int? = nil) -> [UInt8] { + let totalBytes = length ?? (MemoryLayout.size * 8) + + let valuePointer = UnsafeMutablePointer.allocate(capacity: 1) + valuePointer.pointee = value + + let bytes = valuePointer.withMemoryRebound(to: UInt8.self, capacity: totalBytes) { (bytesPointer) -> [UInt8] in + var bytes = [UInt8](repeating: 0, count: totalBytes) + for j in 0...size, totalBytes) { + bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee + } + return bytes + } + + valuePointer.deinitialize() + valuePointer.deallocate(capacity: 1) + + return bytes +} + +extension Int { + /** Array of bytes with optional padding (little-endian) */ + func bytes(_ totalBytes: Int = MemoryLayout.size) -> [UInt8] { + return arrayOfBytes(self, length: totalBytes) + } + +} + +extension NSMutableData { + + /** Convenient way to append bytes */ + func appendBytes(_ arrayOfBytes: [UInt8]) { + append(arrayOfBytes, length: arrayOfBytes.count) + } + +} + +protocol HashProtocol { + var message: Array { get } + + /** Common part for hash calculation. Prepare header data. */ + func prepare(_ len: Int) -> Array +} + +extension HashProtocol { + + func prepare(_ len: Int) -> Array { + var tmpMessage = message + + // Step 1. Append Padding Bits + tmpMessage.append(0x80) // append one bit (UInt8 with one bit) to message + + // append "0" bit until message length in bits ≡ 448 (mod 512) + var msgLength = tmpMessage.count + var counter = 0 + + while msgLength % len != (len - 8) { + counter += 1 + msgLength += 1 + } + + tmpMessage += Array(repeating: 0, count: counter) + return tmpMessage + } +} + +func toUInt32Array(_ slice: ArraySlice) -> Array { + var result = Array() + result.reserveCapacity(16) + + for idx in stride(from: slice.startIndex, to: slice.endIndex, by: MemoryLayout.size) { + let d0 = UInt32(slice[idx.advanced(by: 3)]) << 24 + let d1 = UInt32(slice[idx.advanced(by: 2)]) << 16 + let d2 = UInt32(slice[idx.advanced(by: 1)]) << 8 + let d3 = UInt32(slice[idx]) + let val: UInt32 = d0 | d1 | d2 | d3 + + result.append(val) + } + return result +} + +struct BytesIterator: IteratorProtocol { + + let chunkSize: Int + let data: [UInt8] + + init(chunkSize: Int, data: [UInt8]) { + self.chunkSize = chunkSize + self.data = data + } + + var offset = 0 + + mutating func next() -> ArraySlice? { + let end = min(chunkSize, data.count - offset) + let result = data[offset.. 0 ? result : nil + } +} + +struct BytesSequence: Sequence { + let chunkSize: Int + let data: [UInt8] + + func makeIterator() -> BytesIterator { + return BytesIterator(chunkSize: chunkSize, data: data) + } +} + +func rotateLeft(_ value: UInt32, bits: UInt32) -> UInt32 { + return ((value << bits) & 0xFFFFFFFF) | (value >> (32 - bits)) +} + +class MD5: HashProtocol { + + static let size = 16 // 128 / 8 + let message: [UInt8] + + init (_ message: [UInt8]) { + self.message = message + } + + /** specifies the per-round shift amounts */ + private let shifts: [UInt32] = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21] + + /** binary integer part of the sines of integers (Radians) */ + private let sines: [UInt32] = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391] + + private let hashes: [UInt32] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476] + + func calculate() -> [UInt8] { + var tmpMessage = prepare(64) + tmpMessage.reserveCapacity(tmpMessage.count + 4) + + // hash values + var hh = hashes + + // Step 2. Append Length a 64-bit representation of lengthInBits + let lengthInBits = (message.count * 8) + let lengthBytes = lengthInBits.bytes(64 / 8) + tmpMessage += lengthBytes.reversed() + + // Process the message in successive 512-bit chunks: + let chunkSizeBytes = 512 / 8 // 64 + + for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) { + // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 + var M = toUInt32Array(chunk) + assert(M.count == 16, "Invalid array") + + // Initialize hash value for this chunk: + var A: UInt32 = hh[0] + var B: UInt32 = hh[1] + var C: UInt32 = hh[2] + var D: UInt32 = hh[3] + + var dTemp: UInt32 = 0 + + // Main loop + for j in 0 ..< sines.count { + var g = 0 + var F: UInt32 = 0 + + switch j { + case 0...15: + F = (B & C) | ((~B) & D) + g = j + break + case 16...31: + F = (D & B) | (~D & C) + g = (5 * j + 1) % 16 + break + case 32...47: + F = B ^ C ^ D + g = (3 * j + 5) % 16 + break + case 48...63: + F = C ^ (B | (~D)) + g = (7 * j) % 16 + break + default: + break + } + dTemp = D + D = C + C = B + B = B &+ rotateLeft((A &+ F &+ sines[j] &+ M[g]), bits: shifts[j]) + A = dTemp + } + + hh[0] = hh[0] &+ A + hh[1] = hh[1] &+ B + hh[2] = hh[2] &+ C + hh[3] = hh[3] &+ D } - CC_MD5(cStr, bytesLength, md5StringPointer) - var md5String = "" - for i in 0 ..< md5DigestLenth { - md5String = md5String.appendingFormat("%02x", md5StringPointer[i]) + + var result = [UInt8]() + result.reserveCapacity(hh.count / 4) + + hh.forEach { + let itemLE = $0.littleEndian + let r1 = UInt8(itemLE & 0xff) + let r2 = UInt8((itemLE >> 8) & 0xff) + let r3 = UInt8((itemLE >> 16) & 0xff) + let r4 = UInt8((itemLE >> 24) & 0xff) + result += [r1, r2, r3, r4] } - return md5String + return result } }