Procházet zdrojové kódy

Remove many Foundation dependencies by replacing NSData with [UInt8]. Separate Foundation dependency to extensions (ChaCha20, AES)

Marcin Krzyżanowski před 10 roky
rodič
revize
5efd01ecbe
42 změnil soubory, kde provedl 223 přidání a 239 odebrání
  1. 14 18
      CryptoSwift.xcodeproj/project.pbxproj
  2. 19 0
      CryptoSwift/AES+Foundation.swift
  3. 23 36
      CryptoSwift/AES.swift
  4. 1 5
      CryptoSwift/ArrayExtension.swift
  5. 42 0
      CryptoSwift/ArraySlice+Bytes.swift
  6. 0 2
      CryptoSwift/Authenticator.swift
  7. 0 2
      CryptoSwift/BitExtension.swift
  8. 1 1
      CryptoSwift/BytesSequence.swift
  9. 3 5
      CryptoSwift/CRC.swift
  10. 18 0
      CryptoSwift/ChaCha20+Foundation.swift
  11. 0 10
      CryptoSwift/ChaCha20.swift
  12. 1 1
      CryptoSwift/Cipher.swift
  13. 0 2
      CryptoSwift/CipherBlockMode.swift
  14. 0 19
      CryptoSwift/CryptoSwift.h
  15. 0 2
      CryptoSwift/Generics.swift
  16. 8 10
      CryptoSwift/HMAC.swift
  17. 18 20
      CryptoSwift/Hash.swift
  18. 10 18
      CryptoSwift/HashProtocol.swift
  19. 1 1
      CryptoSwift/IntExtension.swift
  20. 0 2
      CryptoSwift/IntegerConvertible.swift
  21. 10 13
      CryptoSwift/MD5.swift
  22. 7 7
      CryptoSwift/NSDataExtension.swift
  23. 2 0
      CryptoSwift/NSDataSequence.swift
  24. 0 2
      CryptoSwift/Operators.swift
  25. 0 2
      CryptoSwift/PKCS5.swift
  26. 0 2
      CryptoSwift/PKCS7.swift
  27. 0 2
      CryptoSwift/Padding.swift
  28. 0 2
      CryptoSwift/Poly1305.swift
  29. 9 10
      CryptoSwift/SHA1.swift
  30. 14 17
      CryptoSwift/SHA2.swift
  31. 1 1
      CryptoSwift/UInt16Extension.swift
  32. 4 1
      CryptoSwift/UInt32Extension.swift
  33. 0 2
      CryptoSwift/UInt64Extension.swift
  34. 4 1
      CryptoSwift/UInt8Extension.swift
  35. 0 2
      CryptoSwiftTests/AESTests.swift
  36. 0 2
      CryptoSwiftTests/ChaCha20Tests.swift
  37. 9 3
      CryptoSwiftTests/ExtensionsTest.swift
  38. 0 2
      CryptoSwiftTests/HMACTests.swift
  39. 4 8
      CryptoSwiftTests/HashTests.swift
  40. 0 2
      CryptoSwiftTests/Helpers.swift
  41. 0 2
      CryptoSwiftTests/PaddingTests.swift
  42. 0 2
      CryptoSwiftTests/Poly1305Tests.swift

+ 14 - 18
CryptoSwift.xcodeproj/project.pbxproj

@@ -30,9 +30,7 @@
 		7539E32C1B3B4C750037F4E1 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* StringExtension.swift */; };
 		7539E32D1B3B4E950037F4E1 /* SHA1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752E087A199FF27C005B0EA0 /* SHA1.swift */; };
 		7539E32E1B3B4E970037F4E1 /* SHA2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75153D4119AA3C7900750381 /* SHA2.swift */; };
-		75445821196AA2A5002FF20E /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75445820196AA2A5002FF20E /* Security.framework */; settings = {ATTRIBUTES = (Required, ); }; };
 		7547195119931802002FA5F1 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7547195019931802002FA5F1 /* IntExtension.swift */; };
-		754BE45B19693E190098E6F3 /* CryptoSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 754BE45A19693E190098E6F3 /* CryptoSwift.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		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 */; };
@@ -60,6 +58,9 @@
 		75BE4EB11B1E4A9F007A2B57 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */; };
 		75D2D1A01B5689EB000A2615 /* NSDataSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D2D19F1B5689EB000A2615 /* NSDataSequence.swift */; };
 		75D63F751BB711270041579B /* BytesSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F741BB711270041579B /* BytesSequence.swift */; settings = {ASSET_TAGS = (); }; };
