Przeglądaj źródła

Remove unused NSDataSequence. Decouple Utils.swift. Refactoring.

Marcin Krzyżanowski 10 lat temu
rodzic
commit
6fff1dc5cb

+ 20 - 12
CryptoSwift.xcodeproj/project.pbxproj

@@ -24,8 +24,11 @@
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		75164E4919AD30AC00737F30 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75164E4819AD30AC00737F30 /* Utils.swift */; };
 		751C5C3D19B26B000094C75D /* Poly1305.swift in Sources */ = {isa = PBXBuildFile; fileRef = 751C5C3C19B26B000094C75D /* Poly1305.swift */; };
+		753403F31BB8864F0039099C /* Utils+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753403F21BB8864F0039099C /* Utils+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
+		753403F51BB892ED0039099C /* Array<UInt8>+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753403F41BB892ED0039099C /* Array<UInt8>+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
+		753403F71BB898470039099C /* Array<UInt8>+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753403F61BB898470039099C /* Array<UInt8>+Extension.swift */; settings = {ASSET_TAGS = (); }; };
 		7539E3291B3B4A530037F4E1 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750A545F1992D2680017DA75 /* MD5.swift */; };
-		7539E32A1B3B4C480037F4E1 /* NSDataExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752DEF7619693EA000E17557 /* NSDataExtension.swift */; };
+		7539E32A1B3B4C480037F4E1 /* NSData+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752DEF7619693EA000E17557 /* NSData+Extension.swift */; };
 		7539E32B1B3B4C6B0037F4E1 /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758A94251A65AEB100E46135 /* HMAC.swift */; };
 		7539E32C1B3B4C750037F4E1 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* StringExtension.swift */; };
 		7539E32D1B3B4E950037F4E1 /* SHA1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752E087A199FF27C005B0EA0 /* SHA1.swift */; };
@@ -33,7 +36,7 @@
 		7547195119931802002FA5F1 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7547195019931802002FA5F1 /* IntExtension.swift */; };
 		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 */; };
+		754C8FED19979F94005AD904 /* Array+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754C8FEC19979F94005AD904 /* Array+Extension.swift */; };
 		754DD76E19A149AF00E52288 /* HashProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754DD76D19A149AF00E52288 /* HashProtocol.swift */; };
 		755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755111E719B7B7DF00C2AD86 /* Authenticator.swift */; };
 		7552614E1993051E000D2B20 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7552614D1993051E000D2B20 /* Hash.swift */; };
@@ -56,7 +59,6 @@
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
 		75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */; };
 		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 = (); }; };
@@ -137,8 +139,11 @@
 		75153D4119AA3C7900750381 /* SHA2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA2.swift; sourceTree = "<group>"; };
 		75164E4819AD30AC00737F30 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
 		751C5C3C19B26B000094C75D /* Poly1305.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305.swift; sourceTree = "<group>"; };
-		752DEF7619693EA000E17557 /* NSDataExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataExtension.swift; sourceTree = "<group>"; };
+		752DEF7619693EA000E17557 /* NSData+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSData+Extension.swift"; sourceTree = "<group>"; };
 		752E087A199FF27C005B0EA0 /* SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA1.swift; sourceTree = "<group>"; };
+		753403F21BB8864F0039099C /* Utils+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Utils+Foundation.swift"; sourceTree = "<group>"; };
+		753403F41BB892ED0039099C /* Array<UInt8>+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array<UInt8>+Foundation.swift"; sourceTree = "<group>"; };
+		753403F61BB898470039099C /* Array<UInt8>+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array<UInt8>+Extension.swift"; sourceTree = "<group>"; };
 		75445820196AA2A5002FF20E /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
 		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; };
@@ -147,7 +152,7 @@
 		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>"; };
 		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>"; };
+		754C8FEC19979F94005AD904 /* Array+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Extension.swift"; sourceTree = "<group>"; };
 		754DD76D19A149AF00E52288 /* HashProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HashProtocol.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>"; };
@@ -171,7 +176,6 @@
 		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>"; };
 		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>"; };
@@ -255,15 +259,17 @@
 				758C764219B61DE900653BC6 /* UInt16Extension.swift */,
 				75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */,
 				75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */,
