Updatable.swift 4.0 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 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. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  35. let processed = try update(withBytes: bytes, isLast: isLast)
  36. if !processed.isEmpty {
  37. output(processed)
  38. }
  39. }
  40. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  41. try self.update(withBytes: bytes, isLast: isLast)
  42. }
  43. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  44. try self.update(withBytes: bytes.slice, isLast: isLast)
  45. }
  46. public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
  47. try self.update(withBytes: bytes.slice, isLast: isLast, output: output)
  48. }
  49. /// Finish updates. This may apply padding.
  50. /// - parameter bytes: Bytes to process
  51. /// - returns: Processed data.
  52. public mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
  53. try self.update(withBytes: bytes, isLast: true)
  54. }
  55. public mutating func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
  56. try self.finish(withBytes: bytes.slice)
  57. }
  58. /// Finish updates. May add padding.
  59. ///
  60. /// - Returns: Processed data
  61. /// - Throws: Error
  62. public mutating func finish() throws -> Array<UInt8> {
  63. try self.update(withBytes: [], isLast: true)
  64. }
  65. /// Finish updates. This may apply padding.
  66. /// - parameter bytes: Bytes to process
  67. /// - parameter output: Resulting data
  68. /// - returns: Processed data.
  69. public mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  70. let processed = try update(withBytes: bytes, isLast: true)
  71. if !processed.isEmpty {
  72. output(processed)
  73. }
  74. }
  75. public mutating func finish(withBytes bytes: Array<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
  76. try self.finish(withBytes: bytes.slice, output: output)
  77. }
  78. /// Finish updates. May add padding.
  79. ///
  80. /// - Parameter output: Processed data
  81. /// - Throws: Error
  82. public mutating func finish(output: (Array<UInt8>) -> Void) throws {
  83. try self.finish(withBytes: [], output: output)
  84. }
  85. }