+		75D63F771BB840050041579B /* ArraySlice+Bytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F761BB840050041579B /* ArraySlice+Bytes.swift */; settings = {ASSET_TAGS = (); }; };
+		75D63F791BB86F6E0041579B /* ChaCha20+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F781BB86F6E0041579B /* ChaCha20+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
+		75D63F7B1BB86F9C0041579B /* AES+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F7A1BB86F9C0041579B /* AES+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
 		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
 		75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */; };
 		75D94E2819B60DDE007CB2A4 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */; };
@@ -142,7 +143,6 @@
 		7547195019931802002FA5F1 /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntExtension.swift; sourceTree = "<group>"; };
 		754BE45519693E190098E6F3 /* CryptoSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CryptoSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		754BE45919693E190098E6F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
-		754BE45A19693E190098E6F3 /* CryptoSwift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CryptoSwift.h; sourceTree = "<group>"; };
 		754BE46019693E190098E6F3 /* CryptoSwiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CryptoSwiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 		754BE46619693E190098E6F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		754BE46719693E190098E6F3 /* HashTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashTests.swift; sourceTree = "<group>"; };
@@ -173,6 +173,9 @@
 		75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntegerConvertible.swift; sourceTree = "<group>"; };
 		75D2D19F1B5689EB000A2615 /* NSDataSequence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataSequence.swift; sourceTree = "<group>"; };
 		75D63F741BB711270041579B /* BytesSequence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BytesSequence.swift; sourceTree = "<group>"; };
+		75D63F761BB840050041579B /* ArraySlice+Bytes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ArraySlice+Bytes.swift"; sourceTree = "<group>"; };
+		75D63F781BB86F6E0041579B /* ChaCha20+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChaCha20+Foundation.swift"; sourceTree = "<group>"; };
+		75D63F7A1BB86F9C0041579B /* AES+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AES+Foundation.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>"; };
 		75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt64Extension.swift; sourceTree = "<group>"; };
@@ -184,7 +187,6 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				75445821196AA2A5002FF20E /* Security.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -229,7 +231,6 @@
 		754BE45719693E190098E6F3 /* CryptoSwift */ = {
 			isa = PBXGroup;
 			children = (
-				754BE45A19693E190098E6F3 /* CryptoSwift.h */,
 				7552614D1993051E000D2B20 /* Hash.swift */,
 				7563B2E719B14D4300B152CD /* Cipher.swift */,
 				75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */,
@@ -243,9 +244,11 @@
 				75153D4119AA3C7900750381 /* SHA2.swift */,
 				757EF7F419AAA82400586276 /* CRC.swift */,
 				75EB380019ABDD710002375A /* ChaCha20.swift */,
+				75D63F781BB86F6E0041579B /* ChaCha20+Foundation.swift */,
 				751C5C3C19B26B000094C75D /* Poly1305.swift */,
 				758A94251A65AEB100E46135 /* HMAC.swift */,
 				75A74B261A1FF6B2004419F1 /* AES.swift */,
+				75D63F7A1BB86F9C0041579B /* AES+Foundation.swift */,
 				759D481019B517BC005FF7FC /* BitExtension.swift */,
 				758F3F771992F6CE0014BBDA /* UInt8Extension.swift */,
 				7547195019931802002FA5F1 /* IntExtension.swift */,
@@ -254,6 +257,7 @@
 				75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */,
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
+				75D63F761BB840050041579B /* ArraySlice+Bytes.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
 				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
@@ -306,7 +310,6 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				754BE45B19693E190098E6F3 /* CryptoSwift.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -456,12 +459,15 @@
 				7539E3291B3B4A530037F4E1 /* MD5.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
 				758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */,
+				75D63F7B1BB86F9C0041579B /* AES+Foundation.swift in Sources */,
+				75D63F791BB86F6E0041579B /* ChaCha20+Foundation.swift in Sources */,
 				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
 				7539E32D1B3B4E950037F4E1 /* SHA1.swift in Sources */,
 				75B0A5701AB1A1BB000BD8D2 /* PKCS5.swift in Sources */,
 				757EF7F519AAA82400586276 /* CRC.swift in Sources */,
 				75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */,
 				75A74B271A1FF6B2004419F1 /* AES.swift in Sources */,
+				75D63F771BB840050041579B /* ArraySlice+Bytes.swift in Sources */,
 				754C30B71AA13BC000E6FFA4 /* PKCS7.swift in Sources */,
 				755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */,
 				754DD76E19A149AF00E52288 /* HashProtocol.swift in Sources */,
@@ -580,6 +586,7 @@
 				METAL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = iphoneos;
+				SWIFT_INSTALL_OBJC_HEADER = NO;
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
 				TARGETED_DEVICE_FAMILY = "1,2";
 				VERSIONING_SYSTEM = "apple-generic";
@@ -619,6 +626,7 @@
 				IPHONEOS_DEPLOYMENT_TARGET = 8.0;
 				METAL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = iphoneos;
+				SWIFT_INSTALL_OBJC_HEADER = NO;
 				TARGETED_DEVICE_FAMILY = "1,2";
 				VALIDATE_PRODUCT = YES;
 				VERSIONING_SYSTEM = "apple-generic";
@@ -631,16 +639,10 @@
 			buildSettings = {
 				APPLICATION_EXTENSION_API_ONLY = YES;
 				CLANG_ENABLE_MODULES = YES;
-				DEFINES_MODULE = YES;
 				DYLIB_COMPATIBILITY_VERSION = 1;
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_TESTABILITY = YES;
-				HEADER_SEARCH_PATHS = (
-					"$(inherited)",
-					"/Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
-					"includes/**",
-				);
 				INFOPLIST_FILE = CryptoSwift/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
@@ -656,18 +658,12 @@
 			buildSettings = {
 				APPLICATION_EXTENSION_API_ONLY = YES;
 				CLANG_ENABLE_MODULES = YES;
-				DEFINES_MODULE = YES;
 				DYLIB_COMPATIBILITY_VERSION = 1;
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_TESTABILITY = YES;
 				GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
 				GCC_UNROLL_LOOPS = YES;
-				HEADER_SEARCH_PATHS = (
-					"$(inherited)",
-					"/Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
-					"includes/**",
-				);
 				INFOPLIST_FILE = CryptoSwift/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";

+ 19 - 0
CryptoSwift/AES+Foundation.swift

@@ -0,0 +1,19 @@
+//
+//  AES+Foundation.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+extension AES {
+    convenience public init?(key:String, iv:String, blockMode:CipherBlockMode = .CBC) {
+        guard let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes() else {
+            return nil
+        }
+        
+        self.init(key: kkey, iv: iiv, blockMode: blockMode)
+    }
+}

+ 23 - 36
CryptoSwift/AES.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final public class AES {
     
     enum Error: ErrorType {
@@ -88,16 +86,7 @@ final public class AES {
         let defaultIV = [UInt8](count: AES.blockSize, repeatedValue: 0)
         self.init(key: key, iv: defaultIV, blockMode: blockMode)
     }
-    
-    convenience public init?(key:String, iv:String, blockMode:CipherBlockMode = .CBC) {
-        if let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes() {
-            self.init(key: kkey, iv: iiv, blockMode: blockMode)
-        }
-        else {
-            return nil
-        }
-    }
-    
+        
     /**
     Encrypt message. If padding is necessary, then PKCS7 padding is added and needs to be removed after decryption.
     
@@ -122,33 +111,31 @@ final public class AES {
     private func encryptBlock(block:[UInt8]) -> [UInt8]? {
         var out:[UInt8] = [UInt8]()
         
-        autoreleasepool { () -> () in
-            var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
-            for (i, row) in state.enumerate() {
-                for (j, _) in row.enumerate() {
-                    state[j][i] = block[i * row.count + j]
-                }
-            }
-            
-            state = addRoundKey(state,expandedKey, 0)
-            
-            for roundCount in 1..<variant.Nr {
-                subBytes(&state)
-                state = shiftRows(state)
-                state = mixColumns(state)
-                state = addRoundKey(state, expandedKey, roundCount)
+        var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
+        for (i, row) in state.enumerate() {
+            for (j, _) in row.enumerate() {
+                state[j][i] = block[i * row.count + j]
             }
-            
+        }
+        
+        state = addRoundKey(state,expandedKey, 0)
+        
+        for roundCount in 1..<variant.Nr {
             subBytes(&state)
             state = shiftRows(state)
-            state = addRoundKey(state, expandedKey, variant.Nr)
-            
-            
-            out = [UInt8](count: state.count * state.first!.count, repeatedValue: 0)
-            for i in 0..<state.count {
-                for j in 0..<state[i].count {
-                    out[(i * 4) + j] = state[j][i]
-                }
+            state = mixColumns(state)
+            state = addRoundKey(state, expandedKey, roundCount)
+        }
+        
+        subBytes(&state)
+        state = shiftRows(state)
+        state = addRoundKey(state, expandedKey, variant.Nr)
+        
+        
+        out = [UInt8](count: state.count * state.first!.count, repeatedValue: 0)
+        for i in 0..<state.count {
+            for j in 0..<state[i].count {
+                out[(i * 4) + j] = state[j][i]
             }
         }
         return out

+ 1 - 5
CryptoSwift/ArrayExtension.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 extension Array {
     
     /** split in chunks with given chunk size */
@@ -28,9 +26,6 @@ extension Array {
 
 // MARK: - Array<UInt8>
 
-public protocol _UInt8Type {}
-extension UInt8: _UInt8Type {}
-
 extension Array where Element: _UInt8Type {
     public func toHexString() -> String {
         var s:String = "";
@@ -41,3 +36,4 @@ extension Array where Element: _UInt8Type {
     }
 }
 
+

+ 42 - 0
CryptoSwift/ArraySlice+Bytes.swift

@@ -0,0 +1,42 @@
+//
+//  ArraySlice.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+extension ArraySlice where Element: _UInt8Type {
+    typealias Element = UInt8
+    
+    func toUInt32Array() -> Array<UInt32> {
+        var result = Array<UInt32>()
+        for idx in self.startIndex.stride(to: self.endIndex, by: sizeof(UInt32)) {
+            var val:UInt32 = 0
+            val |= UInt32(self[idx.advancedBy(3)] as! UInt8) << 24
+            val |= UInt32(self[idx.advancedBy(2)] as! UInt8) << 16
+            val |= UInt32(self[idx.advancedBy(1)] as! UInt8) << 8
+            val |= UInt32(self[idx.advancedBy(0)] as! UInt8) << 0
+            result.append(val)
+        }
+        return result
+    }
+    
+    func toUInt64Array() -> Array<UInt64> {
+        var result = Array<UInt64>()
+        for idx in self.startIndex.stride(to: self.endIndex, by: sizeof(UInt64)) {
+            var val:UInt64 = 0
+            val |= UInt64(self[idx.advancedBy(7)] as! UInt8) << 56
+            val |= UInt64(self[idx.advancedBy(6)] as! UInt8) << 48
+            val |= UInt64(self[idx.advancedBy(5)] as! UInt8) << 40
+            val |= UInt64(self[idx.advancedBy(4)] as! UInt8) << 32
+            val |= UInt64(self[idx.advancedBy(3)] as! UInt8) << 24
+            val |= UInt64(self[idx.advancedBy(2)] as! UInt8) << 16
+            val |= UInt64(self[idx.advancedBy(1)] as! UInt8) << 8
+            val |= UInt64(self[idx.advancedBy(0)] as! UInt8) << 0
+            result.append(val)
+        }
+        return result
+    }
+
+}

+ 0 - 2
CryptoSwift/Authenticator.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 /**
 *  Message Authentication
 */

+ 0 - 2
CryptoSwift/BitExtension.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 extension Bit {
 
     func inverted() -> Bit {

+ 1 - 1
CryptoSwift/BytesSequence.swift

@@ -16,7 +16,7 @@ struct BytesSequence: SequenceType {
         
         return anyGenerator {
             let end = min(self.chunkSize, self.data.count - offset)
-            let result = self.data[offset...offset + end]
+            let result = self.data[offset..<offset + end]
             offset += result.count
             return result.count > 0 ? result : nil
         }

+ 3 - 5
CryptoSwift/CRC.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final class CRC {
     
     private let table:[UInt32] = [0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
@@ -43,11 +41,11 @@ final class CRC {
         0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
         0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d]
     
-    func crc32(message:NSData) -> UInt32 {
+    func crc32(message:[UInt8]) -> UInt32 {
         var crc:UInt32 = 0xffffffff
         
-        for chunk in NSDataSequence(chunkSize: 256, data: message) {
-            for b in chunk.arrayOfBytes() {
+        for chunk in BytesSequence(chunkSize: 256, data: message) {
+            for b in chunk {
                 let idx = Int((crc ^ UInt32(b)) & 0xff)
                 crc = (crc >> 8) ^ table[idx]
             }

+ 18 - 0
CryptoSwift/ChaCha20+Foundation.swift

@@ -0,0 +1,18 @@
+//
+//  ChaCha20+Foundation.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+extension ChaCha20 {
+    convenience public init?(key:String, iv:String) {
+        guard let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes() else {
+            return nil
+        }
+        self.init(key: kkey, iv: iiv)
+    }
+}

+ 0 - 10
CryptoSwift/ChaCha20.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final public class ChaCha20 {
     
     enum Error: ErrorType {
@@ -36,14 +34,6 @@ final public class ChaCha20 {
         }
     }
     
-    convenience public init?(key:String, iv:String) {
-        if let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.arrayOfBytes() {
-            self.init(key: kkey, iv: iiv)
-        }
-        return nil
-    }
-
-    
     public func encrypt(bytes:[UInt8]) throws -> [UInt8] {
         guard context != nil else {
             throw Error.MissingContext

+ 1 - 1
CryptoSwift/Cipher.swift

@@ -6,7 +6,7 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
+import Darwin.libc
 
 public enum Cipher {
     

+ 0 - 2
CryptoSwift/CipherBlockMode.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 // I have no better name for that
 typealias CipherOperationOnBlock = (block: [UInt8]) -> [UInt8]?
 

+ 0 - 19
CryptoSwift/CryptoSwift.h

@@ -1,19 +0,0 @@
-//
-//  CryptoSwift.h
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 06/07/14.
-//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-
-//! Project version number for CryptoSwift.
-FOUNDATION_EXPORT double CryptoSwiftVersionNumber;
-
-//! Project version string for CryptoSwift.
-FOUNDATION_EXPORT const unsigned char CryptoSwiftVersionString[];
-
-// In this header, you should import all the public headers of your framework using statements like #import <CryptoSwift/PublicHeader.h>
-
-

+ 0 - 2
CryptoSwift/Generics.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 /** Protocol and extensions for integerFromBitsArray. Bit hakish for me, but I can't do it in any other way */
 protocol Initiable  {
     init(_ v: Int)

+ 8 - 10
CryptoSwift/HMAC.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final public class HMAC {
     
     public enum Variant {
@@ -16,7 +14,7 @@ final public class HMAC {
         var size:Int {
             switch (self) {
             case .sha1:
-                return SHA1(NSData()).size
+                return SHA1.size
             case .sha256:
                 return SHA2.Variant.sha256.size
             case .sha384:
@@ -24,22 +22,22 @@ final public class HMAC {
             case .sha512:
                 return SHA2.Variant.sha512.size
             case .md5:
-                return MD5(NSData()).size
+                return MD5.size
             }
         }
         
-        func calculateHash(bytes  bytes:[UInt8]) -> [UInt8]? {
+        func calculateHash(bytes bytes:[UInt8]) -> [UInt8]? {
             switch (self) {
             case .sha1:
-                return NSData.withBytes(bytes).sha1()?.arrayOfBytes()
+                return Hash.sha1(bytes).calculate()
             case .sha256:
-                return NSData.withBytes(bytes).sha256()?.arrayOfBytes()
+                return Hash.sha256(bytes).calculate()
             case .sha384:
-                return NSData.withBytes(bytes).sha384()?.arrayOfBytes()
+                return Hash.sha384(bytes).calculate()
             case .sha512:
-                return NSData.withBytes(bytes).sha512()?.arrayOfBytes()
+                return Hash.sha512(bytes).calculate()
             case .md5:
-                return NSData.withBytes(bytes).md5()?.arrayOfBytes();
+                return Hash.md5(bytes).calculate()
             }
         }
         

+ 18 - 20
CryptoSwift/Hash.swift

@@ -6,30 +6,28 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 public enum Hash {
-    case md5(NSData)
-    case sha1(NSData)
-    case sha224(NSData), sha256(NSData), sha384(NSData), sha512(NSData)
-    case crc32(NSData)
+    case md5(Array<UInt8>)
+    case sha1(Array<UInt8>)
+    case sha224(Array<UInt8>), sha256(Array<UInt8>), sha384(Array<UInt8>), sha512(Array<UInt8>)
+    case crc32(Array<UInt8>)
     
     public func calculate() -> [UInt8]? {
         switch self {
-        case md5(let data):
-            return MD5(data).calculate()
-        case sha1(let data):
-            return SHA1(data).calculate()
-        case sha224(let data):
-            return SHA2(data, variant: .sha224).calculate32()
-        case sha256(let data):
-            return SHA2(data, variant: .sha256).calculate32()
-        case sha384(let data):
-            return SHA2(data, variant: .sha384).calculate64()
-        case sha512(let data):
-            return SHA2(data, variant: .sha512).calculate64()
-        case crc32(let data):
-            return CRC().crc32(data).bytes()
+        case md5(let bytes):
+            return MD5(bytes).calculate()
+        case sha1(let bytes):
+            return SHA1(bytes).calculate()
+        case sha224(let bytes):
+            return SHA2(bytes, variant: .sha224).calculate32()
+        case sha256(let bytes):
+            return SHA2(bytes, variant: .sha256).calculate32()
+        case sha384(let bytes):
+            return SHA2(bytes, variant: .sha384).calculate64()
+        case sha512(let bytes):
+            return SHA2(bytes, variant: .sha512).calculate64()
+        case crc32(let bytes):
+            return CRC().crc32(bytes).bytes()
         }
     }
 }

+ 10 - 18
CryptoSwift/HashProtocol.swift

@@ -6,39 +6,31 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 internal protocol HashProtocol {
-    var message: NSData { get }
-
+    var message: Array<UInt8> { get }
+    
     /** Common part for hash calculation. Prepare header data. */
-    func prepare(len:Int) -> NSMutableData
+    func prepare(len:Int) -> Array<UInt8>
 }
 
 extension HashProtocol {
-
-    func prepare(len:Int) -> NSMutableData {
-        let tmpMessage: NSMutableData = NSMutableData(data: self.message)
+    
+    func prepare(len:Int) -> Array<UInt8> {
+        var tmpMessage = message
         
         // Step 1. Append Padding Bits
-        tmpMessage.appendBytes([0x80]) // append one bit (UInt8 with one bit) to message
+        tmpMessage.append(0x80) // append one bit (UInt8 with one bit) to message
         
         // append "0" bit until message length in bits ≡ 448 (mod 512)
-        var msgLength = tmpMessage.length
+        var msgLength = tmpMessage.count
         var counter = 0
         
         while msgLength % len != (len - 8) {
             counter++
             msgLength++
         }
-        
-        let bufZeros = UnsafeMutablePointer<UInt8>(calloc(counter, sizeof(UInt8)))
-        
-        tmpMessage.appendBytes(bufZeros, length: counter)
-        
-        bufZeros.destroy()
-        bufZeros.dealloc(1)
-        
+
+        tmpMessage += Array<UInt8>(count: counter, repeatedValue: 0)
         return tmpMessage
     }
 }

+ 1 - 1
CryptoSwift/IntExtension.swift

@@ -14,7 +14,7 @@
 //  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
 //  - This notice may not be removed or altered from any source or binary distribution.
 
-import Foundation
+import Darwin.libc
 
 /* array of bits */
 extension Int {

+ 0 - 2
CryptoSwift/IntegerConvertible.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 protocol BitshiftOperationsType {
     func <<(lhs: Self, rhs: Self) -> Self
     func >>(lhs: Self, rhs: Self) -> Self

+ 10 - 13
CryptoSwift/MD5.swift

@@ -6,13 +6,11 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final class MD5 : HashProtocol  {
-    var size:Int = 16 // 128 / 8
-    let message: NSData
+    static let size:Int = 16 // 128 / 8
+    let message: Array<UInt8>
     
-    init (_ message: NSData) {
+    init (_ message: Array<UInt8>) {
         self.message = message
     }
 
@@ -43,23 +41,22 @@ final class MD5 : HashProtocol  {
     private let h:[UInt32] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
     
     func calculate() -> [UInt8] {
-        let tmpMessage = prepare(64)
+        var tmpMessage = prepare(64)
 
-        // hash values
+        // initialize hh with hash values
         var hh = h
         
         // Step 2. Append Length a 64-bit representation of lengthInBits
-        let lengthInBits = (message.length * 8)
+        let lengthInBits = (message.count * 8)
         let lengthBytes = lengthInBits.bytes(64 / 8)
-        tmpMessage.appendBytes(Array(lengthBytes.reverse())); //FIXME: Array?
+        tmpMessage += lengthBytes.reverse()
 
         // Process the message in successive 512-bit chunks:
         let chunkSizeBytes = 512 / 8 // 64
-        for chunk in NSDataSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
+        for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
-            var M = [UInt32](count: 16, repeatedValue: 0)
-            let range = NSRange(location:0, length: M.count * sizeof(UInt32))
-            chunk.getBytes(UnsafeMutablePointer<Void>(M), range: range)
+            var M = chunk.toUInt32Array()
+            assert(M.count == 16, "Invalid array")
             
             // Initialize hash value for this chunk:
             var A:UInt32 = hh[0]

+ 7 - 7
CryptoSwift/NSDataExtension.swift

@@ -31,37 +31,37 @@ extension NSData {
     }
     
     public func md5() -> NSData? {
-        guard let result = Hash.md5(self).calculate() else { return nil }
+        guard let result = Hash.md5(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func sha1() -> NSData? {
-        guard let result = Hash.sha1(self).calculate() else { return nil }
+        guard let result = Hash.sha1(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func sha224() -> NSData? {
-        guard let result = Hash.sha224(self).calculate() else { return nil }
+        guard let result = Hash.sha224(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func sha256() -> NSData? {
-        guard let result = Hash.sha256(self).calculate() else { return nil }
+        guard let result = Hash.sha256(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func sha384() -> NSData? {
-        guard let result = Hash.sha384(self).calculate() else { return nil }
+        guard let result = Hash.sha384(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func sha512() -> NSData? {
-        guard let result = Hash.sha512(self).calculate() else { return nil }
+        guard let result = Hash.sha512(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 
     public func crc32() -> NSData? {
-        guard let result = Hash.crc32(self).calculate() else { return nil }
+        guard let result = Hash.crc32(self.arrayOfBytes()).calculate() else { return nil }
         return NSData.withBytes(result)
     }
 

+ 2 - 0
CryptoSwift/NSDataSequence.swift

@@ -6,6 +6,8 @@
 //  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
 //
 
+import Foundation
+
 struct NSDataSequence: SequenceType {
     
     let chunkSize: Int

+ 0 - 2
CryptoSwift/Operators.swift

@@ -22,8 +22,6 @@ j &<<= 2        //shift left and assign
 @see: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071
 */
 
-import Foundation
-
 infix operator &<<= {
 associativity none
 precedence 160

+ 0 - 2
CryptoSwift/PKCS5.swift

@@ -10,8 +10,6 @@
 //
 //  PKCS#5 http://tools.ietf.org/html/rfc2898
 
-import Foundation
-
 //public struct PKCS5 {
 //}
 //

+ 0 - 2
CryptoSwift/PKCS7.swift

@@ -9,8 +9,6 @@
 //  and published by RSA Security Inc, starting in the early 1990s.
 //
 
-import Foundation
-
 public struct PKCS7: Padding {
     
     public init() {

+ 0 - 2
CryptoSwift/Padding.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 public protocol Padding {
     func add(data: [UInt8], blockSize:Int) -> [UInt8]
     func remove(data: [UInt8], blockSize:Int?) -> [UInt8]

+ 0 - 2
CryptoSwift/Poly1305.swift

@@ -10,8 +10,6 @@
 //  Poly1305 takes a 32-byte, one-time key and a message and produces a 16-byte tag that authenticates the
 //  message such that an attacker has a negligible chance of producing a valid tag for an inauthentic message.
 
-import Foundation
-
 final public class Poly1305 {
     let blockSize = 16
     private var ctx:Context?

+ 9 - 10
CryptoSwift/SHA1.swift

@@ -6,38 +6,37 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 final class SHA1 : HashProtocol {
-    var size:Int = 20 // 160 / 8
-    let message: NSData
+    static let size:Int = 20 // 160 / 8
+    let message: [UInt8]
     
-    init(_ message: NSData) {
+    init(_ message: [UInt8]) {
         self.message = message
     }
     
     private let h:[UInt32] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
         
     func calculate() -> [UInt8] {
-        let tmpMessage = self.prepare(64)
+        var tmpMessage = self.prepare(64)
         
         // hash values
         var hh = h
         
         // append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
-        tmpMessage.appendBytes((self.message.length * 8).bytes(64 / 8));
+        tmpMessage += (self.message.count * 8).bytes(64 / 8)
         
         // Process the message in successive 512-bit chunks:
         let chunkSizeBytes = 512 / 8 // 64
-        for chunk in NSDataSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
+        for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
             // Extend the sixteen 32-bit words into eighty 32-bit words:
             var M:[UInt32] = [UInt32](count: 80, repeatedValue: 0)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    var le:UInt32 = 0
-                    chunk.getBytes(&le, range:NSRange(location:x * sizeofValue(M[x]), length: sizeofValue(M[x])));
+                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
+                    let end = start + sizeofValue(M[x])
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 14 - 17
CryptoSwift/SHA2.swift

@@ -6,16 +6,13 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
-
 final class SHA2 : HashProtocol {
     var size:Int { return variant.rawValue }
     let variant:SHA2.Variant
     
-    let message: NSData
+    let message: [UInt8]
     
-    init(_ message:NSData, variant: SHA2.Variant) {
+    init(_ message:[UInt8], variant: SHA2.Variant) {
         self.variant = variant
         self.message = message
     }
@@ -117,7 +114,7 @@ final class SHA2 : HashProtocol {
     
     //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() -> [UInt8] {
-        let tmpMessage = self.prepare(64)
+        var tmpMessage = self.prepare(64)
         
         // hash values
         var hh = [UInt32]()
@@ -126,19 +123,20 @@ final class SHA2 : HashProtocol {
         }
 		
         // append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
-        tmpMessage.appendBytes((message.length * 8).bytes(64 / 8));
+        tmpMessage += (message.count * 8).bytes(64 / 8)
         
         // Process the message in successive 512-bit chunks:
         let chunkSizeBytes = 512 / 8 // 64
-        for chunk in NSDataSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
+        for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // 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)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    var le:UInt32 = 0
-                    chunk.getBytes(&le, range:NSRange(location:x * sizeofValue(le), length: sizeofValue(le)));
+                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
+                    let end = start + sizeofValue(M[x])
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:
@@ -198,7 +196,7 @@ final class SHA2 : HashProtocol {
     }
     
     func calculate64() -> [UInt8] {
-        let tmpMessage = self.prepare(128)
+        var tmpMessage = self.prepare(128)
         
         // hash values
         var hh = [UInt64]()
@@ -208,21 +206,20 @@ final class SHA2 : HashProtocol {
 		
   
         // append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
-        tmpMessage.appendBytes((message.length * 8).bytes(64 / 8));
+        tmpMessage += (message.count * 8).bytes(64 / 8)
         
         // Process the message in successive 1024-bit chunks:
         let chunkSizeBytes = 1024 / 8 // 128
-        var leftMessageBytes = tmpMessage.length
-        for var i = 0; i < tmpMessage.length; i = i + chunkSizeBytes, leftMessageBytes -= chunkSizeBytes {
-            let chunk = tmpMessage.subdataWithRange(NSRange(location: i, length: min(chunkSizeBytes,leftMessageBytes)))
+        for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // 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)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    var le:UInt64 = 0
-                    chunk.getBytes(&le, range:NSRange(location:x * sizeofValue(le), length: sizeofValue(le)));
+                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
+                    let end = start + sizeofValue(M[x])
+                    let le = chunk[start..<end].toUInt64Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 1 - 1
CryptoSwift/UInt16Extension.swift

@@ -6,7 +6,7 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
+import Darwin.libc
 
 /** Shift bits */
 extension UInt16 {

+ 4 - 1
CryptoSwift/UInt32Extension.swift

@@ -6,7 +6,10 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
+import Darwin.libc
+
+protocol _UInt32Type { }
+extension UInt32: _UInt32Type {}
 
 /** array of bytes */
 extension UInt32 {

+ 0 - 2
CryptoSwift/UInt64Extension.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 /** array of bytes */
 extension UInt64 {
     public func bytes(totalBytes: Int = sizeof(UInt64)) -> [UInt8] {

+ 4 - 1
CryptoSwift/UInt8Extension.swift

@@ -6,7 +6,10 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
+import Darwin.libc
+
+public protocol _UInt8Type { }
+extension UInt8: _UInt8Type {}
 
 /** casting */
 extension UInt8 {

+ 0 - 2
CryptoSwiftTests/AESTests.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 27/12/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift
 

+ 0 - 2
CryptoSwiftTests/ChaCha20Tests.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 27/12/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift
 

+ 9 - 3
CryptoSwiftTests/ExtensionsTest.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 15/08/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift
 
@@ -76,6 +74,14 @@ final class ExtensionsTest: XCTestCase {
         XCTAssert(iii &<< 1 == iii << 1, "shift left failed")
         XCTAssert(iii &<< 8 == iii << 8, "shift left failed")
         XCTAssert((iii &<< 32) == 0, "shift left failed")
-
+    }
+    
+    func testtoUInt32Array() {
+        let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
+        let result = chunk.toUInt32Array()
+        
+        XCTAssert(result.count == 2, "Invalid conversion")
+        XCTAssert(result[0] == 117506305, "Invalid conversion")
+        XCTAssert(result[1] == 84148994, "Invalid conversion")
     }
 }

+ 0 - 2
CryptoSwiftTests/HMACTests.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 29/08/14.
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift
 

+ 4 - 8
CryptoSwiftTests/HashTests.swift

@@ -20,7 +20,7 @@ final class CryptoSwiftTests: XCTestCase {
     }
     
     func testMD5() {
-        let data1:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3) // "1", "2", "3"
+        let data1 = [0x31, 0x32, 0x33] as [UInt8] // "1", "2", "3"
         if let hash = Hash.md5(data1).calculate() {
             XCTAssertEqual(hash, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
         } else {
@@ -29,16 +29,12 @@ final class CryptoSwiftTests: XCTestCase {
         
         let string:NSString = ""
         let data:NSData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
-        if let hashData = Hash.md5(data).calculate() {
-            XCTAssertEqual(hashData, [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
+        if let hash = Hash.md5(data.arrayOfBytes()).calculate() {
+            XCTAssertEqual(hash, [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
         } else {
             XCTAssert(false, "Missing result")
         }
         
-        if let hash = data1.md5() {
-            XCTAssertEqual(hash.toHexString(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
-        }
-        
         if let hash = "123".md5() {
             XCTAssertEqual(hash, "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
         }
@@ -79,7 +75,7 @@ final class CryptoSwiftTests: XCTestCase {
             let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
             let data = NSData(bytes: buf, length: 2048)
             self.startMeasuring()
-            Hash.md5(data).calculate()
+            Hash.md5(data.arrayOfBytes()).calculate()
             self.stopMeasuring()
             buf.dealloc(1024)
             buf.destroy()

+ 0 - 2
CryptoSwiftTests/Helpers.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 func compareMatrix(a:[[UInt8]], b:[[UInt8]]) -> Bool {
     for (i,arr) in a.enumerate() {
         for (j,val) in arr.enumerate() {

+ 0 - 2
CryptoSwiftTests/PaddingTests.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 27/12/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift
 

+ 0 - 2
CryptoSwiftTests/Poly1305Tests.swift

@@ -5,8 +5,6 @@
 //  Created by Marcin Krzyzanowski on 29/08/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
-
-import Foundation
 import XCTest
 @testable import CryptoSwift