Updatable.swift 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // 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:
  10. //
  11. // - 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.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. /// A type that supports incremental updates. For example Digest or Cipher may be updatable
  16. /// and calculate result incerementally.
  17. public protocol Updatable {
  18. /// Update given bytes in chunks.
  19. ///
  20. /// - parameter bytes: Bytes to process.
  21. /// - parameter isLast: Indicate if given chunk is the last one. No more updates after this call.
  22. /// - returns: Processed data or empty array.
  23. mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool) throws -> Array<UInt8>
  24. /// Update given bytes in chunks.
  25. ///
  26. /// - Parameters:
  27. /// - bytes: Bytes to process.
  28. /// - isLast: Indicate if given chunk is the last one. No more updates after this call.
  29. /// - output: Resulting bytes callback.
  30. /// - Returns: Processed data or empty array.
  31. mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool, output: (_ bytes: Array<UInt8>) -> Void) throws
  32. /// Finish updates. This may apply padding.
  33. /// - parameter bytes: Bytes to process
  34. /// - returns: Processed data.
  35. mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8>
  36. /// Finish updates. This may apply padding.
  37. /// - parameter bytes: Bytes to process
  38. /// - parameter output: Resulting data
  39. /// - returns: Processed data.
  40. mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws
  41. }
  42. extension Updatable {
  43. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  44. let processed = try update(withBytes: bytes, isLast: isLast)
  45. if !processed.isEmpty {
  46. output(processed)
  47. }
  48. }
  49. @discardableResult
  50. public mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
  51. return try update(withBytes: bytes, isLast: true)
  52. }
  53. @discardableResult
  54. public mutating func finish() throws -> Array<UInt8> {
  55. return try update(withBytes: [], isLast: true)
  56. }
  57. public mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  58. let processed = try update(withBytes: bytes, isLast: true)
  59. if !processed.isEmpty {
  60. output(processed)
  61. }
  62. }
  63. public mutating func finish(output: (Array<UInt8>) -> Void) throws {
  64. try finish(withBytes: [], output: output)
  65. }
  66. }
  67. extension Updatable {
  68. @discardableResult
  69. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  70. return try update(withBytes: bytes.slice, isLast: isLast)
  71. }
  72. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  73. return try update(withBytes: bytes.slice, isLast: isLast, output: output)
  74. }
  75. @discardableResult
  76. public mutating func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
  77. return try finish(withBytes: bytes.slice)
  78. }
  79. public mutating func finish(withBytes bytes: Array<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  80. return try finish(withBytes: bytes.slice, output: output)
  81. }
  82. }