浏览代码

Final changes for Swift 1.2 - it is working now.

Marcin Krzyżanowski 10 年之前
父节点
当前提交
f34f2341e1
共有 3 个文件被更改,包括 14 次插入11 次删除
  1. 2 2
      CryptoSwift/AES.swift
  2. 10 7
      CryptoSwift/CipherBlockMode.swift
  3. 2 2
      CryptoSwiftTests/HashTests.swift

+ 2 - 2
CryptoSwift/AES.swift

@@ -156,7 +156,7 @@ public class AES {
         return out == nil ? nil : NSData.withBytes(out!)
     }
     
-    private func encryptBlock(block:[UInt8]) -> [UInt8] {
+    private func encryptBlock(block:[UInt8]) -> [UInt8]? {
         let expandedKey = expandKey()
         
         var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
@@ -212,7 +212,7 @@ public class AES {
         return out == nil ? nil : NSData.withBytes(out!)
     }
     
-    private func decryptBlock(block:[UInt8]) -> [UInt8] {
+    private func decryptBlock(block:[UInt8]) -> [UInt8]? {
         let expandedKey = expandKey()
         
         var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))

+ 10 - 7
CryptoSwift/CipherBlockMode.swift

@@ -8,6 +8,8 @@
 
 import Foundation
 
+typealias CipherWorker = (block: [UInt8]) -> [UInt8]?
+
 public enum CipherBlockMode {
     case ECB, CBC, CFB
     
@@ -20,6 +22,7 @@ public enum CipherBlockMode {
         }
     }
     
+    
     /**
     Process input blocks with given block cipher mode. With fallback to plain mode.
     
@@ -29,7 +32,7 @@ public enum CipherBlockMode {
     
     :returns: encrypted bytes
     */
-    func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
         
         // if IV is not available, fallback to plain
         var finalBlockMode:CipherBlockMode = self
@@ -69,7 +72,7 @@ public enum CipherBlockMode {
 *  Cipher-block chaining (CBC)
 */
 private struct CBCMode {
-    static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
         
         if (iv == nil) {
             assertionFailure("CBC require IV")
@@ -100,7 +103,7 @@ private struct CBCMode {
         return out;
     }
     
-    static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
         if (iv == nil) {
             assertionFailure("CBC require IV")
             return nil
@@ -132,7 +135,7 @@ private struct CBCMode {
 *  Cipher feedback (CFB)
 */
 private struct CFBMode {
-    static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
         
         if (iv == nil) {
             assertionFailure("CFB require IV")
@@ -160,7 +163,7 @@ private struct CFBMode {
         return out;
     }
     
-    static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
         if (iv == nil) {
             assertionFailure("CFB require IV")
             return nil
@@ -194,7 +197,7 @@ private struct CFBMode {
 *  Electronic codebook (ECB)
 */
 private struct ECBMode {
-    static func encryptBlocks(blocks:[[UInt8]], cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func encryptBlocks(blocks:[[UInt8]], cipher:CipherWorker) -> [UInt8]? {
         var out:[UInt8]?
         for (idx,plaintext) in enumerate(blocks) {
             if let encrypted = cipher(block: plaintext) {
@@ -209,7 +212,7 @@ private struct ECBMode {
         return out
     }
     
-    static func decryptBlocks(blocks:[[UInt8]], cipher:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
+    static func decryptBlocks(blocks:[[UInt8]], cipher:CipherWorker) -> [UInt8]? {
         return encryptBlocks(blocks, cipher: cipher)
     }
 }

+ 2 - 2
CryptoSwiftTests/HashTests.swift

@@ -76,7 +76,7 @@ class CryptoSwiftTests: XCTestCase {
     
     func testMD5PerformanceSwift() {
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
-            let buf = UnsafeMutablePointer<UInt8>(calloc(2048, UInt(sizeof(UInt8))))
+            let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
             let data = NSData(bytes: buf, length: 2048)
             self.startMeasuring()
             for _ in [0...1000] {
@@ -90,7 +90,7 @@ class CryptoSwiftTests: XCTestCase {
     
     func testMD5PerformanceCommonCrypto() {
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
-            let buf = UnsafeMutablePointer<UInt8>(calloc(2048, UInt(sizeof(UInt8))))
+            let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
             let data = NSData(bytes: buf, length: 2048)
             self.startMeasuring()
             for _ in [0...1000] {