| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // Updatable.swift
- // CryptoSwift
- //
- // Copyright (C) 2014-2017 Krzyżanowski <marcin@krzyzanowskim.com>
- // This software is provided 'as-is', without any express or implied warranty.
- //
- // In no event will the authors be held liable for any damages arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
- //
- // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
- // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- // - This notice may not be removed or altered from any source or binary distribution.
- //
- /// A type that supports incremental updates. For example Digest or Cipher may be updatable
- /// and calculate result incerementally.
- public protocol Updatable {
- /// Update given bytes in chunks.
- ///
- /// - parameter bytes: Bytes to process.
- /// - parameter isLast: Indicate if given chunk is the last one. No more updates after this call.
- /// - returns: Processed data or empty array.
- mutating func update<T: Collection>(withBytes bytes: T, isLast: Bool) throws -> Array<UInt8> where T.Iterator.Element == UInt8
- /// Update given bytes in chunks.
- ///
- /// - Parameters:
- /// - bytes: Bytes to process.
- /// - isLast: Indicate if given chunk is the last one. No more updates after this call.
- /// - output: Resulting bytes callback.
- /// - Returns: Processed data or empty array.
- mutating func update<T: Collection>(withBytes bytes: T, isLast: Bool, output: (_ bytes: Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8
- /// Finish updates. This may apply padding.
- /// - parameter bytes: Bytes to process
- /// - returns: Processed data.
- mutating func finish<T: Collection>(withBytes bytes: T) throws -> Array<UInt8> where T.Iterator.Element == UInt8
- /// Finish updates. This may apply padding.
- /// - parameter bytes: Bytes to process
- /// - parameter output: Resulting data
- /// - returns: Processed data.
- mutating func finish<T: Collection>(withBytes bytes: T, output: (_ bytes: Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8
- }
- extension Updatable {
- mutating public func update<T: Collection>(withBytes bytes: T, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8 {
- let processed = try self.update(withBytes: bytes, isLast: isLast)
- if (!processed.isEmpty) {
- output(processed)
- }
- }
- mutating public func finish<T: Collection>(withBytes bytes: T) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
- return try self.update(withBytes: bytes, isLast: true)
- }
- mutating public func finish() throws -> Array<UInt8> {
- return try self.update(withBytes: [], isLast: true)
- }
- mutating public func finish<T: Collection>(withBytes bytes: T, output: (_ bytes: Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8 {
- let processed = try self.update(withBytes: bytes, isLast: true)
- if (!processed.isEmpty) {
- output(processed)
- }
- }
- mutating public func finish(output: (Array<UInt8>) -> Void) throws {
- try self.finish(withBytes: [], output: output)
- }
- }
|