Forráskód Böngészése

Refactoring for hashes. Update for 6.3 beta3.

Marcin Krzyżanowski 10 éve
szülő
commit
c7851c7ad4

+ 8 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -20,7 +20,7 @@
 		754BE46819693E190098E6F3 /* HashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754BE46719693E190098E6F3 /* HashTests.swift */; };
 		754C30B71AA13BC000E6FFA4 /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754C30B61AA13BC000E6FFA4 /* PKCS7.swift */; };
 		754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754C8FEC19979F94005AD904 /* ArrayExtension.swift */; };
-		754DD76E19A149AF00E52288 /* CryptoHashBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754DD76D19A149AF00E52288 /* CryptoHashBase.swift */; };
+		754DD76E19A149AF00E52288 /* HashBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754DD76D19A149AF00E52288 /* HashBase.swift */; };
 		755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755111E719B7B7DF00C2AD86 /* Authenticator.swift */; };
 		7552614E1993051E000D2B20 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7552614D1993051E000D2B20 /* Hash.swift */; };
 		755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755FB1D9199E347D00475437 /* ExtensionsTest.swift */; };
@@ -41,6 +41,7 @@
 		759D481119B517BC005FF7FC /* BitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759D481019B517BC005FF7FC /* BitExtension.swift */; };
 		75A663A61AA0CAD00052110B /* Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75A663A51AA0CAD00052110B /* Padding.swift */; };
 		75A74B271A1FF6B2004419F1 /* AES.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75A74B261A1FF6B2004419F1 /* AES.swift */; };
+		75B0A5701AB1A1BB000BD8D2 /* PKCS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */; };
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
 		75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */; };
 		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
@@ -131,7 +132,7 @@
 		754BE46719693E190098E6F3 /* HashTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashTests.swift; sourceTree = "<group>"; };
 		754C30B61AA13BC000E6FFA4 /* PKCS7.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKCS7.swift; sourceTree = "<group>"; };
 		754C8FEC19979F94005AD904 /* ArrayExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayExtension.swift; sourceTree = "<group>"; };
-		754DD76D19A149AF00E52288 /* CryptoHashBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CryptoHashBase.swift; sourceTree = "<group>"; };
+		754DD76D19A149AF00E52288 /* HashBase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HashBase.swift; sourceTree = "<group>"; };
 		755111E719B7B7DF00C2AD86 /* Authenticator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Authenticator.swift; sourceTree = "<group>"; };
 		7552614D1993051E000D2B20 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Hash.swift; sourceTree = "<group>"; };
 		755FB1D9199E347D00475437 /* ExtensionsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionsTest.swift; sourceTree = "<group>"; };
@@ -152,6 +153,7 @@
 		759D481019B517BC005FF7FC /* BitExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BitExtension.swift; sourceTree = "<group>"; };
 		75A663A51AA0CAD00052110B /* Padding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Padding.swift; sourceTree = "<group>"; };
 		75A74B261A1FF6B2004419F1 /* AES.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AES.swift; sourceTree = "<group>"; };
+		75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKCS5.swift; sourceTree = "<group>"; };
 		75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CipherBlockMode.swift; sourceTree = "<group>"; };
 		75D94E2319B60C08007CB2A4 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
 		75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt32Extension.swift; sourceTree = "<group>"; };
@@ -215,9 +217,10 @@
 				7563B2E719B14D4300B152CD /* Cipher.swift */,
 				75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */,
 				75A663A51AA0CAD00052110B /* Padding.swift */,
+				75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */,
 				754C30B61AA13BC000E6FFA4 /* PKCS7.swift */,
 				755111E719B7B7DF00C2AD86 /* Authenticator.swift */,
-				754DD76D19A149AF00E52288 /* CryptoHashBase.swift */,
+				754DD76D19A149AF00E52288 /* HashBase.swift */,
 				750A545F1992D2680017DA75 /* MD5.swift */,
 				752E087A199FF27C005B0EA0 /* SHA1.swift */,
 				75153D4119AA3C7900750381 /* SHA2.swift */,
