Browse Source

Merge branch 'swift4'

Marcin Krzyżanowski 8 năm trước cách đây
mục cha
commit
ed38c2bb94

+ 7 - 0
CryptoSwift.playground/playground.xcworkspace/contents.xcworkspacedata

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+   version = "1.0">
+   <FileRef
+      location = "self:">
+   </FileRef>
+</Workspace>

+ 3 - 3
CryptoSwift.podspec

@@ -1,11 +1,11 @@
 Pod::Spec.new do |s|
   s.name         = "CryptoSwift"
-  s.version      = "0.7.0"
+  s.version      = "0.7.1"
+  s.source       = { :git => "https://github.com/krzyzanowskim/CryptoSwift.git", :tag => "#{s.version}" }
   s.summary      = "Cryptography in Swift. SHA, MD5, CRC, PBKDF, Poly1305, HMAC, ChaCha20, Rabbit, Blowfish, AES."
   s.description  = "Cryptography functions and helpers for Swift implemented in Swift. SHA, MD5, PBKDF1, PBKDF2, CRC, Poly1305, HMAC, ChaCha20, Rabbit, Blowfish, AES."
   s.homepage     = "https://github.com/krzyzanowskim/CryptoSwift"
   s.license      = {:type => "Attribution License", :file => "LICENSE"}
-  s.source       = { :git => "https://github.com/krzyzanowskim/CryptoSwift.git", :tag => "#{s.version}" }
   s.authors      = {'Marcin Krzyżanowski' => 'marcin@krzyzanowskim.com'}
   s.social_media_url   = "https://twitter.com/krzyzanowskim"
   s.ios.deployment_target = "8.0"
@@ -13,6 +13,6 @@ Pod::Spec.new do |s|
   s.watchos.deployment_target = "2.0"
   s.tvos.deployment_target = "9.0"
   s.source_files  = "Sources/CryptoSwift/**/*.swift"
-  s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0', 'SWIFT_OPTIMIZATION_LEVEL' => '-Owholemodule' }
   s.requires_arc = true
+  s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0', 'SWIFT_OPTIMIZATION_LEVEL' => '-Owholemodule' }
 end

+ 4 - 12
CryptoSwift.xcodeproj/project.pbxproj

@@ -3,7 +3,7 @@
 	archiveVersion = 1;
 	classes = {
 	};
-	objectVersion = 46;
+	objectVersion = 48;
 	objects = {
 
 /* Begin PBXBuildFile section */
@@ -59,7 +59,6 @@
 		75EC529E1EE8B8230048EB3B /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC525C1EE8B6CA0048EB3B /* Generics.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 */; };
-		75EC52A11EE8B8290048EB3B /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52601EE8B6CA0048EB3B /* IntegerConvertible.swift */; };
 		75EC52A21EE8B8290048EB3B /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52611EE8B6CA0048EB3B /* MD5.swift */; };
 		75EC52A31EE8B8290048EB3B /* NoPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52621EE8B6CA0048EB3B /* NoPadding.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>"; };
 		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>"; };
-		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>"; };
 		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>"; };
@@ -346,7 +344,6 @@
 				75EC525C1EE8B6CA0048EB3B /* Generics.swift */,
 				75EC525D1EE8B6CA0048EB3B /* HMAC.swift */,
 				75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */,
-				75EC52601EE8B6CA0048EB3B /* IntegerConvertible.swift */,
 				75EC52611EE8B6CA0048EB3B /* MD5.swift */,
 				75EC52621EE8B6CA0048EB3B /* NoPadding.swift */,
 				75EC52631EE8B6CA0048EB3B /* Operators.swift */,
@@ -495,7 +492,7 @@
 				};
 			};
 			buildConfigurationList = 754BE44F19693E190098E6F3 /* Build configuration list for PBXProject "CryptoSwift" */;
