浏览代码

refactor enums, cleanup

Marcin Krzyżanowski 11 年之前
父节点
当前提交
edc874ed45

+ 8 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -19,8 +19,9 @@
 		754BE46819693E190098E6F3 /* HashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754BE46719693E190098E6F3 /* HashTests.swift */; };
 		754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754C8FEC19979F94005AD904 /* ArrayExtension.swift */; };
 		754DD76E19A149AF00E52288 /* CryptoHashBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 754DD76D19A149AF00E52288 /* CryptoHashBase.swift */; };
-		7552614E1993051E000D2B20 /* CryptoHash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7552614D1993051E000D2B20 /* CryptoHash.swift */; };
+		7552614E1993051E000D2B20 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7552614D1993051E000D2B20 /* Hash.swift */; };
 		755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755FB1D9199E347D00475437 /* ExtensionsTest.swift */; };
+		7563B2E819B14D4300B152CD /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7563B2E719B14D4300B152CD /* Cipher.swift */; };
 		757EF7F519AAA82400586276 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757EF7F419AAA82400586276 /* CRC.swift */; };
 		758F3F761992E57D0014BBDA /* Playground in Resources */ = {isa = PBXBuildFile; fileRef = 758F3F751992E57D0014BBDA /* Playground */; };
 		758F3F781992F6CE0014BBDA /* ByteExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F3F771992F6CE0014BBDA /* ByteExtension.swift */; };
@@ -110,8 +111,9 @@
 		754BE46719693E190098E6F3 /* HashTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashTests.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>"; };
-		7552614D1993051E000D2B20 /* CryptoHash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CryptoHash.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>"; };
+		7563B2E719B14D4300B152CD /* Cipher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cipher.swift; sourceTree = "<group>"; };
 		757EF7F419AAA82400586276 /* CRC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CRC.swift; sourceTree = "<group>"; };
 		758F3F751992E57D0014BBDA /* Playground */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Playground; path = CryptoSwift/Playground; sourceTree = "<group>"; };
 		758F3F771992F6CE0014BBDA /* ByteExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteExtension.swift; sourceTree = "<group>"; };
@@ -171,7 +173,8 @@
 			isa = PBXGroup;
 			children = (
 				754BE45A19693E190098E6F3 /* CryptoSwift.h */,
-				7552614D1993051E000D2B20 /* CryptoHash.swift */,
+				7552614D1993051E000D2B20 /* Hash.swift */,
+				7563B2E719B14D4300B152CD /* Cipher.swift */,
 				754DD76D19A149AF00E52288 /* CryptoHashBase.swift */,
 				757EF7F419AAA82400586276 /* CRC.swift */,
 				750A545F1992D2680017DA75 /* MD5.swift */,
@@ -332,8 +335,9 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				7552614E1993051E000D2B20 /* CryptoHash.swift in Sources */,
+				7552614E1993051E000D2B20 /* Hash.swift in Sources */,
 				7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */,
+				7563B2E819B14D4300B152CD /* Cipher.swift in Sources */,
 				752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
 				750A54601992D2680017DA75 /* MD5.swift in Sources */,

+ 1 - 1
CryptoSwift/CRC.swift

