Kaynağa Gözat

Update project to Swift 4 mode

Marcin Krzyżanowski 8 yıl önce
ebeveyn
işleme
6945d12958

+ 0 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -59,7 +59,6 @@
 		75EC529E1EE8B8230048EB3B /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525C1EE8B6CA0048EB3B /* Generics.swift */; };
 		75EC529E1EE8B8230048EB3B /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525C1EE8B6CA0048EB3B /* Generics.swift */; };
 		75EC529F1EE8B8230048EB3B /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525D1EE8B6CA0048EB3B /* HMAC.swift */; };
 		75EC529F1EE8B8230048EB3B /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525D1EE8B6CA0048EB3B /* HMAC.swift */; };
 		75EC52A01EE8B8290048EB3B /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */; };
 		75EC52A01EE8B8290048EB3B /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */; };
-		75EC52A11EE8B8290048EB3B /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52601EE8B6CA0048EB3B /* IntegerConvertible.swift */; };
 		75EC52A21EE8B8290048EB3B /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52611EE8B6CA0048EB3B /* MD5.swift */; };
 		75EC52A21EE8B8290048EB3B /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52611EE8B6CA0048EB3B /* MD5.swift */; };
 		75EC52A31EE8B8290048EB3B /* NoPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52621EE8B6CA0048EB3B /* NoPadding.swift */; };
 		75EC52A31EE8B8290048EB3B /* NoPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52621EE8B6CA0048EB3B /* NoPadding.swift */; };
 		75EC52A41EE8B8290048EB3B /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52631EE8B6CA0048EB3B /* Operators.swift */; };
 		75EC52A41EE8B8290048EB3B /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52631EE8B6CA0048EB3B /* Operators.swift */; };
@@ -206,7 +205,6 @@
 		75EC525C1EE8B6CA0048EB3B /* Generics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Generics.swift; sourceTree = "<group>"; };
 		75EC525C1EE8B6CA0048EB3B /* Generics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Generics.swift; sourceTree = "<group>"; };
 		75EC525D1EE8B6CA0048EB3B /* HMAC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HMAC.swift; sourceTree = "<group>"; };
 		75EC525D1EE8B6CA0048EB3B /* HMAC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HMAC.swift; sourceTree = "<group>"; };
 		75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extension.swift"; sourceTree = "<group>"; };
 		75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extension.swift"; sourceTree = "<group>"; };
-		75EC52601EE8B6CA0048EB3B /* IntegerConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegerConvertible.swift; sourceTree = "<group>"; };
 		75EC52611EE8B6CA0048EB3B /* MD5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MD5.swift; sourceTree = "<group>"; };
 		75EC52611EE8B6CA0048EB3B /* MD5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MD5.swift; sourceTree = "<group>"; };
 		75EC52621EE8B6CA0048EB3B /* NoPadding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoPadding.swift; sourceTree = "<group>"; };
 		75EC52621EE8B6CA0048EB3B /* NoPadding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoPadding.swift; sourceTree = "<group>"; };
 		75EC52631EE8B6CA0048EB3B /* Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
 		75EC52631EE8B6CA0048EB3B /* Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
@@ -346,7 +344,6 @@
 				75EC525C1EE8B6CA0048EB3B /* Generics.swift */,
 				75EC525C1EE8B6CA0048EB3B /* Generics.swift */,
 				75EC525D1EE8B6CA0048EB3B /* HMAC.swift */,
 				75EC525D1EE8B6CA0048EB3B /* HMAC.swift */,
 				75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */,
 				75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */,
-				75EC52601EE8B6CA0048EB3B /* IntegerConvertible.swift */,
 				75EC52611EE8B6CA0048EB3B /* MD5.swift */,
 				75EC52611EE8B6CA0048EB3B /* MD5.swift */,
 				75EC52621EE8B6CA0048EB3B /* NoPadding.swift */,
 				75EC52621EE8B6CA0048EB3B /* NoPadding.swift */,
 				75EC52631EE8B6CA0048EB3B /* Operators.swift */,
 				75EC52631EE8B6CA0048EB3B /* Operators.swift */,