-			compatibilityVersion = "Xcode 3.2";
+			compatibilityVersion = "Xcode 8.0";
 			developmentRegion = English;
 			hasScannedForEncodings = 0;
 			knownRegions = (
@@ -553,7 +550,6 @@
 				75EC52811EE8B8130048EB3B /* BlockCipher.swift in Sources */,
 				75EC52941EE8B81A0048EB3B /* DigestType.swift in Sources */,
 				75EC529B1EE8B8200048EB3B /* Rabbit+Foundation.swift in Sources */,
-				75EC52A11EE8B8290048EB3B /* IntegerConvertible.swift in Sources */,
 				75EC52A61EE8B8390048EB3B /* PBKDF1.swift in Sources */,
 				75EC52B41EE8B83D0048EB3B /* UInt32+Extension.swift in Sources */,
 				75EC52911EE8B81A0048EB3B /* Cryptors.swift in Sources */,
@@ -715,7 +711,7 @@
 				PRODUCT_NAME = CryptoSwift;
 				SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvos watchos appletvsimulator watchsimulator";
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
-				SWIFT_VERSION = 3.0;
+				SWIFT_VERSION = 4.0;
 				TARGETED_DEVICE_FAMILY = "1,2,3,4";
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 				VERSIONING_SYSTEM = "apple-generic";
@@ -769,7 +765,7 @@
 				METAL_ENABLE_DEBUG_INFO = NO;
 				PRODUCT_NAME = CryptoSwift;
 				SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx appletvos watchos appletvsimulator watchsimulator";
-				SWIFT_VERSION = 3.0;
+				SWIFT_VERSION = 4.0;
 				TARGETED_DEVICE_FAMILY = "1,2,3,4";
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 				VALIDATE_PRODUCT = YES;
@@ -796,7 +792,6 @@
 				PRODUCT_NAME = CryptoSwift;
 				SKIP_INSTALL = YES;
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
-				SWIFT_VERSION = 3.0;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 				WATCHOS_DEPLOYMENT_TARGET = 2.0;
 			};
@@ -824,7 +819,6 @@
 				SKIP_INSTALL = YES;
 				SWIFT_DISABLE_SAFETY_CHECKS = YES;
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
-				SWIFT_VERSION = 3.0;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 				WATCHOS_DEPLOYMENT_TARGET = 2.0;
 			};
@@ -850,7 +844,6 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.hakore.${PRODUCT_NAME:rfc1034identifier}";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SWIFT_OBJC_BRIDGING_HEADER = Tests/CryptoSwiftTests/Bridging.h;
-				SWIFT_VERSION = 3.0;
 			};
 			name = Debug;
 		};
@@ -874,7 +867,6 @@
 				SWIFT_DISABLE_SAFETY_CHECKS = YES;
 				SWIFT_OBJC_BRIDGING_HEADER = Tests/CryptoSwiftTests/Bridging.h;
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
-				SWIFT_VERSION = 3.0;
 			};
 			name = Release;
 		};

+ 5 - 0
CryptoSwift.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict/>
+</plist>

+ 2 - 0
CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme

@@ -26,6 +26,7 @@
       buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      language = ""
       shouldUseLaunchSchemeArgsEnv = "YES"
       codeCoverageEnabled = "YES">
       <Testables>
@@ -56,6 +57,7 @@
       buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      language = ""
       launchStyle = "0"
       useCustomWorkingDirectory = "NO"
       ignoresPersistentStateOnLaunch = "NO"

+ 5 - 0
CryptoSwift.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict/>
+</plist>

+ 3 - 3
README.md

