Selaa lähdekoodia

Cipher protocol accepts Collection input

Marcin Krzyżanowski 9 vuotta sitten
vanhempi
commit
0f5b960e25

+ 4 - 4
Sources/CryptoSwift/AES.swift

@@ -512,8 +512,8 @@ extension AES: Cryptors {
 
 // MARK: Cipher
 extension AES: Cipher {
-    public func encrypt(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
-        let chunks = bytes.chunks(size: AES.blockSize)
+    public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
+        let chunks = Array(bytes).chunks(size: AES.blockSize)
 
         var oneTimeCryptor = self.makeEncryptor()
         var out = Array<UInt8>()
@@ -529,13 +529,13 @@ extension AES: Cipher {
         return out
     }
 
-    public func decrypt(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
+    public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         if blockMode.options.contains(.PaddingRequired) && (bytes.count % AES.blockSize != 0) {
             throw Error.dataPaddingRequired
         }
 
         var oneTimeCryptor = self.makeDecryptor()
-        let chunks = bytes.chunks(size: AES.blockSize)
+        let chunks = Array(bytes).chunks(size: AES.blockSize)
         var out = Array<UInt8>()
         out.reserveCapacity(bytes.count)
         for idx in chunks.indices {

+ 9 - 9
Sources/CryptoSwift/ChaCha20.swift

@@ -114,13 +114,13 @@ final public class ChaCha20: BlockCipher {
 
 // MARK: Cipher
 extension ChaCha20: Cipher {
-    public func encrypt(_ message:Array<UInt8>) throws -> Array<UInt8> {
+    public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         var ctx = context
-        var c = Array<UInt8>(repeating: 0, count: message.count)
+        var c = Array<UInt8>(repeating: 0, count: bytes.count)
 
         var cPos:Int = 0
         var mPos:Int = 0
-        var bytes = message.count
+        var bytesCount = bytes.count
 
         while (true) {
             if let output = wordToByte(ctx.input) {
@@ -129,23 +129,23 @@ extension ChaCha20: Cipher {
                     ctx.input[13] = ctx.input[13] &+ 1
                     /* stopping at 2^70 bytes per nonce is user's responsibility */
                 }
-                if (bytes <= ChaCha20.blockSize) {
-                    for i in 0..<bytes {
-                        c[i + cPos] = message[i + mPos] ^ output[i]
+                if (bytesCount <= ChaCha20.blockSize) {
+                    for i in 0..<bytesCount {
+                        c[i + cPos] = bytes[i + mPos] ^ output[i]
                     }
                     return c
                 }
                 for i in 0..<ChaCha20.blockSize {
-                    c[i + cPos] = message[i + mPos] ^ output[i]
+                    c[i + cPos] = bytes[i + mPos] ^ output[i]
                 }
-                bytes -= ChaCha20.blockSize
+                bytesCount -= ChaCha20.blockSize
                 cPos += ChaCha20.blockSize
                 mPos += ChaCha20.blockSize
             }
         }
     }
 
-    public func decrypt(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
+    public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         return try encrypt(bytes)
     }
 }

+ 2 - 2
Sources/CryptoSwift/Cipher.swift

@@ -16,11 +16,11 @@ public protocol Cipher: class {
     ///
     /// - parameter bytes: Plaintext data
     /// - returns: Encrypted data
-    func encrypt(_ bytes: Array<UInt8>) throws -> Array<UInt8>
+    func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int
 
     /// Decrypt given bytes at once
     ///
     /// - parameter bytes: Ciphertext data
     /// - returns: Plaintext data
-    func decrypt(_ bytes: Array<UInt8>) throws -> Array<UInt8>
+    func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int
 }

+ 2 - 2
Sources/CryptoSwift/Rabbit.swift

@@ -179,7 +179,7 @@ final public class Rabbit: BlockCipher {
 
 // MARK: Cipher
 extension Rabbit: Cipher {
-    public func encrypt(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    public func encrypt<C: Collection>(_ bytes: C) -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         setup()
 
         var result = Array<UInt8>(repeating: 0, count: bytes.count)
@@ -200,7 +200,7 @@ extension Rabbit: Cipher {
         return result
     }
 
-    public func decrypt(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         return encrypt(bytes)
     }
 }