-				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
-				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
+				752DEF7619693EA000E17557 /* NSData+Extension.swift */,
+				754C8FEC19979F94005AD904 /* Array+Extension.swift */,
+				753403F61BB898470039099C /* Array<UInt8>+Extension.swift */,
+				753403F41BB892ED0039099C /* Array<UInt8>+Foundation.swift */,
 				75D63F761BB840050041579B /* ArraySlice+Bytes.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
 				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
+				753403F21BB8864F0039099C /* Utils+Foundation.swift */,
 				758C764019B61AE500653BC6 /* Generics.swift */,
 				75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */,
-				75D2D19F1B5689EB000A2615 /* NSDataSequence.swift */,
 				75D63F741BB711270041579B /* BytesSequence.swift */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 			);
@@ -439,23 +445,23 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				75D2D1A01B5689EB000A2615 /* NSDataSequence.swift in Sources */,
 				75D94E2819B60DDE007CB2A4 /* UInt64Extension.swift in Sources */,
 				751C5C3D19B26B000094C75D /* Poly1305.swift in Sources */,
 				7552614E1993051E000D2B20 /* Hash.swift in Sources */,
+				753403F51BB892ED0039099C /* Array<UInt8>+Foundation.swift in Sources */,
 				75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */,
 				75BE4EB11B1E4A9F007A2B57 /* IntegerConvertible.swift in Sources */,
 				7539E32E1B3B4E970037F4E1 /* SHA2.swift in Sources */,
 				7539E32C1B3B4C750037F4E1 /* StringExtension.swift in Sources */,
 				7563B2E819B14D4300B152CD /* Cipher.swift in Sources */,
 				7539E32B1B3B4C6B0037F4E1 /* HMAC.swift in Sources */,
-				7539E32A1B3B4C480037F4E1 /* NSDataExtension.swift in Sources */,
+				7539E32A1B3B4C480037F4E1 /* NSData+Extension.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
 				75A663A61AA0CAD00052110B /* Padding.swift in Sources */,
 				75164E4919AD30AC00737F30 /* Utils.swift in Sources */,
 				75D63F751BB711270041579B /* BytesSequence.swift in Sources */,
 				759D481119B517BC005FF7FC /* BitExtension.swift in Sources */,
-				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
+				754C8FED19979F94005AD904 /* Array+Extension.swift in Sources */,
 				7539E3291B3B4A530037F4E1 /* MD5.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
 				758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */,
@@ -470,9 +476,11 @@
 				75D63F771BB840050041579B /* ArraySlice+Bytes.swift in Sources */,
 				754C30B71AA13BC000E6FFA4 /* PKCS7.swift in Sources */,
 				755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */,
+				753403F71BB898470039099C /* Array<UInt8>+Extension.swift in Sources */,
 				754DD76E19A149AF00E52288 /* HashProtocol.swift in Sources */,
 				758C764119B61AE500653BC6 /* Generics.swift in Sources */,
 				758F3F781992F6CE0014BBDA /* UInt8Extension.swift in Sources */,