@@ -411,12 +414,13 @@
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
 				758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */,
 				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
+				75B0A5701AB1A1BB000BD8D2 /* PKCS5.swift in Sources */,
 				757EF7F519AAA82400586276 /* CRC.swift in Sources */,
 				75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */,
 				75A74B271A1FF6B2004419F1 /* AES.swift in Sources */,
 				754C30B71AA13BC000E6FFA4 /* PKCS7.swift in Sources */,
 				755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */,
-				754DD76E19A149AF00E52288 /* CryptoHashBase.swift in Sources */,
+				754DD76E19A149AF00E52288 /* HashBase.swift in Sources */,
 				758C764119B61AE500653BC6 /* Generics.swift in Sources */,
 				758F3F781992F6CE0014BBDA /* UInt8Extension.swift in Sources */,
 				75153D4219AA3C7900750381 /* SHA2.swift in Sources */,

+ 1 - 1
CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme

@@ -3,7 +3,7 @@
    LastUpgradeVersion = "0610"
    version = "1.3">
    <BuildAction
-      parallelizeBuildables = "YES"
+      parallelizeBuildables = "NO"
       buildImplicitDependencies = "YES">
       <BuildActionEntries>
          <BuildActionEntry

+ 14 - 3
CryptoSwift/HMAC.swift

