Marcin Krzyżanowski 11 år sedan
förälder
incheckning
a6d0bd877a
3 ändrade filer med 10 tillägg och 14 borttagningar
  1. 5 7
      CryptoSwift/MD5.swift
  2. 2 3
      CryptoSwift/SHA1.swift
  3. 3 4
      CryptoSwift/SHA2.swift

+ 5 - 7
CryptoSwift/MD5.swift

@@ -17,7 +17,7 @@ class MD5 : CryptoHashBase {
                        6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]
     
     /** binary integer part of the sines of integers (Radians) */
-    private let K: [UInt32] = [0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
+    private let k: [UInt32] = [0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
                        0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
                        0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
                        0x6b901122,0xfd987193,0xa679438e,0x49b40821,
@@ -38,7 +38,6 @@ class MD5 : CryptoHashBase {
     
     func calculate() -> NSData {
         var tmpMessage = prepare()
-        let wordSize = sizeof(UInt32)
 
         // hash values
         var hh = h
@@ -57,8 +56,7 @@ class MD5 : CryptoHashBase {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
             var M:[UInt32] = [UInt32](count: 16, repeatedValue: 0)
             for x in 0..<M.count {
-                var range = NSRange(location:x * wordSize, length: wordSize)
-                chunk.getBytes(&M[x], range:range);
+                chunk.getBytes(&M[x], range:NSRange(location:x * sizeofValue(M[x]), length: sizeofValue(M[x])));
             }
             
             // Initialize hash value for this chunk:
@@ -70,7 +68,7 @@ class MD5 : CryptoHashBase {
             var dTemp:UInt32 = 0
             
             // Main loop
-            for j in 0...63 {
+            for j in 0..<k.count {
                 var g = 0
                 var F:UInt32 = 0
                 
@@ -97,7 +95,7 @@ class MD5 : CryptoHashBase {
                 dTemp = D
                 D = C
                 C = B
-                B = B &+ rotateLeft((A &+ F &+ K[j] &+ M[g]), s[j])
+                B = B &+ rotateLeft((A &+ F &+ k[j] &+ M[g]), s[j])
                 A = dTemp    
             }
             
@@ -110,7 +108,7 @@ class MD5 : CryptoHashBase {
         var buf: NSMutableData = NSMutableData();
         hh.map({ (item) -> () in
             var i:UInt32 = item.littleEndian
-            buf.appendBytes(&i, length: sizeof(UInt32))
+            buf.appendBytes(&i, length: sizeofValue(i))
         })
         
         return buf.copy() as NSData;

+ 2 - 3
CryptoSwift/SHA1.swift

@@ -14,7 +14,6 @@ class SHA1 : CryptoHashBase {
         
     func calculate() -> NSData {
         var tmpMessage = self.prepare()
-        let wordSize = sizeof(UInt32)
         
         // hash values
         var hh = h
@@ -34,7 +33,7 @@ class SHA1 : CryptoHashBase {
                 switch (x) {
                 case 0...15:
                     var le:UInt32 = 0
-                    chunk.getBytes(&le, range:NSRange(location:x * wordSize, length: wordSize));
+                    chunk.getBytes(&le, range:NSRange(location:x * sizeofValue(M[x]), length: sizeofValue(M[x])));
                     M[x] = le.bigEndian
                     break
                 default:
@@ -95,7 +94,7 @@ class SHA1 : CryptoHashBase {
         var buf: NSMutableData = NSMutableData();
         hh.map({ (item) -> () in
             var i:UInt32 = item.bigEndian
-            buf.appendBytes(&i, length: sizeof(UInt32))
+            buf.appendBytes(&i, length: sizeofValue(i))
         })
         
         return buf.copy() as NSData;

+ 3 - 4
CryptoSwift/SHA2.swift

@@ -55,7 +55,6 @@ class SHA2 : CryptoHashBase {
     
     func calculate(variant: SHA2.variant) -> NSData {
         var tmpMessage = self.prepare()
-        let wordSize = sizeof(UInt32)
         
         // hash values
         var hh = variant.h()
@@ -75,7 +74,7 @@ class SHA2 : CryptoHashBase {
                 switch (x) {
                 case 0...15:
                     var le:UInt32 = 0
-                    chunk.getBytes(&le, range:NSRange(location:x * wordSize, length: wordSize));
+                    chunk.getBytes(&le, range:NSRange(location:x * sizeofValue(le), length: sizeofValue(le)));
                     M[x] = le.bigEndian
                     break
                 default:
@@ -96,7 +95,7 @@ class SHA2 : CryptoHashBase {
             var H = hh[7]
             
             // Main loop
-            for j in 0...63 {
+            for j in 0..<variant.k().count {
                 let s0 = rotateRight(A,2) ^ rotateRight(A,13) ^ rotateRight(A,22)
                 let maj = (A & B) ^ (A & C) ^ (B & C)
                 let t2 = s0 &+ maj
@@ -129,7 +128,7 @@ class SHA2 : CryptoHashBase {
         
         variant.resultingArray(hh).map({ (item) -> () in
             var i:UInt32 = item.bigEndian
-            buf.appendBytes(&i, length: sizeof(UInt32))
+            buf.appendBytes(&i, length: sizeofValue(i))
         })
         
         return buf.copy() as NSData;