Explorar el Código

Add trailing closure with result of Cryptor

Marcin Krzyżanowski hace 9 años
padre
commit
5663670795
Se han modificado 1 ficheros con 29 adiciones y 1 borrados
  1. 29 1
      Sources/CryptoSwift/UpdatableCryptor.swift

+ 29 - 1
Sources/CryptoSwift/UpdatableCryptor.swift

@@ -14,14 +14,42 @@ public protocol UpdatableCryptor {
     /// - returns: Processed data or empty array.
     /// - returns: Processed data or empty array.
     mutating func update(withBytes bytes:[UInt8], isLast: Bool) throws -> [UInt8]
     mutating func update(withBytes bytes:[UInt8], isLast: Bool) throws -> [UInt8]
 
 
+    /// Encrypt/Decrypt given bytes in chunks.
+    ///
+    /// - parameter bytes: Bytes to process
+    /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
+    /// - parameter output: Resulting data
+    /// - returns: Processed data or empty array.
+    mutating func update(withBytes bytes:[UInt8], isLast: Bool, output: (Array<UInt8>) -> Void) throws
+
+    /// Finish encryption/decryption. This may apply padding.
+    /// - parameter bytes: Bytes to process
+    /// - returns: Processed data.
+    mutating func finish(withBytes bytes:[UInt8]) throws -> [UInt8]
+
     /// Finish encryption/decryption. This may apply padding.
     /// Finish encryption/decryption. This may apply padding.
     /// - parameter bytes: Bytes to process
     /// - parameter bytes: Bytes to process
+    /// - parameter output: Resulting data
     /// - returns: Processed data.
     /// - returns: Processed data.
-    mutating func finish(withBytes bytes:[UInt8]) throws  -> [UInt8]
+    mutating func finish(withBytes bytes:[UInt8], output: (Array<UInt8>) -> Void) throws
 }
 }
 
 
 extension UpdatableCryptor {
 extension UpdatableCryptor {
+    mutating public func update(withBytes bytes:[UInt8], isLast: Bool = false, output: (Array<UInt8>) -> Void) throws {
+        let processed = try self.update(withBytes: bytes, isLast: isLast)
+        if (!processed.isEmpty) {
+            output(processed)
+        }
+    }
+
     mutating public func finish(withBytes bytes:[UInt8] = []) throws  -> [UInt8] {
     mutating public func finish(withBytes bytes:[UInt8] = []) throws  -> [UInt8] {
         return try self.update(withBytes: bytes, isLast: true)
         return try self.update(withBytes: bytes, isLast: true)
     }
     }
+
+    mutating public func finish(withBytes bytes:[UInt8] = [], output: (Array<UInt8>) -> Void) throws {
+        let processed = try self.update(withBytes: bytes, isLast: true)
+        if (!processed.isEmpty) {
+            output(processed)
+        }
+    }
 }
 }