From 3fc9ca85590984271b70e252e00a86f4c8802f79 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 2 Aug 2016 13:25:10 +1200 Subject: [PATCH] change UpdableCryptor to accept any SequenceType instead of Array --- Sources/CryptoSwift/AES.swift | 4 ++-- Sources/CryptoSwift/UpdatableCryptor.swift | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Sources/CryptoSwift/AES.swift b/Sources/CryptoSwift/AES.swift index 2400ef0..7b6eb86 100644 --- a/Sources/CryptoSwift/AES.swift +++ b/Sources/CryptoSwift/AES.swift @@ -403,7 +403,7 @@ extension AES { self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired) } - mutating public func update(withBytes bytes:Array, isLast: Bool = false) throws -> Array { + mutating public func update(withBytes bytes:T, isLast: Bool = false) throws -> Array { self.accumulated += bytes if isLast { @@ -445,7 +445,7 @@ extension AES { self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired); } - mutating public func update(withBytes bytes:Array, isLast: Bool = false) throws -> Array { + mutating public func update(withBytes bytes:T, isLast: Bool = false) throws -> Array { self.accumulated += bytes var plaintext = Array() diff --git a/Sources/CryptoSwift/UpdatableCryptor.swift b/Sources/CryptoSwift/UpdatableCryptor.swift index dd50ab9..13c70bf 100644 --- a/Sources/CryptoSwift/UpdatableCryptor.swift +++ b/Sources/CryptoSwift/UpdatableCryptor.swift @@ -12,7 +12,7 @@ public protocol UpdatableCryptor { /// - parameter bytes: Bytes to process /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call. /// - returns: Processed data or empty array. - mutating func update(withBytes bytes:Array, isLast: Bool) throws -> Array + mutating func update(withBytes bytes:T, isLast: Bool) throws -> Array /// Encrypt/Decrypt given bytes in chunks. /// @@ -20,36 +20,44 @@ public protocol UpdatableCryptor { /// - 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:Array, isLast: Bool, output: (Array) -> Void) throws + mutating func update(withBytes bytes:T, isLast: Bool, output: (Array) -> Void) throws /// Finish encryption/decryption. This may apply padding. /// - parameter bytes: Bytes to process /// - returns: Processed data. - mutating func finish(withBytes bytes:Array) throws -> Array + mutating func finish(withBytes bytes:T) throws -> Array /// Finish encryption/decryption. This may apply padding. /// - parameter bytes: Bytes to process /// - parameter output: Resulting data /// - returns: Processed data. - mutating func finish(withBytes bytes:Array, output: (Array) -> Void) throws + mutating func finish(withBytes bytes:T, output: (Array) -> Void) throws } extension UpdatableCryptor { - mutating public func update(withBytes bytes:Array, isLast: Bool = false, output: (Array) -> Void) throws { + mutating public func update(withBytes bytes:T, isLast: Bool = false, output: (Array) -> Void) throws { let processed = try self.update(withBytes: bytes, isLast: isLast) if (!processed.isEmpty) { output(processed) } } - mutating public func finish(withBytes bytes:Array = []) throws -> Array { + mutating public func finish(withBytes bytes:T) throws -> Array { return try self.update(withBytes: bytes, isLast: true) } + + mutating public func finish() throws -> Array { + return try self.update(withBytes: [], isLast: true) + } - mutating public func finish(withBytes bytes:Array = [], output: (Array) -> Void) throws { + mutating public func finish(withBytes bytes:T, output: (Array) -> Void) throws { let processed = try self.update(withBytes: bytes, isLast: true) if (!processed.isEmpty) { output(processed) } } + + mutating public func finish(output: (Array) -> Void) throws { + try self.finish(withBytes: [], output: output) + } } \ No newline at end of file