@@ -553,7 +550,6 @@
 				75EC52811EE8B8130048EB3B /* BlockCipher.swift in Sources */,
 				75EC52811EE8B8130048EB3B /* BlockCipher.swift in Sources */,
 				75EC52941EE8B81A0048EB3B /* DigestType.swift in Sources */,
 				75EC52941EE8B81A0048EB3B /* DigestType.swift in Sources */,
 				75EC529B1EE8B8200048EB3B /* Rabbit+Foundation.swift in Sources */,
 				75EC529B1EE8B8200048EB3B /* Rabbit+Foundation.swift in Sources */,
-				75EC52A11EE8B8290048EB3B /* IntegerConvertible.swift in Sources */,
 				75EC52A61EE8B8390048EB3B /* PBKDF1.swift in Sources */,
 				75EC52A61EE8B8390048EB3B /* PBKDF1.swift in Sources */,
 				75EC52B41EE8B83D0048EB3B /* UInt32+Extension.swift in Sources */,
 				75EC52B41EE8B83D0048EB3B /* UInt32+Extension.swift in Sources */,
 				75EC52911EE8B81A0048EB3B /* Cryptors.swift in Sources */,
 				75EC52911EE8B81A0048EB3B /* Cryptors.swift in Sources */,

+ 4 - 4
Sources/CryptoSwift/Array+Extension.swift

@@ -38,7 +38,7 @@ extension Array {
     }
     }
 }
 }
 
 
-extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
+extension Array where Element == UInt8 {
     
     
     public init(hex: String) {
     public init(hex: String) {
         self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
         self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
@@ -67,14 +67,14 @@ extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
                 return
                 return
             }
             }
             if let b = buffer {
             if let b = buffer {
-                self.append(b << 4 | v as! Element)
+                self.append(b << 4 | v)
                 buffer = nil
                 buffer = nil
             } else {
             } else {
                 buffer = v
                 buffer = v
             }
             }
         }
         }
-        if let b = buffer{
-            self.append(b as! Element)
+        if let b = buffer {
+            self.append(b)
         }
         }
     }
     }
     
     

+ 1 - 1
Sources/CryptoSwift/Collection+Extension.swift

@@ -53,7 +53,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
 
 
     /// Initialize integer from array of bytes. Caution: may be slow!
     /// Initialize integer from array of bytes. Caution: may be slow!
     @available(*, deprecated: 0.6.0, message: "Dont use it. Too generic to be fast")
     @available(*, deprecated: 0.6.0, message: "Dont use it. Too generic to be fast")
