Browse Source

Make sliceToUInt32Array generic function for Collection

Marcin Krzyżanowski 9 years ago
parent
commit
9bff118342

+ 10 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -44,6 +44,10 @@
 		755655C91D080E3F00F004E7 /* Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755655C61D080E3F00F004E7 /* Cryptors.swift */; };
 		755655CA1D080E3F00F004E7 /* Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755655C61D080E3F00F004E7 /* Cryptors.swift */; };
 		755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755FB1D9199E347D00475437 /* ExtensionsTest.swift */; };
+		756B575F1D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57601D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57611D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57621D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
 		7574E5FE1CD02C8B00E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
 		7574E5FF1CD02C9300E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
 		7574E6001CD02C9300E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
@@ -354,6 +358,7 @@
 		755655C61D080E3F00F004E7 /* Cryptors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Cryptors.swift; path = Sources/CryptoSwift/Cryptors.swift; sourceTree = SOURCE_ROOT; };
 		755D27BC1D06DE6400C41692 /* CryptoSwift.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = CryptoSwift.playground; sourceTree = "<group>"; };
 		755FB1D9199E347D00475437 /* ExtensionsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionsTest.swift; sourceTree = "<group>"; };
+		756B575E1D5131A4001C0C72 /* Collection+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Collection+Extension.swift"; path = "Sources/CryptoSwift/Collection+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		756BFDCA1A82B87300B9D9A4 /* Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bridging.h; sourceTree = "<group>"; };
 		7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSArrayType+Foundation.swift"; sourceTree = "<group>"; };
 		757BC8F71C1CA56A0093AAA9 /* AES+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AES+Foundation.swift"; sourceTree = "<group>"; };
@@ -500,6 +505,7 @@
 			children = (
 				75DB81B11CDC0E4100ED181A /* Ciphers */,
 				757BC91B1C1CA5790093AAA9 /* Array+Extension.swift */,
+				756B575E1D5131A4001C0C72 /* Collection+Extension.swift */,
 				757BC91D1C1CA5790093AAA9 /* Authenticator.swift */,
 				80545D121CA9FECD00474A99 /* Bit.swift */,
 				757F440D1CC1822A002B1F85 /* SecureBytes.swift */,
@@ -858,6 +864,7 @@
 				757F440F1CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9751C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACC1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57601D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9071C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				75D0E05D1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
 				757BC9851C1CA5790093AAA9 /* IntExtension.swift in Sources */,
@@ -922,6 +929,7 @@
 				757F440E1CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9741C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACB1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B575F1D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC90E1C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				75D0E05C1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
 				757BC9841C1CA5790093AAA9 /* IntExtension.swift in Sources */,
@@ -1003,6 +1011,7 @@
 				757F44111CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9761C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACE1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57621D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9101C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				75D0E05F1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
 				757BC9861C1CA5790093AAA9 /* IntExtension.swift in Sources */,
@@ -1067,6 +1076,7 @@
 				757F44101CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9771C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACD1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57611D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9091C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				75D0E05E1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
 				757BC9871C1CA5790093AAA9 /* IntExtension.swift in Sources */,

+ 1 - 1
CryptoSwiftTests/ExtensionsTest.swift