@@ -109,9 +109,9 @@ In the project, you'll find [single scheme](http://promisekit.org/news/2016/08/M
 - Swift 1.2: branch [swift12](https://github.com/krzyzanowskim/CryptoSwift/tree/swift12) version <= 0.0.13
 - Swift 2.1: branch [swift21](https://github.com/krzyzanowskim/CryptoSwift/tree/swift21) version <= 0.2.3
 - Swift 2.2, 2.3: branch [swift2](https://github.com/krzyzanowskim/CryptoSwift/tree/swift2) version <= 0.5.2
-- Swift 3.0, 3.1, branch [swift3](https://github.com/krzyzanowskim/CryptoSwift/tree/swift3) version <= 0.6.9
-- Swift 3.2, branch [swift32](https://github.com/krzyzanowskim/CryptoSwift/tree/swift32) version <= 0.7.0
-- Swift 4.0, branch [master](https://github.com/krzyzanowskim/CryptoSwift/tree/swift4)
+- Swift 3.1, branch [swift3](https://github.com/krzyzanowskim/CryptoSwift/tree/swift3) version <= 0.6.9
+- Swift 3.2, branch [swift32](https://github.com/krzyzanowskim/CryptoSwift/tree/swift32) version = 0.7.0
+- Swift 4.0, branch [master](https://github.com/krzyzanowskim/CryptoSwift/tree/master) version >= 0.7.1
 
 #### CocoaPods
 

+ 24 - 9
Sources/CryptoSwift/AES.swift

@@ -150,14 +150,29 @@ fileprivate extension AES {
         let rounds = self.variant.Nr
         let rk = self.expandedKey
 
-        var b0 = UInt32(block[block.startIndex + 0 + (0 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (0 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (0 << 2)]) << 16
-            b0 = b0 | UInt32(block[block.startIndex + 3 + (0 << 2)]) << 24
-        var b1 = UInt32(block[block.startIndex + 0 + (1 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (1 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (1 << 2)]) << 16
-            b1 = b1 | UInt32(block[block.startIndex + 3 + (1 << 2)]) << 24
-        var b2 = UInt32(block[block.startIndex + 0 + (2 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (2 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (2 << 2)]) << 16
-            b2 = b2 | UInt32(block[block.startIndex + 3 + (2 << 2)]) << 24
-        var b3 = UInt32(block[block.startIndex + 0 + (3 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (3 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (3 << 2)]) << 16
-            b3 = b3 | UInt32(block[block.startIndex + 3 + (3 << 2)]) << 24
+        let b00 = UInt32(block[block.startIndex + 0 + (0 << 2)]) << 0
+        let b01 = UInt32(block[block.startIndex + 1 + (0 << 2)]) << 8
+        let b02 = UInt32(block[block.startIndex + 2 + (0 << 2)]) << 16
+        let b03 = UInt32(block[block.startIndex + 3 + (0 << 2)]) << 24
+        var b0 = b00 | b01 | b02 | b03
+
+        let b10 = UInt32(block[block.startIndex + 0 + (1 << 2)]) << 0
+        let b11 = UInt32(block[block.startIndex + 1 + (1 << 2)]) << 8
+        let b12 = UInt32(block[block.startIndex + 2 + (1 << 2)]) << 16
+        let b13 = UInt32(block[block.startIndex + 3 + (1 << 2)]) << 24
+        var b1 = b10 | b11 | b12 | b13
+
+        let b20 = UInt32(block[block.startIndex + 0 + (2 << 2)]) << 0
+        let b21 = UInt32(block[block.startIndex + 1 + (2 << 2)]) << 8
+        let b22 = UInt32(block[block.startIndex + 2 + (2 << 2)]) << 16
+        let b23 = UInt32(block[block.startIndex + 3 + (2 << 2)]) << 24
+        var b2 = b20 | b21 | b22 | b23
+
+        let b30 = UInt32(block[block.startIndex + 0 + (3 << 2)]) << 0
+        let b31 = UInt32(block[block.startIndex + 1 + (3 << 2)]) << 8
+        let b32 = UInt32(block[block.startIndex + 2 + (3 << 2)]) << 16
+        let b33 = UInt32(block[block.startIndex + 3 + (3 << 2)]) << 24
+        var b3 = b30 | b31 | b32 | b33
 
         var t = Array<UInt32>(repeating: 0, count: 4)
 
@@ -402,7 +417,7 @@ fileprivate extension AES {
         var p: UInt8 = 1, q: UInt8 = 1
 
         repeat {
-            p = p ^ (UInt8(truncatingBitPattern: Int(p) << 1) ^ ((p & 0x80) == 0x80 ? 0x1B : 0))
+            p = p ^ (UInt8(truncatingIfNeeded: Int(p) << 1) ^ ((p & 0x80) == 0x80 ? 0x1B : 0))
             q ^= q << 1
             q ^= q << 2
             q ^= q << 4

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

+ 5 - 1
Sources/CryptoSwift/Blowfish.swift

@@ -465,7 +465,11 @@ public final class Blowfish {
     }
 
     private func F(x: UInt32) -> UInt32 {
-        return ((self.S[0][Int(x >> 24) & 0xFF] &+ self.S[1][Int(x >> 16) & 0xFF]) ^ self.S[2][Int(x >> 8) & 0xFF]) &+ self.S[3][Int(x & 0xFF)]
+        let f1 = self.S[0][Int(x >> 24) & 0xFF];
+        let f2 = self.S[1][Int(x >> 16) & 0xFF];
+        let f3 = self.S[2][Int(x >> 8) & 0xFF];
+        let f4 = self.S[3][Int(x & 0xFF)]
+        return ((f1 &+ f2) ^ f3) &+ f4
     }
 }
 

+ 2 - 2
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!
     @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 {
             return 0
         }
@@ -68,7 +68,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
         }
 
         if size == 1 {
-            return T(truncatingBitPattern: UInt64(bytes[0]))
+            return T(truncatingIfNeeded: UInt64(bytes[0]))
         }
 
         var result: T = 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.
 //
 
-/// 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 */
 @_specialize(where T == UInt8)
-func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T {
+func integerFrom<T: FixedWidthInteger>(_ bits: Array<Bit>) -> T {
     var bitPattern: T = 0
     for idx in bits.indices {
         if bits[idx] == Bit.one {
-            let bit = T(UIntMax(1) << UIntMax(idx))
+            let bit = T(UInt64(1) << UInt64(idx))
             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
 ///
 /// - 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)
     valuePointer.pointee = value
 

+ 1 - 1
Sources/CryptoSwift/Info.plist

@@ -15,7 +15,7 @@
 	<key>CFBundlePackageType</key>
 	<string>FMWK</string>
 	<key>CFBundleShortVersionString</key>
-	<string>0.7.0</string>
+	<string>0.7.1</string>
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<key>CFBundleVersion</key>

+ 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 - 5
Sources/CryptoSwift/MD5.swift

@@ -102,11 +102,10 @@ public final class MD5: DigestType {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 and get M[g] value
             let gAdvanced = g << 2
 
-            let Mg0 = UInt32(chunk[chunk.startIndex &+ gAdvanced])
-            let Mg1 = UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 1]) << 8
-            let Mg2 = UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 2]) << 16
-            let Mg3 = UInt32(chunk[chunk.startIndex &+ gAdvanced &+ 3]) << 24
-            let Mg = (Mg0 | Mg1 | Mg2) | Mg3
+            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])
             A = dTemp

+ 11 - 11
Sources/CryptoSwift/Rabbit.swift

@@ -160,26 +160,26 @@ public final class Rabbit: BlockCipher {
     private func g(_ j: Int) -> UInt32 {
         let sum = x[j] &+ c[j]
         let square = UInt64(sum) * UInt64(sum)
-        return UInt32(truncatingBitPattern: square ^ (square >> 32))
+        return UInt32.init(truncatingIfNeeded: square ^ (square >> 32))
     }
 
     fileprivate func nextOutput() -> Array<UInt8> {
         nextState()
 
         var output16 = [UInt16](repeating: 0, count: Rabbit.blockSize / 2)
-        output16[7] = UInt16(truncatingBitPattern: x[0]) ^ UInt16(truncatingBitPattern: x[5] >> 16)
-        output16[6] = UInt16(truncatingBitPattern: x[0] >> 16) ^ UInt16(truncatingBitPattern: x[3])
-        output16[5] = UInt16(truncatingBitPattern: x[2]) ^ UInt16(truncatingBitPattern: x[7] >> 16)
-        output16[4] = UInt16(truncatingBitPattern: x[2] >> 16) ^ UInt16(truncatingBitPattern: x[5])
-        output16[3] = UInt16(truncatingBitPattern: x[4]) ^ UInt16(truncatingBitPattern: x[1] >> 16)
-        output16[2] = UInt16(truncatingBitPattern: x[4] >> 16) ^ UInt16(truncatingBitPattern: x[7])
-        output16[1] = UInt16(truncatingBitPattern: x[6]) ^ UInt16(truncatingBitPattern: x[3] >> 16)
-        output16[0] = UInt16(truncatingBitPattern: x[6] >> 16) ^ UInt16(truncatingBitPattern: x[1])
+        output16[7] = UInt16(truncatingIfNeeded: x[0]) ^ UInt16(truncatingIfNeeded: x[5] >> 16)
+        output16[6] = UInt16(truncatingIfNeeded: x[0] >> 16) ^ UInt16(truncatingIfNeeded: x[3])
+        output16[5] = UInt16(truncatingIfNeeded: x[2]) ^ UInt16(truncatingIfNeeded: x[7] >> 16)
+        output16[4] = UInt16(truncatingIfNeeded: x[2] >> 16) ^ UInt16(truncatingIfNeeded: x[5])
+        output16[3] = UInt16(truncatingIfNeeded: x[4]) ^ UInt16(truncatingIfNeeded: x[1] >> 16)
+        output16[2] = UInt16(truncatingIfNeeded: x[4] >> 16) ^ UInt16(truncatingIfNeeded: x[7])
+        output16[1] = UInt16(truncatingIfNeeded: x[6]) ^ UInt16(truncatingIfNeeded: x[3] >> 16)
+        output16[0] = UInt16(truncatingIfNeeded: x[6] >> 16) ^ UInt16(truncatingIfNeeded: x[1])
 
         var output8 = Array<UInt8>(repeating: 0, count: Rabbit.blockSize)
         for j in 0 ..< output16.count {
-            output8[j * 2] = UInt8(truncatingBitPattern: output16[j] >> 8)
-            output8[j * 2 + 1] = UInt8(truncatingBitPattern: output16[j])
+            output8[j * 2] = UInt8(truncatingIfNeeded: output16[j] >> 8)
+            output8[j * 2 + 1] = UInt8(truncatingIfNeeded: output16[j])
         }
         return output8
     }

+ 2 - 2
Tests/CryptoSwiftTests/AESTests.swift

@@ -356,7 +356,7 @@ final class AESTests: XCTestCase {
             let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
             let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
             let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
-            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+            measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
                 _ = try! aes.encrypt(message)
             })
         }
@@ -366,7 +366,7 @@ final class AESTests: XCTestCase {
             let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
             let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
             let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
-            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+            measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
                 _ = try! aes.decrypt(message)
             })
         }

+ 1 - 1
Tests/CryptoSwiftTests/ChaCha20Tests.swift

@@ -109,7 +109,7 @@ final class ChaCha20Tests: XCTestCase {
             let key: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F]
             let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
             let message = Array<UInt8>(repeating: 7, count: 1024)
-            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+            measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
                 do {
                     let _ = try ChaCha20(key: key, iv: iv).encrypt(message)
                 } catch {

+ 1 - 1
Tests/CryptoSwiftTests/DigestTests.swift

@@ -166,7 +166,7 @@ final class DigestTests: XCTestCase {
     extension DigestTests {
 
         func testMD5Performance() {
-            self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+            self.measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
                 let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
                 self.startMeasuring()
                 _ = Digest.md5(arr)

+ 1 - 1
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -93,7 +93,7 @@ final class ExtensionsTest: XCTestCase {
     extension ExtensionsTest {
 
         func testArrayChunksPerformance() {
-            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+            measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
                 let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
                 self.startMeasuring()
                 _ = message.chunks(size: AES.blockSize)

+ 1 - 1
Tests/CryptoSwiftTests/PBKDF.swift

@@ -56,7 +56,7 @@ class PBKDF: XCTestCase {
     func testPerformance() {
         let password: Array<UInt8> = Array("s33krit".utf8)
         let salt: Array<UInt8> = Array("nacl".utf8)
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+        measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
             let _ = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 65536, keyLength: 32, variant: .sha1).calculate()
         })
     }

+ 1 - 1
Tests/CryptoSwiftTests/RabbitTests.swift

@@ -118,7 +118,7 @@ class RabbitTests: XCTestCase {
             let key: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
             let iv: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
             let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
-            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+            measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
                 let encrypted = try! Rabbit(key: key, iv: iv).encrypt(message)
                 self.stopMeasuring()
                 XCTAssert(!encrypted.isEmpty, "not encrypted")