-    func toInteger<T: Integer>() -> T where T: ByteConvertible, T: BitshiftOperationsType {
+    func toInteger<T>() -> T where T: FixedWidthInteger {
         if self.count == 0 {
         if self.count == 0 {
             return 0
             return 0
         }
         }

+ 7 - 16
Sources/CryptoSwift/Generics.swift

@@ -14,26 +14,13 @@
 //  - This notice may not be removed or altered from any source or binary distribution.
 //  - This notice may not be removed or altered from any source or binary distribution.
 //
 //
 
 
-/// Protocol and extensions for integerFrom(bits:). Bit hakish for me, but I can't do it in any other way */
-protocol Initiable {
-    init(_ v: Int)
-    init(_ v: UInt)
-}
-
-extension Int: Initiable {}
-extension UInt: Initiable {}
-extension UInt8: Initiable {}
-extension UInt16: Initiable {}
-extension UInt32: Initiable {}
-extension UInt64: Initiable {}
-
 /** build bit pattern from array of bits */
 /** build bit pattern from array of bits */
 @_specialize(where T == UInt8)
 @_specialize(where T == UInt8)
-func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T {
+func integerFrom<T: FixedWidthInteger>(_ bits: Array<Bit>) -> T {
     var bitPattern: T = 0
     var bitPattern: T = 0
     for idx in bits.indices {
     for idx in bits.indices {
         if bits[idx] == Bit.one {
         if bits[idx] == Bit.one {
-            let bit = T(UIntMax(1) << UIntMax(idx))
+            let bit = T(UInt64(1) << UInt64(idx))
             bitPattern = bitPattern | bit
             bitPattern = bitPattern | bit
         }
         }
     }
     }
@@ -46,7 +33,11 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T {
 /// - parameter length: length of output array. By default size of value type
 /// - parameter length: length of output array. By default size of value type
 ///
 ///
 /// - returns: Array of bytes
 /// - returns: Array of bytes
-func arrayOfBytes<T: Integer>(value: T, length totalBytes: Int = MemoryLayout<T>.size) -> Array<UInt8> {
+@_specialize(where T == Int)
+@_specialize(where T == UInt16)
+@_specialize(where T == UInt32)
+@_specialize(where T == UInt64)
+func arrayOfBytes<T: FixedWidthInteger>(value: T, length totalBytes: Int = MemoryLayout<T>.size) -> Array<UInt8> {
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     valuePointer.pointee = value
     valuePointer.pointee = value
 
 

+ 0 - 51
Sources/CryptoSwift/IntegerConvertible.swift

@@ -1,51 +0,0 @@
-//
-//  IntegerConvertible.swift
-//  CryptoSwift
-//
-//  Copyright (C) 2014-2017 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.
-//
-
-protocol BitshiftOperationsType {
-    static func <<(lhs: Self, rhs: Self) -> Self
-    static func >>(lhs: Self, rhs: Self) -> Self
-    static func <<=(lhs: inout Self, rhs: Self)
-    static func >>=(lhs: inout Self, rhs: Self)
-}
-
-protocol ByteConvertible {
-    init(_ value: UInt8)
-    init(truncatingBitPattern: UInt64)
-}
-
-extension Int: BitshiftOperationsType, ByteConvertible {}
-extension Int8: BitshiftOperationsType, ByteConvertible {}
-extension Int16: BitshiftOperationsType, ByteConvertible {}
-extension Int32: BitshiftOperationsType, ByteConvertible {}
-
-extension Int64: BitshiftOperationsType, ByteConvertible {
-
-    init(truncatingBitPattern value: UInt64) {
-        self = Int64(bitPattern: value)
-    }
-}
-
-extension UInt: BitshiftOperationsType, ByteConvertible {}
-extension UInt8: BitshiftOperationsType, ByteConvertible {}
-extension UInt16: BitshiftOperationsType, ByteConvertible {}
-extension UInt32: BitshiftOperationsType, ByteConvertible {}
-
-extension UInt64: BitshiftOperationsType, ByteConvertible {
-
-    init(truncatingBitPattern value: UInt64) {
-        self = value
-    }
-}

+ 4 - 2
Sources/CryptoSwift/MD5.swift

@@ -101,8 +101,10 @@ public final class MD5: DigestType {
 
 
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 and get M[g] value
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 and get M[g] value
             let gAdvanced = g << 2
             let gAdvanced = g << 2
-            var Mg = UInt32(chunk[chunk.startIndex &+ gAdvanced]) | UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 1]) << 8 | UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 2]) << 16
-                Mg = Mg | UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 3]) << 24
+            var Mg  = UInt32(chunk[chunk.startIndex &+ gAdvanced])
+                Mg |= UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 1]) << 8
+                Mg |= UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 2]) << 16
+                Mg |= UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 3]) << 24
 
 
             B = B &+ rotateLeft(A &+ F &+ k[j] &+ Mg, by: s[j])
             B = B &+ rotateLeft(A &+ F &+ k[j] &+ Mg, by: s[j])
             A = dTemp
             A = dTemp