浏览代码

Make it slice

Marcin Krzyżanowski 7 年之前
父节点
当前提交
cefa04aeef
共有 3 个文件被更改,包括 5 次插入4 次删除
  1. 1 0
      CHANGELOG
  2. 3 3
      Sources/CryptoSwift/ChaCha20.swift
  3. 1 1
      Tests/CryptoSwiftTests/ChaCha20Tests.swift

+ 1 - 0
CHANGELOG

@@ -1,5 +1,6 @@
 0.8.2
 - Fix SHA3 partial updates calculations.
+- Make ChaCha20 processing fast again.
 
 0.8.1
 - Adds Data(hex:) helper.

+ 3 - 3
Sources/CryptoSwift/ChaCha20.swift

@@ -206,12 +206,12 @@ public final class ChaCha20: BlockCipher {
     }
 
     // XORKeyStream
-    func process(bytes: Array<UInt8>, counter: inout Array<UInt8>, key: Array<UInt8>) -> Array<UInt8> {
+    func process(bytes: ArraySlice<UInt8>, counter: inout Array<UInt8>, key: Array<UInt8>) -> Array<UInt8> {
         precondition(counter.count == 16)
         precondition(key.count == 32)
 
         var block = Array<UInt8>(repeating: 0, count: ChaCha20.blockSize)
-        var bytesSlice = bytes.slice
+        var bytesSlice = bytes
         var out = Array<UInt8>(reserveCapacity: bytesSlice.count)
 
         while bytesSlice.count >= ChaCha20.blockSize {
@@ -242,7 +242,7 @@ public final class ChaCha20: BlockCipher {
 extension ChaCha20: Cipher {
 
     public func encrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
-        return process(bytes: Array(bytes), counter: &counter, key: Array(key))
+        return process(bytes: bytes, counter: &counter, key: Array(key))
     }
 
     public func decrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {

+ 1 - 1
Tests/CryptoSwiftTests/ChaCha20Tests.swift

@@ -68,7 +68,7 @@ final class ChaCha20Tests: XCTestCase {
         var counter: Array<UInt8> = [1, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 74, 0, 0, 0, 0]
         let input = Array<UInt8>.init(repeating: 0, count: 129)
         let chacha = try! ChaCha20(key: key, iv: Array(key[4..<16]))
-        let result = chacha.process(bytes: input, counter: &counter, key: key)
+        let result = chacha.process(bytes: input.slice, counter: &counter, key: key)
         XCTAssertEqual(result.toHexString(), "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e0a88837739d7bf4ef8ccacb0ea2bb9d69d56c394aa351dfda5bf459f0a2e9fe8e721f89255f9c486bf21679c683d4f9c5cf2fa27865526005b06ca374c86af3bdc")
     }