@@ -13,6 +13,17 @@ public class HMAC {
     public enum Variant {
         case sha1, sha256, md5
         
+        var size:Int {
+            switch (self) {
+            case .sha1:
+                return SHA1(NSData()).size
+            case .sha256:
+                return SHA2.Variant.sha256.size
+            case .md5:
+                return MD5(NSData()).size
+            }
+        }
+        
         func calculateHash(# bytes:[UInt8]) -> [UInt8]? {
             switch (self) {
             case .sha1:
@@ -31,14 +42,14 @@ public class HMAC {
     
     var key:[UInt8]
     let variant:Variant
-        
+    
     class internal func authenticate(# key: [UInt8], message: [UInt8], variant:HMAC.Variant = .md5) -> [UInt8]? {
         return HMAC(key, variant: variant)?.authenticate(message: message)
     }
 
     // MARK: - Private
     
-    private init? (_ key: [UInt8], variant:HMAC.Variant = .md5) {
+    internal init? (_ key: [UInt8], variant:HMAC.Variant = .md5) {
         self.variant = variant
         self.key = key
 
@@ -53,7 +64,7 @@ public class HMAC {
         }
     }
     
-    private func authenticate(# message:[UInt8]) -> [UInt8]? {
+    internal func authenticate(# message:[UInt8]) -> [UInt8]? {
         var opad = [UInt8](count: variant.blockSize(), repeatedValue: 0x5c)
         for (idx, val) in enumerate(key) {
             opad[idx] = key[idx] ^ opad[idx]

+ 4 - 4
CryptoSwift/Hash.swift

@@ -21,13 +21,13 @@ public enum Hash {
         case sha1(let data):
             return SHA1(data).calculate()
         case sha224(let data):
-            return SHA2(data).calculate32(.sha224)
+            return SHA2(data, variant: .sha224).calculate32()
         case sha256(let data):
-            return SHA2(data).calculate32(.sha256)
+            return SHA2(data, variant: .sha256).calculate32()
         case sha384(let data):
-            return SHA2(data).calculate64(.sha384)
+            return SHA2(data, variant: .sha384).calculate64()
         case sha512(let data):
-            return SHA2(data).calculate64(.sha512)
+            return SHA2(data, variant: .sha512).calculate64()
         case crc32(let data):
             return CRC().crc32(data);
         default:

+ 8 - 3
CryptoSwift/CryptoHashBase.swift → CryptoSwift/HashBase.swift

@@ -8,16 +8,21 @@
 
 import Foundation
 
-class HashBase {
+protocol _Hash {
+    var size:Int { get }
+    func prepare(len:Int) -> NSMutableData;
+}
+
+internal class HashBase {
     
     var message: NSData
     
-    init(_ message: NSData) {
+    internal init(_ message: NSData) {
         self.message = message
     }
     
     /** Common part for hash calculation. Prepare header data. */
-    func prepare(_ len:Int = 64) -> NSMutableData {
+    internal func prepare(_ len:Int = 64) -> NSMutableData {
         var tmpMessage: NSMutableData = NSMutableData(data: self.message)
         
         // Step 1. Append Padding Bits

+ 1 - 1
CryptoSwift/IntExtension.swift

@@ -30,7 +30,7 @@ extension Int {
         return arrayOfBytes(self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: Slice<UInt8>) -> Int {
+    public static func withBytes(bytes: ArraySlice<UInt8>) -> Int {
         return Int.withBytes(Array(bytes))
     }
 

+ 3 - 2
CryptoSwift/MD5.swift

@@ -8,8 +8,9 @@
 
 import Foundation
 
-class MD5 : CryptoSwift.HashBase {
-
+class MD5 : CryptoSwift.HashBase, _Hash {
+    var size:Int = 16 // 128 / 8
+    
     /** specifies the per-round shift amounts */
     private let s: [UInt32] = [7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,
                        5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,

+ 52 - 0
CryptoSwift/PKCS5.swift

@@ -0,0 +1,52 @@
+//
+//  PKCS.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 12/03/15.
+//  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
+//
+//  PKCS is a group of public-key cryptography standards devised 
+//  and published by RSA Security Inc, starting in the early 1990s.
+//
+//  PKCS#5 http://tools.ietf.org/html/rfc2898
+
+import Foundation
+
+//public struct PKCS5 {
+//}
+//
+//extension PKCS5 {
+//    //
+//    // PBKDF2 - Password-Based Key Derivation Function 2. Key stretching technique.
+//    //          DK = PBKDF2(PRF, Password, Salt, c, dkLen)
+//    //
+//    struct PBKDF2 {
+//        typealias Bytes = [UInt8]
+//        private func calc(# hash:Hash,  password:Bytes, salt:Bytes, c:Int, dkLen:Int) -> [UInt8]? {
+//            if (dkLen > Int(pow(2,32) as Float - 1)) {
+//                println("ERROR: derived key too long");
+//                return nil
+//            }
+//
+//            if let prf = HMAC(password, variant: .sha256) { //FIXME: hardcoded SHA256
+//                let hLen = prf.variant.size
+//                let numBlocks = Int(ceilf(Float(dkLen) / Float(hLen)))  // l
+//                let lastBlockOctets = dkLen - (1 - numBlocks) * hLen    // r
+//                // blocks
+//                for block in 1...numBlocks {
+//                    // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
+//                    // U_1 = PRF(password, salt || uint(i))
+//                    var uinti = [UInt8](count: 4, repeatedValue: 0)
+//                    uinti[0] = UInt8(block >> 24)
+//                    uinti[1] = UInt8(block >> 16)
+//                    uinti[2] = UInt8(block >> 8)
+//                    uinti[3] = UInt8(block)
+//                    if let dk = prf.authenticate(message: salt + uinti) {
+//                        let T = dk[dk.count - hLen]
+//                    }
+//                }
+//            }
+//            return nil
+//        }
+//    }
+//}

+ 3 - 0
CryptoSwift/PKCS7.swift

@@ -5,6 +5,9 @@
 //  Created by Marcin Krzyzanowski on 28/02/15.
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
+//  PKCS is a group of public-key cryptography standards devised
+//  and published by RSA Security Inc, starting in the early 1990s.
+//
 
 import Foundation
 

+ 2 - 0
CryptoSwift/Playground/CryptoPlayground.playground/contents.swift

@@ -0,0 +1,2 @@
+import Foundation
+import CryptoSwift

+ 1 - 4
CryptoSwift/Playground/CryptoPlayground.playground/contents.xcplayground

@@ -1,7 +1,4 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<playground version='3.0' sdk='iphonesimulator'>
-    <sections>
-        <code source-file-name='section-1.swift'/>
-    </sections>
+<playground version='5.0' target-platform='ios'>
     <timeline fileName='timeline.xctimeline'/>
 </playground>

+ 0 - 123
CryptoSwift/Playground/CryptoPlayground.playground/section-1.swift

@@ -1,123 +0,0 @@
-// Playground - noun: a place where people can play
-
-import Foundation
-//import CryptoSwift
-
-//Cipher.AES(key: [0x01], iv: [0x01], blockMode: .CBC)
-var arr = [1,2,3,4,5]
-
-//arr.reduce(0, combine: { a,b in
-//    println("a \(a), b \(b)")
-//    return a + 1
-//})
-
-let blocks:[[UInt8]] = [[1,2],[3,4],[5,6]]
-blocks.reduce([UInt8](), combine: { item1,item2 -> [UInt8] in
-    println("a \(item1), b \(item2)")
-    return item2
-})
-
-
-//let arr2 = arr.map { num in
-//    return num + 10
-//}
-
-
-//typealias Byte = UInt8
-//
-//protocol GenericIntegerType: IntegerType {
-//    init(_ v: Int)
-//    init(_ v: UInt)
-//    init(_ v: Int8)
-//    init(_ v: UInt8)
-//    init(_ v: Int16)
-//    init(_ v: UInt16)
-//    init(_ v: Int32)
-//    init(_ v: UInt32)
-//    init(_ v: Int64)
-//    init(_ v: UInt64)
-//}
-//
-//protocol GenericSignedIntegerBitPattern {
-//    init(bitPattern: UIntMax)
-//    init(truncatingBitPattern: IntMax)
-//}
-//
-//protocol GenericUnsignedIntegerBitPattern {
-//    init(truncatingBitPattern: UIntMax)
-//}
-//
-//extension Int:GenericIntegerType, GenericSignedIntegerBitPattern  {
-//    init(bitPattern: UIntMax) {
-//        self.init(bitPattern: UInt(truncatingBitPattern: bitPattern))
-//    }
-//}
-//extension UInt:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
-//extension Int8:GenericIntegerType, GenericSignedIntegerBitPattern {
-//    init(bitPattern: UIntMax) {
-//        self.init(bitPattern: UInt8(truncatingBitPattern: bitPattern))
-//    }
-//}
-//extension UInt8:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
-//extension Int16:GenericIntegerType, GenericSignedIntegerBitPattern {
-//    init(bitPattern: UIntMax) {
-//        self.init(bitPattern: UInt16(truncatingBitPattern: bitPattern))
-//    }
-//}
-//extension UInt16:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
-//extension Int32:GenericIntegerType, GenericSignedIntegerBitPattern {
-//    init(bitPattern: UIntMax) {
-//        self.init(bitPattern: UInt32(truncatingBitPattern: bitPattern))
-//    }
-//}
-//extension UInt32:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
-//extension Int64:GenericIntegerType, GenericSignedIntegerBitPattern {
-//    // init(bitPattern: UInt64) already defined
-//    
-//    init(truncatingBitPattern: IntMax) {
-//        self.init(truncatingBitPattern)
-//    }
-//}
-//extension UInt64:GenericIntegerType, GenericUnsignedIntegerBitPattern {
-//    // init(bitPattern: Int64) already defined
-//    
-//    init(truncatingBitPattern: UIntMax) {
-//        self.init(truncatingBitPattern)
-//    }
-//}
-//
-//func integerWithBytes<T: GenericIntegerType where T: UnsignedIntegerType, T: GenericUnsignedIntegerBitPattern>(bytes:[UInt8]) -> T? {
-//    if (bytes.count < sizeof(T)) {
-//        return nil
-//    }
-//
-//    let maxBytes = sizeof(T)
-//    var i:UIntMax = 0
-//    for (var j = 0; j < maxBytes; j++) {
-//        i = i | T(bytes[j]).toUIntMax() << UIntMax(j * 8)
-//    }
-//    return T(truncatingBitPattern: i)
-//}
-//
-//func integerWithBytes<T: GenericIntegerType where T: SignedIntegerType, T:  GenericSignedIntegerBitPattern>(bytes:[UInt8]) -> T? {
-//    if (bytes.count < sizeof(T)) {
-//        return nil
-//    }
-//    
-//    let maxBytes = sizeof(T)
-//    var i:IntMax = 0
-//    for (var j = 0; j < maxBytes; j++) {
-//        i = i | T(bitPattern: UIntMax(bytes[j].toUIntMax())).toIntMax() << (j * 8).toIntMax()
-//    }
-//    return T(truncatingBitPattern: i)
-//}
-//
-//let bytes:[UInt8] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
-//integerWithBytes(bytes) as Int8?
-//integerWithBytes(bytes) as UInt8?
-//integerWithBytes(bytes) as Int16?
-//integerWithBytes(bytes) as UInt16?
-//integerWithBytes(bytes) as Int32?
-//integerWithBytes(bytes) as UInt32?
-//integerWithBytes(bytes) as Int64?
-//integerWithBytes(bytes) as UInt64?

+ 6 - 2
CryptoSwift/SHA1.swift

@@ -8,7 +8,12 @@
 
 import Foundation
 
-class SHA1 : CryptoSwift.HashBase {
+class SHA1 : CryptoSwift.HashBase, _Hash {
+    var size:Int = 20 // 160 / 8
+    
+    override init(_ message: NSData) {
+        super.init(message)
+    }
     
     private let h:[UInt32] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
         
@@ -80,7 +85,6 @@ class SHA1 : CryptoSwift.HashBase {
                 C = rotateLeft(B, 30)
                 B = A
                 A = temp
-                
             }
             
             hh[0] = (hh[0] &+ A) & 0xffffffff

+ 59 - 16
CryptoSwift/SHA2.swift

@@ -8,12 +8,50 @@
 
 import Foundation
 
-class SHA2 : CryptoSwift.HashBase {
+
+class SHA2 : HashBase, _Hash {
+    var size:Int { return variant.rawValue }
+    let variant:SHA2.Variant
     
-    enum variant {
+    enum Variant: RawRepresentable {
         case sha224, sha256, sha384, sha512
         
-        func h() -> [UInt64] {
+        typealias RawValue = Int
+        var rawValue: RawValue {
+            switch (self) {
+            case .sha224:
+                return 224
+            case .sha256:
+                return 256
+            case .sha384:
+                return 384
+            case .sha512:
+                return 512
+            }
+        }
+        
+        init?(rawValue: RawValue) {
+            switch (rawValue) {
+            case 224:
+                self = .sha224
+                break;
+            case 256:
+                self = .sha256
+                break;
+            case 384:
+                self = .sha384
+                break;
+            case 512:
+                self = .sha512
+                break;
+            default:
+                return nil
+            }
+        }
+        
+        var size:Int { return self.rawValue }
+        
+        private var h:[UInt64] {
             switch (self) {
             case .sha224:
                 return [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
@@ -26,7 +64,7 @@ class SHA2 : CryptoSwift.HashBase {
             }
         }
         
-        func k() -> [UInt64] {
+        private var k:[UInt64] {
             switch (self) {
             case .sha224, .sha256:
                 return [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
@@ -57,7 +95,7 @@ class SHA2 : CryptoSwift.HashBase {
             }
         }
         
-        func resultingArray<T>(hh:[T]) -> [T] {
+        private func resultingArray<T>(hh:[T]) -> [T] {
             var finalHH:[T] = hh;
             switch (self) {
             case .sha224:
@@ -73,13 +111,18 @@ class SHA2 : CryptoSwift.HashBase {
         }
     }
     
-    //FIXME: I can't do Generic fuct out of calculate32 and calculate64 (UInt32 vs UInt64), but if you can - please do pull request.
-    func calculate32(variant: SHA2.variant) -> NSData {
+    init(_ message:NSData, variant: SHA2.Variant) {
+        self.variant = variant
+        super.init(message)
+    }
+    
+    //FIXME: I can't do Generic func out of calculate32 and calculate64 (UInt32 vs UInt64), but if you can - please do pull request.
+    func calculate32() -> NSData {
         var tmpMessage = self.prepare()
         
         // hash values
         var hh = [UInt32]()
-        variant.h().map({(h) -> () in
+        variant.h.map({(h) -> () in
             hh.append(UInt32(h))
         })
         
@@ -93,7 +136,7 @@ class SHA2 : CryptoSwift.HashBase {
             let chunk = tmpMessage.subdataWithRange(NSRange(location: i, length: min(chunkSizeBytes,leftMessageBytes)))
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
             // Extend the sixteen 32-bit words into sixty-four 32-bit words:
-            var M:[UInt32] = [UInt32](count: variant.k().count, repeatedValue: 0)
+            var M:[UInt32] = [UInt32](count: variant.k.count, repeatedValue: 0)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
@@ -119,13 +162,13 @@ class SHA2 : CryptoSwift.HashBase {
             var H = hh[7]
             
             // Main loop
-            for j in 0..<variant.k().count {
+            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
                 let s1 = rotateRight(E,6) ^ rotateRight(E,11) ^ rotateRight(E,25)
                 let ch = (E & F) ^ ((~E) & G)
-                let t1 = H &+ s1 &+ ch &+ UInt32(variant.k()[j]) &+ M[j]
+                let t1 = H &+ s1 &+ ch &+ UInt32(variant.k[j]) &+ M[j]
                 
                 H = G
                 G = F
@@ -158,12 +201,12 @@ class SHA2 : CryptoSwift.HashBase {
         return buf.copy() as! NSData;
     }
     
-    func calculate64(variant: SHA2.variant) -> NSData {
+    func calculate64() -> NSData {
         var tmpMessage = self.prepare(128)
         
         // hash values
         var hh = [UInt64]()
-        variant.h().map({(h) -> () in
+        variant.h.map({(h) -> () in
             hh.append(h)
         })
         
@@ -177,7 +220,7 @@ class SHA2 : CryptoSwift.HashBase {
             var chunk = tmpMessage.subdataWithRange(NSRange(location: i, length: min(chunkSizeBytes,leftMessageBytes)))
             // break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian
             // Extend the sixteen 64-bit words into eighty 64-bit words:
-            var M = [UInt64](count: variant.k().count, repeatedValue: 0)
+            var M = [UInt64](count: variant.k.count, repeatedValue: 0)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
@@ -203,13 +246,13 @@ class SHA2 : CryptoSwift.HashBase {
             var H = hh[7]
             
             // Main loop
-            for j in 0..<variant.k().count {
+            for j in 0..<variant.k.count {
                 let s0 = rotateRight(A,28) ^ rotateRight(A,34) ^ rotateRight(A,39)
                 let maj = (A & B) ^ (A & C) ^ (B & C)
                 let t2 = s0 &+ maj
                 let s1 = rotateRight(E,14) ^ rotateRight(E,18) ^ rotateRight(E,41)
                 let ch = (E & F) ^ ((~E) & G)
-                let t1 = H &+ s1 &+ ch &+ variant.k()[j] &+ UInt64(M[j])
+                let t1 = H &+ s1 &+ ch &+ variant.k[j] &+ UInt64(M[j])
                 
                 H = G
                 G = F

+ 1 - 1
CryptoSwift/UInt32Extension.swift

@@ -14,7 +14,7 @@ extension UInt32 {
         return arrayOfBytes(self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: Slice<UInt8>) -> UInt32 {
+    public static func withBytes(bytes: ArraySlice<UInt8>) -> UInt32 {
         return UInt32.withBytes(Array(bytes))
     }
 

+ 1 - 1
CryptoSwift/UInt64Extension.swift

@@ -14,7 +14,7 @@ extension UInt64 {
         return arrayOfBytes(self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: Slice<UInt8>) -> UInt64 {
+    public static func withBytes(bytes: ArraySlice<UInt8>) -> UInt64 {
         return UInt64.withBytes(Array(bytes))
     }