瀏覽代碼

while -> for loop for readability. xor() -> Utils

Marcin Krzyżanowski 10 年之前
父節點
當前提交
7ea6297f44
共有 3 個文件被更改,包括 13 次插入15 次删除
  1. 5 6
      CryptoSwift/AES.swift
  2. 0 9
      CryptoSwift/CipherBlockMode.swift
  3. 8 0
      CryptoSwift/Utils.swift

+ 5 - 6
CryptoSwift/AES.swift

@@ -255,16 +255,17 @@ public class AES {
             return result
         }
 
-        var w:[UInt8] = [UInt8](count: variant.Nb * (variant.Nr + 1) * 4, repeatedValue: 0)
+        var w = [UInt8](count: variant.Nb * (variant.Nr + 1) * 4, repeatedValue: 0)
         for i in 0..<variant.Nk {
             for wordIdx in 0..<4 {
                 w[(4*i)+wordIdx] = key[(4*i)+wordIdx]
             }
         }
         
-        var i = variant.Nk
-        while (i < variant.Nb * (variant.Nr + 1)) {
-            var tmp:[UInt8] = [UInt8](count: 4, repeatedValue: 0)
+        var tmp = [UInt8](count: 4, repeatedValue: 0)
+        for (var i = variant.Nk; i < variant.Nb * (variant.Nr + 1); i++) {
+            tmp = tmp.map({ val in 0});
+            
             for wordIdx in 0..<4 {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
@@ -280,8 +281,6 @@ public class AES {
             for wordIdx in 0..<4 {
                 w[4*i+wordIdx] = w[4*(i-variant.Nk)+wordIdx]^tmp[wordIdx];
             }
-            
-            i++
         }
         return w;
     }

+ 0 - 9
CryptoSwift/CipherBlockMode.swift

@@ -159,12 +159,3 @@ private struct ECBMode: BlockMode {
         return encryptBlocks(blocks, iv: iv, cipherOperation: cipherOperation)
     }
 }
-
-//MARK: helpers
-private func xor(a: [UInt8], b:[UInt8]) -> [UInt8] {
-    var xored = [UInt8](count: a.count, repeatedValue: 0)
-    for i in 0..<xored.count {
-        xored[i] = a[i] ^ b[i]
-    }
-    return xored
-}

+ 8 - 0
CryptoSwift/Utils.swift

@@ -40,4 +40,12 @@ func reverseBytes(value: UInt32) -> UInt32 {
     var tmp1 = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8)
     var tmp2 = ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24)
     return tmp1 | tmp2
+}
+
+func xor(a: [UInt8], b:[UInt8]) -> [UInt8] {
+    var xored = [UInt8](count: a.count, repeatedValue: 0)
+    for i in 0..<xored.count {
+        xored[i] = a[i] ^ b[i]
+    }
+    return xored
 }