+				753403F31BB8864F0039099C /* Utils+Foundation.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

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

@@ -37,7 +37,7 @@
       </BuildActionEntries>
    </BuildAction>
    <TestAction
-      buildConfiguration = "Debug"
+      buildConfiguration = "Release"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       shouldUseLaunchSchemeArgsEnv = "YES">

+ 0 - 13
CryptoSwift/ArrayExtension.swift → CryptoSwift/Array+Extension.swift

@@ -24,16 +24,3 @@ extension Array {
     }
 }
 
-// MARK: - Array<UInt8>
-
-extension Array where Element: _UInt8Type {
-    public func toHexString() -> String {
-        var s:String = "";
-        for byte in self {
-            s = s + String(format:"%02x", byte as! UInt8)
-        }
-        return s
-    }
-}
-
-

+ 47 - 0
CryptoSwift/Array<UInt8>+Extension.swift

@@ -0,0 +1,47 @@
+//
+//  ArrayUInt8+CryptoSwift.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+extension Array where Element: _UInt8Type {
+    
+    public func toHexString() -> String {
+        var s:String = ""
+        for byte in self {
+            s = s + String(format:"%02x", byte as! UInt8)
+        }
+        return s
+    }
+    
+    public func md5() -> [UInt8]? {
+        return Hash.md5(Element.arrayValue(self)).calculate()
+    }
+    
+    public func sha1() -> [UInt8]? {
+        return Hash.sha1(Element.arrayValue(self)).calculate()
+    }
+
+    public func sha224() -> [UInt8]? {
+        return Hash.sha224(Element.arrayValue(self)).calculate()
+    }
+
+    public func sha256() -> [UInt8]? {
+        return Hash.sha256(Element.arrayValue(self)).calculate()
+    }
+
+    public func sha384() -> [UInt8]? {
+        return Hash.sha384(Element.arrayValue(self)).calculate()
+    }
+
+    public func sha512() -> [UInt8]? {
+        return Hash.sha512(Element.arrayValue(self)).calculate()
+    }
+    
+    public func crc32() -> [UInt8]? {
+        return Hash.crc32(Element.arrayValue(self)).calculate()
+    }
+
+}

+ 22 - 0
CryptoSwift/Array<UInt8>+Foundation.swift

@@ -0,0 +1,22 @@
+//
+//  Array+Foundation.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+extension _UInt8Type {
+    private static func Zero() -> Self {
+        return (0 as UInt8) as! Self
+    }
+}
+
+extension Array where Element: _UInt8Type {
+    public init(_ data: NSData) {
+        self = Array<Element>(count: data.length, repeatedValue: Element.Zero())
+        data.getBytes(&self, length: self.count)
+    }
+}

+ 0 - 1
CryptoSwift/ArraySlice+Bytes.swift

@@ -7,7 +7,6 @@
 //
 
 extension ArraySlice where Element: _UInt8Type {
-    typealias Element = UInt8
     
     func toUInt32Array() -> Array<UInt32> {
         var result = Array<UInt32>()

+ 0 - 0
CryptoSwift/NSDataExtension.swift → CryptoSwift/NSData+Extension.swift


+ 0 - 26
CryptoSwift/NSDataSequence.swift

@@ -1,26 +0,0 @@
-//
-//  NSDataSequence.swift
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 15/07/15.
-//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
-//
-
-import Foundation
-
-struct NSDataSequence: SequenceType {
-    
-    let chunkSize: Int
-    let data: NSData
-    
-    func generate() -> AnyGenerator<NSData> {
-        
-        var offset:Int = 0
-        
-        return anyGenerator {
-            let result = self.data.subdataWithRange(NSRange(location: offset, length: min(self.chunkSize, self.data.length - offset)))
-            offset += result.length
-            return result.length > 0 ? result : nil
-        }
-    }
-}

+ 11 - 1
CryptoSwift/UInt8Extension.swift

@@ -8,7 +8,17 @@
 
 import Darwin.libc
 
-public protocol _UInt8Type { }
+public protocol _UInt8Type {
+    /// [Hack](https://twitter.com/jckarter/status/648235521127256064) for array `value`
+    static func arrayValue(array: [Self]) -> [UInt8]
+}
+
+extension _UInt8Type {
+    static public func arrayValue(array: [Self]) -> [Self] {
+        return array
+    }
+}
+
 extension UInt8: _UInt8Type {}
 
 /** casting */

+ 20 - 0
CryptoSwift/Utils+Foundation.swift

@@ -0,0 +1,20 @@
+//
+//  Utils+Foundation.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 27/09/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+func perf(text: String, closure: () -> ()) {
+    let measurementStart = NSDate();
+    
+    closure()
+    
+    let measurementStop = NSDate();
+    let executionTime = measurementStop.timeIntervalSinceDate(measurementStart)
+    
+    print("\(text) \(executionTime)");
+}

+ 0 - 13
CryptoSwift/Utils.swift

@@ -6,8 +6,6 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-import Foundation
-
 func rotateLeft(v:UInt8, _ n:UInt8) -> UInt8 {
     return ((v << n) & 0xFF) | (v >> (8 - n))
 }
@@ -46,15 +44,4 @@ func xor(a: [UInt8], b:[UInt8]) -> [UInt8] {
         xored[i] = a[i] ^ b[i]
     }
     return xored
-}
-
-func perf(text: String, closure: () -> ()) {
-    let measurementStart = NSDate();
-    
-    closure()
-    
-    let measurementStop = NSDate();
-    let executionTime = measurementStop.timeIntervalSinceDate(measurementStart)
-    
-    print("\(text) \(executionTime)");
 }