Browse Source

Merge pull request #467 from adolfo/fix/reduce-expression-complexity

Reduce expression complexity to appease compiler [Xcode 9]
Marcin Krzyzanowski 8 năm trước cách đây
mục cha
commit
58e1f6c588
2 tập tin đã thay đổi với 14 bổ sung15 xóa
  1. 8 13
      Sources/CryptoSwift/AES.swift
  2. 6 2
      Sources/CryptoSwift/MD5.swift

+ 8 - 13
Sources/CryptoSwift/AES.swift

@@ -307,19 +307,14 @@ fileprivate extension AES {
         var rk2: Array<Array<UInt32>> = expandKey(key, variant: variant)
 
         for r in 1 ..< rounds {
-            var w: UInt32
-
-            w = rk2[r][0]
-            rk2[r][0] = U1[Int(B0(w))] ^ U2[Int(B1(w))] ^ U3[Int(B2(w))] ^ U4[Int(B3(w))]
-
-            w = rk2[r][1]
-            rk2[r][1] = U1[Int(B0(w))] ^ U2[Int(B1(w))] ^ U3[Int(B2(w))] ^ U4[Int(B3(w))]
-
-            w = rk2[r][2]
-            rk2[r][2] = U1[Int(B0(w))] ^ U2[Int(B1(w))] ^ U3[Int(B2(w))] ^ U4[Int(B3(w))]
-
-            w = rk2[r][3]
-            rk2[r][3] = U1[Int(B0(w))] ^ U2[Int(B1(w))] ^ U3[Int(B2(w))] ^ U4[Int(B3(w))]
+            for i in 0..<4 {
+                let w = rk2[r][i]
+                let u1 = U1[Int(B0(w))]
+                let u2 = U2[Int(B1(w))]
+                let u3 = U3[Int(B2(w))]
+                let u4 = U4[Int(B3(w))]
+                rk2[r][i] = u1^u2^u3^u4
+            }
         }
 
         return rk2

+ 6 - 2
Sources/CryptoSwift/MD5.swift

@@ -101,8 +101,12 @@ 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
-            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
+
+            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
 
             B = B &+ rotateLeft(A &+ F &+ k[j] &+ Mg, by: s[j])
             A = dTemp