Updatable.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 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 partial result 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 partial result data or empty array.
  31. mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool, output: (_ bytes: Array<UInt8>) -> Void) throws
  32. }
  33. extension Updatable {
  34. @inlinable
  35. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  36. let processed = try update(withBytes: bytes, isLast: isLast)
  37. if !processed.isEmpty {
  38. output(processed)
  39. }
  40. }
  41. @inlinable
  42. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  43. try self.update(withBytes: bytes, isLast: isLast)
  44. }
  45. @inlinable
  46. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  47. try self.update(withBytes: bytes.slice, isLast: isLast)
  48. }
  49. @inlinable
  50. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  51. try self.update(withBytes: bytes.slice, isLast: isLast, output: output)
  52. }
  53. /// Finish updates. This may apply padding.
  54. /// - parameter bytes: Bytes to process
  55. /// - returns: Processed data.
  56. @inlinable
  57. public mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
  58. try self.update(withBytes: bytes, isLast: true)
  59. }
  60. @inlinable
  61. public mutating func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
  62. try self.finish(withBytes: bytes.slice)
  63. }
  64. /// Finish updates. May add padding.
  65. ///
  66. /// - Returns: Processed data
  67. /// - Throws: Error
  68. @inlinable
  69. public mutating func finish() throws -> Array<UInt8> {
  70. try self.update(withBytes: [], isLast: true)
  71. }
  72. /// Finish updates. This may apply padding.
  73. /// - parameter bytes: Bytes to process
  74. /// - parameter output: Resulting data
  75. /// - returns: Processed data.
  76. @inlinable
  77. public mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  78. let processed = try update(withBytes: bytes, isLast: true)
  79. if !processed.isEmpty {
  80. output(processed)
  81. }
  82. }
  83. @inlinable
  84. public mutating func finish(withBytes bytes: Array<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  85. try self.finish(withBytes: bytes.slice, output: output)
  86. }
  87. /// Finish updates. May add padding.
  88. ///
  89. /// - Parameter output: Processed data
  90. /// - Throws: Error
  91. @inlinable
  92. public mutating func finish(output: (Array<UInt8>) -> Void) throws {
  93. try self.finish(withBytes: [], output: output)
  94. }
  95. }