@@ -78,7 +78,7 @@ final class ExtensionsTest: XCTestCase {
     
     func testtoUInt32Array() {
         let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
-        let result = sliceToUInt32Array(chunk)
+        let result = chunk.toUInt32Array()
         
         XCTAssert(result.count == 2, "Invalid conversion")
         XCTAssert(result[0] == 117506305, "Invalid conversion")

+ 2 - 2
Sources/CryptoSwift/AES.swift

@@ -114,7 +114,7 @@ fileprivate extension AES {
     func encryptBlock(_ block:Array<UInt8>) -> Array<UInt8>? {
         let rounds = self.variant.Nr
         let rk = self.expandedKey
-        var b = sliceToUInt32Array(block[block.indices])
+        var b = block[block.indices].toUInt32Array()
 
         var t = Array<UInt32>(repeating: 0, count: 4)
 
@@ -178,7 +178,7 @@ fileprivate extension AES {
     func decryptBlock(_ block:Array<UInt8>) -> Array<UInt8>? {
         let rounds = self.variant.Nr
         let rk = expandedKeyInv
-        var b = sliceToUInt32Array(block[block.indices])
+        var b = block[block.indices].toUInt32Array()
 
         var t = Array<UInt32>(repeating: 0, count: 4)
 

+ 2 - 2
Sources/CryptoSwift/ChaCha20.swift

@@ -237,10 +237,10 @@ extension ChaCha20: Cipher {
 // MARK: Helpers
 
 /// Change array to number. It's here because arrayOfBytes is too slow
-private func wordNumber(_ bytes:ArraySlice<UInt8>) -> UInt32 {
+private func wordNumber<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.IndexDistance == Int {
     var value:UInt32 = 0
     for i:UInt32 in 0..<4 {
-        let j = bytes.startIndex + Int(i)
+        let j = bytes.index(bytes.startIndex, offsetBy: Int(i))
         value = value | UInt32(bytes[j]) << (8 * i)
     }
 

+ 41 - 0
Sources/CryptoSwift/Collection+Extension.swift

@@ -0,0 +1,41 @@
+//
+//  Collection+Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/08/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
+    func toUInt32Array() -> Array<UInt32> {
+        var result = Array<UInt32>()
+        result.reserveCapacity(16)
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: sizeof(UInt32.self)) {
+            let val1:UInt32 = (UInt32(self[idx.advanced(by: 3)]) << 24)
+            let val2:UInt32 = (UInt32(self[idx.advanced(by: 2)]) << 16)
+            let val3:UInt32 = (UInt32(self[idx.advanced(by: 1)]) << 8)
+            let val4:UInt32 = UInt32(self[idx])
+            let val:UInt32 = val1 | val2 | val3 | val4
+            result.append(val)
+        }
+        return result
+    }
+
+    func toUInt64Array() -> Array<UInt64> {
+        var result = Array<UInt64>()
+        result.reserveCapacity(32)
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: sizeof(UInt64.self)) {
+            var val:UInt64 = 0
+            val |= UInt64(self[idx.advanced(by: 7)]) << 56
+            val |= UInt64(self[idx.advanced(by: 6)]) << 48
+            val |= UInt64(self[idx.advanced(by: 5)]) << 40
+            val |= UInt64(self[idx.advanced(by: 4)]) << 32
+            val |= UInt64(self[idx.advanced(by: 3)]) << 24
+            val |= UInt64(self[idx.advanced(by: 2)]) << 16
+            val |= UInt64(self[idx.advanced(by: 1)]) << 8
+            val |= UInt64(self[idx.advanced(by: 0)]) << 0
+            result.append(val)
+        }
+        return result
+    }
+}

+ 1 - 1
Sources/CryptoSwift/MD5.swift

@@ -56,7 +56,7 @@ final class MD5 : HashProtocol  {
         let chunkSizeBytes = 512 / 8 // 64
         for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
-            var M = sliceToUInt32Array(chunk)
+            var M = chunk.toUInt32Array()
             assert(M.count == 16, "Invalid array")
             
             // Initialize hash value for this chunk:

+ 1 - 1
Sources/CryptoSwift/SHA1.swift

@@ -36,7 +36,7 @@ final class SHA1 : HashProtocol {
                 case 0...15:
                     let start = chunk.startIndex + (x * sizeofValue(M[x]))
                     let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt32Array(chunk[start..<end])[0]
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 2 - 2
Sources/CryptoSwift/SHA2.swift

@@ -136,7 +136,7 @@ final class SHA2 : HashProtocol {
                 case 0...15:
                     let start = chunk.startIndex + (x * sizeofValue(M[x]))
                     let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt32Array(chunk[start..<end])[0]
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:
@@ -219,7 +219,7 @@ final class SHA2 : HashProtocol {
                 case 0...15:
                     let start = chunk.startIndex + (x * sizeofValue(M[x]))
                     let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt64Array(chunk[start..<end])[0]
+                    let le = chunk[start..<end].toUInt64Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 0 - 32
Sources/CryptoSwift/Utils.swift

@@ -52,38 +52,6 @@ func reversed(_ uint32 : UInt32) -> UInt32 {
     return v
 }
 
-func sliceToUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
-    var result = Array<UInt32>()
-    result.reserveCapacity(16)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt32.self)) {
-        let val1:UInt32 = (UInt32(slice[idx.advanced(by: 3)]) << 24)
-        let val2:UInt32 = (UInt32(slice[idx.advanced(by: 2)]) << 16)
-        let val3:UInt32 = (UInt32(slice[idx.advanced(by: 1)]) << 8)
-        let val4:UInt32 = UInt32(slice[idx])
-        let val:UInt32 = val1 | val2 | val3 | val4
-        result.append(val)
-    }
-    return result
-}
-
-func sliceToUInt64Array(_ slice: ArraySlice<UInt8>) -> Array<UInt64> {
-    var result = Array<UInt64>()
-    result.reserveCapacity(32)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt64.self)) {
-        var val:UInt64 = 0
-        val |= UInt64(slice[idx.advanced(by: 7)]) << 56
-        val |= UInt64(slice[idx.advanced(by: 6)]) << 48
-        val |= UInt64(slice[idx.advanced(by: 5)]) << 40
-        val |= UInt64(slice[idx.advanced(by: 4)]) << 32
-        val |= UInt64(slice[idx.advanced(by: 3)]) << 24
-        val |= UInt64(slice[idx.advanced(by: 2)]) << 16
-        val |= UInt64(slice[idx.advanced(by: 1)]) << 8
-        val |= UInt64(slice[idx.advanced(by: 0)]) << 0
-        result.append(val)
-    }
-    return result
-}
-
 func xor(_ a: Array<UInt8>, _ b:Array<UInt8>) -> Array<UInt8> {
     var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
     for i in 0..<xored.count {