@@ -10,7 +10,7 @@ import Foundation
 
 class CRC {
     
-    let table:[UInt32] = [0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
+    private let table:[UInt32] = [0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
         0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
         0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
         0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,

+ 11 - 29
CryptoSwift/ChaCha20.swift

@@ -9,8 +9,6 @@
 import Foundation
 
 public class ChaCha20 {
-    let keySize = 32
-    let nonceSize = 8
     let stateSize = 16
     let blockSize = 16 * 4
     
@@ -21,7 +19,6 @@ public class ChaCha20 {
     public init() {
     }
     
-    // checked
     private func wordToByte(input:[UInt32] /* 64 */) -> [Byte]? /* 16 */ {
         if (input.count != stateSize) {
             return nil;
@@ -41,20 +38,12 @@ public class ChaCha20 {
             quarterround(&x[3], &x[4], &x[9],  &x[14])
             i -= 2
         }
-        
-        for i in 0..<16 {
-            x[i] = x[i] &+ input[i]
-        }
 
-        var output:[Byte] = [Byte](count: 64, repeatedValue: 0)
-        
-        // build output
-        // TODO: improve
+        var output = [Byte]()
+
         for i in 0..<16 {
-            let reversed = x[i].bytes().reverse()
-            for j in 0..<reversed.count {
-                output[(i*4)+j] = reversed[j]
-            }
+            x[i] = x[i] &+ input[i]
+            output += x[i].bytes().reverse()
         }
 
         return output;
@@ -91,25 +80,18 @@ public class ChaCha20 {
         break;
         }
         
+        // 8 - 11
         for (var i = 0; i < 4; i++) {
             let start = addPos + (i*4)
             context.input[i + 8] = UInt32.withBytes(key[start..<(start + 4)]).bigEndian
         }
 
-        // iv - checked
+        // iv
         context.input[12] = 0
         context.input[13] = 0
         context.input[14] = UInt32.withBytes(iv[0..<4]).bigEndian
         context.input[15] = UInt32.withBytes(iv[4..<8]).bigEndian
         
-        println(iv)
-        
-        context.input.map({ (value) -> () in
-            var st: String = NSString(format:"%02X", value)
-            print(st)
-        })
-        println()
-
         return context
     }
     
@@ -132,18 +114,18 @@ public class ChaCha20 {
                     context.input[13] = context.input[13] &+ 1
                     /* stopping at 2^70 bytes per nonce is user's responsibility */
                 }
-                if (bytes <= 64) {
+                if (bytes <= blockSize) {
                     for (var i = 0; i < bytes; i++) {
                         c[i + cPos] = message[i + mPos] ^ output[i]
                     }
                     return c
                 }
-                for (var i = 0; i < 64; i++) {
+                for (var i = 0; i < blockSize; i++) {
                     c[i + cPos] = message[i + mPos] ^ output[i]
                 }
-                bytes -= 64
-                cPos += 64
-                mPos += 64
+                bytes -= blockSize
+                cPos += blockSize
+                mPos += blockSize
             }
         }
     }

+ 13 - 0
CryptoSwift/Cipher.swift

@@ -0,0 +1,13 @@
+//
+//  Cipher.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 30/08/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+public enum Cipher {
+    case ChaCha20
+}

+ 1 - 1
CryptoSwift/CryptoHash.swift → CryptoSwift/Hash.swift

@@ -8,7 +8,7 @@
 
 import Foundation
 
-public enum CryptoHash {
+public enum Hash {
     case md5
     case sha1, sha224, sha256, sha384, sha512
     case ripemd160

+ 3 - 3
CryptoSwift/NSDataExtension.swift

@@ -70,10 +70,10 @@ extension NSData {
         self.getBytes(&bytesArray, length:count * sizeof(Byte))
         
         var s:String = "";
-        for(var i = 0; i < bytesArray.count; i++) {
-            var st: String = NSString(format:"%02X", bytesArray[i])
+        bytesArray.map({ (byte) -> () in
+            var st: String = NSString(format:"%02X", byte)
             s = s + st
-        }
+        })
         return s;
     }
     

+ 0 - 14
CryptoSwiftTests/CipherTests.swift

@@ -14,11 +14,9 @@ class CipherTests: XCTestCase {
 
     override func setUp() {
         super.setUp()
-        // Put setup code here. This method is called before the invocation of each test method in the class.
     }
     
     override func tearDown() {
-        // Put teardown code here. This method is called after the invocation of each test method in the class.
         super.tearDown()
     }
 
@@ -62,17 +60,5 @@ class CipherTests: XCTestCase {
             
             XCTAssertEqual(encrypted.hexString, expected, "ChaCha20 failed");
         }
-        
-//        let key:NSData = NSData(bytes: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] as [Byte], length: 32)
-//        let iv:NSData = NSData(bytes: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] as [Byte], length: 32)
-
     }
-
-//    func testPerformanceExample() {
-//        // This is an example of a performance test case.
-//        self.measureBlock() {
-//            // Put the code you want to measure the time of here.
-//        }
-//    }
-
 }

+ 2 - 2
README.md

@@ -24,7 +24,7 @@ Good mood
 
 ##Usage
 
-Generally you should use `CryptoHash` enum or convenience extensions
+Generally you should use `CryptoSwift.Hash`,`CryptoSwift.Cipher` enums or convenience extensions
 
 CryptoHash enum usage
 
@@ -32,7 +32,7 @@ CryptoHash enum usage
     
     /* CryptoHash enum usage */
     var data:NSData = NSData(bytes: [49, 50, 51] as [Byte], length: 3)
-    if let data = CryptoHash.md5.hash(data) {
+    if let data = CryptoSwift.Hash.md5.hash(data) {
         println(data.hexString)
     }