Procházet zdrojové kódy

Swift 4.1 update. (Breaks source compatibility).

Marcin Krzyżanowski před 7 roky
rodič
revize
9d82f5067e

+ 11 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -13,6 +13,7 @@
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		751EE9781F93996100161FFC /* AES.Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 751EE9771F93996100161FFC /* AES.Cryptors.swift */; };
 		753B33011DAB84D600D06422 /* RandomBytesSequenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753B33001DAB84D600D06422 /* RandomBytesSequenceTests.swift */; };
+		754310442050111A003FB1DF /* CompactMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754310432050111A003FB1DF /* CompactMap.swift */; };
 		75482EA41CB310B7001F66A5 /* PBKDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EA31CB310B7001F66A5 /* PBKDF.swift */; };
 		754BE46819693E190098E6F3 /* DigestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754BE46719693E190098E6F3 /* DigestTests.swift */; };
 		755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755FB1D9199E347D00475437 /* ExtensionsTest.swift */; };
@@ -160,6 +161,7 @@
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
 		751EE9771F93996100161FFC /* AES.Cryptors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AES.Cryptors.swift; sourceTree = "<group>"; };
 		753B33001DAB84D600D06422 /* RandomBytesSequenceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RandomBytesSequenceTests.swift; sourceTree = "<group>"; };
+		754310432050111A003FB1DF /* CompactMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompactMap.swift; sourceTree = "<group>"; };
 		75482EA31CB310B7001F66A5 /* PBKDF.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBKDF.swift; sourceTree = "<group>"; };
 		754BE45519693E190098E6F3 /* CryptoSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CryptoSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		754BE46019693E190098E6F3 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -373,6 +375,7 @@
 				75EC52771EE8B6CA0048EB3B /* Updatable.swift */,
 				75EC52781EE8B6CA0048EB3B /* Utils.swift */,
 				75EC52791EE8B6CA0048EB3B /* ZeroPadding.swift */,
+				754310432050111A003FB1DF /* CompactMap.swift */,
 			);
 			path = CryptoSwift;
 			sourceTree = "<group>";
