Эх сурвалжийг харах

Performance test for ChaCha20

Marcin Krzyżanowski 10 жил өмнө
parent
commit
bd558e205a

+ 3 - 3
CryptoSwift/ChaCha20.swift

@@ -24,7 +24,7 @@ public class ChaCha20 {
         }
     }
     
-    init?(key:[UInt8], iv:[UInt8]) {
+    public init?(key:[UInt8], iv:[UInt8]) {
         if let c = contextSetup(iv: iv, key: key) {
             context = c
         } else {
@@ -32,7 +32,7 @@ public class ChaCha20 {
         }
     }
     
-    func encrypt(bytes:[UInt8]) -> [UInt8]? {
+    public func encrypt(bytes:[UInt8]) -> [UInt8]? {
         if (context == nil) {
             return nil
         }
@@ -40,7 +40,7 @@ public class ChaCha20 {
         return encryptBytes(bytes)
     }
     
-    func decrypt(bytes:[UInt8]) -> [UInt8]? {
+    public func decrypt(bytes:[UInt8]) -> [UInt8]? {
         return encrypt(bytes)
     }
     

+ 12 - 0
CryptoSwiftTests/ChaCha20Tests.swift

@@ -70,4 +70,16 @@ class ChaCha20Tests: XCTestCase {
             }
         }
     }
+    
+    func testChaCha20Performance() {
+        let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        let iv:[UInt8] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
+        let message = [UInt8](count: 1024 * 1024, repeatedValue: 7)
+        self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
+            self.startMeasuring()
+            let encrypted = ChaCha20(key: key, iv: iv)?.encrypt(message)
+            self.stopMeasuring()
+            XCTAssert(encrypted != nil, "not encrypted")
+        })
+    }
 }