@@ -486,7 +489,7 @@
 			isa = PBXProject;
 			attributes = {
 				LastSwiftUpdateCheck = 0700;
-				LastUpgradeCheck = 0900;
+				LastUpgradeCheck = 0930;
 				ORGANIZATIONNAME = "Marcin Krzyzanowski";
 				TargetAttributes = {
 					754BE45419693E190098E6F3 = {
@@ -557,6 +560,7 @@
 				75EC52B01EE8B83D0048EB3B /* SHA2.swift in Sources */,
 				75EC52B71EE8B83D0048EB3B /* Updatable.swift in Sources */,
 				75EC528E1EE8B81A0048EB3B /* Checksum.swift in Sources */,
+				754310442050111A003FB1DF /* CompactMap.swift in Sources */,
 				75EC52811EE8B8130048EB3B /* BlockCipher.swift in Sources */,
 				75EC52941EE8B81A0048EB3B /* DigestType.swift in Sources */,
 				75EC529B1EE8B8200048EB3B /* Rabbit+Foundation.swift in Sources */,
@@ -681,12 +685,14 @@
 				CLANG_WARN_BOOL_CONVERSION = YES;
 				CLANG_WARN_COMMA = YES;
 				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
 				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
 				CLANG_WARN_EMPTY_BODY = YES;
 				CLANG_WARN_ENUM_CONVERSION = YES;
 				CLANG_WARN_INFINITE_RECURSION = YES;
 				CLANG_WARN_INT_CONVERSION = YES;
 				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
+				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
 				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
 				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
 				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -744,12 +750,14 @@
 				CLANG_WARN_BOOL_CONVERSION = YES;
 				CLANG_WARN_COMMA = YES;
 				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
 				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
 				CLANG_WARN_EMPTY_BODY = YES;
 				CLANG_WARN_ENUM_CONVERSION = YES;
 				CLANG_WARN_INFINITE_RECURSION = YES;
 				CLANG_WARN_INT_CONVERSION = YES;
 				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
+				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
 				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
 				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
 				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -806,7 +814,6 @@
 				PRODUCT_BUNDLE_IDENTIFIER = com.krzyzanowskim.CryptoSwift;
 				PRODUCT_NAME = CryptoSwift;
 				SKIP_INSTALL = YES;
-				SWIFT_COMPILATION_MODE = wholemodule;
 				SWIFT_ENFORCE_EXCLUSIVE_ACCESS = none;
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
 				SWIFT_VERSION = 4.0;
@@ -835,7 +842,6 @@
 				PRODUCT_BUNDLE_IDENTIFIER = com.krzyzanowskim.CryptoSwift;
 				PRODUCT_NAME = CryptoSwift;
 				SKIP_INSTALL = YES;
-				SWIFT_COMPILATION_MODE = wholemodule;
 				SWIFT_DISABLE_SAFETY_CHECKS = YES;
 				SWIFT_ENFORCE_EXCLUSIVE_ACCESS = "compile-time";
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@@ -897,12 +903,14 @@
 				CLANG_WARN_BOOL_CONVERSION = YES;
 				CLANG_WARN_COMMA = YES;
 				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
 				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
 				CLANG_WARN_EMPTY_BODY = YES;
 				CLANG_WARN_ENUM_CONVERSION = YES;
 				CLANG_WARN_INFINITE_RECURSION = YES;
 				CLANG_WARN_INT_CONVERSION = YES;
 				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
+				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
 				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
 				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
 				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -965,7 +973,6 @@
 				PRODUCT_BUNDLE_IDENTIFIER = com.krzyzanowskim.CryptoSwift;
 				PRODUCT_NAME = CryptoSwift;
 				SKIP_INSTALL = YES;
-				SWIFT_COMPILATION_MODE = wholemodule;
 				SWIFT_DISABLE_SAFETY_CHECKS = YES;
 				SWIFT_ENFORCE_EXCLUSIVE_ACCESS = "compile-time";
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

+ 3 - 5
CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <Scheme
-   LastUpgradeVersion = "0900"
+   LastUpgradeVersion = "0930"
    version = "1.3">
    <BuildAction
       parallelizeBuildables = "NO"
@@ -41,10 +41,9 @@
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       disableMainThreadChecker = "YES"
-      language = ""
       systemAttachmentLifetime = "keepNever"
-      shouldUseLaunchSchemeArgsEnv = "YES"
-      codeCoverageEnabled = "YES">
+      codeCoverageEnabled = "YES"
+      shouldUseLaunchSchemeArgsEnv = "YES">
       <Testables>
          <TestableReference
             skipped = "NO">
@@ -73,7 +72,6 @@
       buildConfiguration = "Test"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      language = ""
       launchStyle = "0"
       useCustomWorkingDirectory = "NO"
       ignoresPersistentStateOnLaunch = "NO"

+ 10 - 10
Sources/CryptoSwift/AES.swift

@@ -160,10 +160,10 @@ public final class AES: BlockCipher {
 
         let tLength = 4
         let t = UnsafeMutablePointer<UInt32>.allocate(capacity: tLength)
-        t.initialize(to: 0, count: tLength)
+        t.initialize(repeating: 0, count: tLength)
         defer {
             t.deinitialize(count: tLength)
-            t.deallocate(capacity: tLength)
+            t.deallocate()
         }
 
         for r in 0..<rounds - 1 {
@@ -255,10 +255,10 @@ public final class AES: BlockCipher {
 
         let tLength = 4
         let t = UnsafeMutablePointer<UInt32>.allocate(capacity: tLength)
-        t.initialize(to: 0, count: tLength)
+        t.initialize(repeating: 0, count: tLength)
         defer {
             t.deinitialize(count: tLength)
-            t.deallocate(capacity: tLength)
+            t.deallocate()
         }
 
         for r in (2...rounds).reversed() {
@@ -385,10 +385,10 @@ private extension AES {
 
         let wLength = variantNb * (variantNr + 1) * 4
         let w = UnsafeMutablePointer<UInt8>.allocate(capacity: wLength)
-        w.initialize(to: 0, count: wLength)
+        w.initialize(repeating: 0, count: wLength)
         defer {
             w.deinitialize(count: wLength)
-            w.deallocate(capacity: wLength)
+            w.deallocate()
         }
 
         for i in 0..<variantNk {
@@ -454,13 +454,13 @@ private extension AES {
         let sboxLength = 256
         let sbox = UnsafeMutablePointer<UInt32>.allocate(capacity: sboxLength)
         let invsbox = UnsafeMutablePointer<UInt32>.allocate(capacity: sboxLength)
-        sbox.initialize(to: 0, count: sboxLength)
-        invsbox.initialize(to: 0, count: sboxLength)
+        sbox.initialize(repeating: 0, count: sboxLength)
+        invsbox.initialize(repeating: 0, count: sboxLength)
         defer {
             sbox.deinitialize(count: sboxLength)
-            sbox.deallocate(capacity: sboxLength)
+            sbox.deallocate()
             invsbox.deinitialize(count: sboxLength)
-            invsbox.deallocate(capacity: sboxLength)
+            invsbox.deallocate()
         }
 
         sbox[0] = 0x63

+ 2 - 2
Sources/CryptoSwift/BatchedCollection.swift

@@ -33,7 +33,7 @@ protocol BatchedCollectionType: Collection {
 
 struct BatchedCollection<Base: Collection>: Collection {
     let base: Base
-    let size: Base.IndexDistance
+    let size: Int
     typealias Index = BatchedCollectionIndex<Base>
     private func nextBreak(after idx: Base.Index) -> Base.Index {
         return base.index(idx, offsetBy: size, limitedBy: base.endIndex)
@@ -58,7 +58,7 @@ struct BatchedCollection<Base: Collection>: Collection {
 }
 
 extension Collection {
-    func batched(by size: IndexDistance) -> BatchedCollection<Self> {
+    func batched(by size: Int) -> BatchedCollection<Self> {
         return BatchedCollection(base: self, size: size)
     }
 }

+ 2 - 2
Sources/CryptoSwift/Blowfish.swift

@@ -495,7 +495,7 @@ extension Blowfish: Cipher {
     ///
     /// - Parameter bytes: Plaintext data
     /// - Returns: Encrypted data
-    public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
+    public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Element == UInt8, C.Index == Int {
 
         let bytes = padding.add(to: Array(bytes), blockSize: Blowfish.blockSize) // FIXME: Array(bytes) copies
 
@@ -517,7 +517,7 @@ extension Blowfish: Cipher {
     ///
     /// - Parameter bytes: Ciphertext data
     /// - Returns: Plaintext data
-    public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
+    public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Element == UInt8, C.Index == Int {
 
         if blockMode.options.contains(.paddingRequired) && (bytes.count % Blowfish.blockSize != 0) {
             throw Error.dataPaddingRequired

+ 23 - 0
Sources/CryptoSwift/CompactMap.swift

@@ -0,0 +1,23 @@
+////  CryptoSwift
+//
+//  Copyright (C) 2014-__YEAR__ Marcin Krzyżanowski <marcin@krzyzanowskim.com>
+//  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.
+//
+
+#if swift(>=4.1)
+// TODO: remove this file when Xcode 9.2 is no longer used
+#else
+extension Sequence {
+    public func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
+        return try flatMap(transform)
+    }
+}
+#endif

+ 2 - 2
Sources/CryptoSwift/Generics.swift

@@ -48,8 +48,8 @@ func arrayOfBytes<T: FixedWidthInteger>(value: T, length totalBytes: Int = Memor
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
     }
 
-    valuePointer.deinitialize()
-    valuePointer.deallocate(capacity: 1)
+    valuePointer.deinitialize(count: 1)
+    valuePointer.deallocate()
 
     return bytes
 }

+ 2 - 2
Sources/CryptoSwift/SHA1.swift

@@ -37,10 +37,10 @@ public final class SHA1: DigestType {
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 32-bit words into eighty 32-bit words:
         let M = UnsafeMutablePointer<UInt32>.allocate(capacity: 80)
-        M.initialize(to: 0, count: 80)
+        M.initialize(repeating: 0, count: 80)
         defer {
             M.deinitialize(count: 80)
-            M.deallocate(capacity: 80)
+            M.deallocate()
         }
 
         for x in 0..<80 {

+ 4 - 4
Sources/CryptoSwift/SHA2.swift

@@ -158,10 +158,10 @@ public final class SHA2: DigestType {
         // break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 64-bit words into eighty 64-bit words:
         let M = UnsafeMutablePointer<UInt64>.allocate(capacity: k.count)
-        M.initialize(to: 0, count: k.count)
+        M.initialize(repeating: 0, count: k.count)
         defer {
             M.deinitialize(count: self.k.count)
-            M.deallocate(capacity: self.k.count)
+            M.deallocate()
         }
         for x in 0..<k.count {
             switch x {
@@ -220,10 +220,10 @@ public final class SHA2: DigestType {
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 32-bit words into sixty-four 32-bit words:
         let M = UnsafeMutablePointer<UInt32>.allocate(capacity: k.count)
-        M.initialize(to: 0, count: k.count)
+        M.initialize(repeating: 0, count: k.count)
         defer {
             M.deinitialize(count: self.k.count)
-            M.deallocate(capacity: self.k.count)
+            M.deallocate()
         }
 
         for x in 0..<k.count {

+ 4 - 4
Sources/CryptoSwift/SHA3.swift

@@ -97,16 +97,16 @@ public final class SHA3: DigestType {
     ///     A′[x, y,z] = A[x, y,z] ⊕ D[x,z].
     private func θ(_ a: inout Array<UInt64>) {
         let c = UnsafeMutablePointer<UInt64>.allocate(capacity: 5)
-        c.initialize(to: 0, count: 5)
+        c.initialize(repeating: 0, count: 5)
         defer {
             c.deinitialize(count: 5)
-            c.deallocate(capacity: 5)
+            c.deallocate()
         }
         let d = UnsafeMutablePointer<UInt64>.allocate(capacity: 5)
-        d.initialize(to: 0, count: 5)
+        d.initialize(repeating: 0, count: 5)
         defer {
             d.deinitialize(count: 5)
-            d.deallocate(capacity: 5)
+            d.deallocate()
         }
 
         for i in 0..<5 {

+ 5 - 5
Sources/CryptoSwift/Utils.swift

@@ -68,18 +68,18 @@ func reversed(_ uint32: UInt32) -> UInt32 {
     return v
 }
 
-func xor<T, V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+func xor<T, V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, V.Element == UInt8, V.Index == Int {
     return xor(left, right).slice
 }
 
-func xor<T, V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+func xor<T, V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, V.Element == UInt8, V.Index == Int {
     let length = Swift.min(left.count, right.count)
 
     let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
-    buf.initialize(to: 0, count: length)
+    buf.initialize(repeating: 0, count: length)
     defer {
-        buf.deinitialize()
-        buf.deallocate(capacity: length)
+        buf.deinitialize(count: length)
+        buf.deallocate()
     }
 
     // xor