Просмотр исходного кода

Updatable partial result oprations are not discardable

Marcin Krzyzanowski 7 лет назад
Родитель
Сommit
2d1432be7e
2 измененных файлов с 37 добавлено и 38 удалено
  1. 34 35
      Sources/CryptoSwift/Updatable.swift
  2. 3 3
      Tests/Tests/DigestTests.swift

+ 34 - 35
Sources/CryptoSwift/Updatable.swift

@@ -20,7 +20,7 @@ public protocol Updatable {
     ///
     /// - 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.
+    /// - returns: Processed partial result data or empty array.
     mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool) throws -> Array<UInt8>
 
     /// Update given bytes in chunks.
@@ -29,19 +29,8 @@ public protocol Updatable {
     ///   - 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.
+    /// - Returns: Processed partial result data or empty array.
     mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool, output: (_ bytes: Array<UInt8>) -> Void) throws
-
-    /// Finish updates. This may apply padding.
-    /// - parameter bytes: Bytes to process
-    /// - returns: Processed data.
-    mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8>
-
-    /// Finish updates. This may apply padding.
-    /// - parameter bytes: Bytes to process
-    /// - parameter output: Resulting data
-    /// - returns: Processed data.
-    mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws
 }
 
 extension Updatable {
@@ -52,16 +41,38 @@ extension Updatable {
         }
     }
 
-    @discardableResult
+    public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
+        return try update(withBytes: bytes.slice, isLast: isLast)
+    }
+
+    public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
+        return try update(withBytes: bytes.slice, isLast: isLast, output: output)
+    }
+
+    /// Finish updates. This may apply padding.
+    /// - parameter bytes: Bytes to process
+    /// - returns: Processed data.
     public mutating func finish(withBytes bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
         return try update(withBytes: bytes, isLast: true)
     }
 
-    @discardableResult
+    public mutating func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
+        return try finish(withBytes: bytes.slice)
+    }
+
+
+    /// Finish updates. May add padding.
+    ///
+    /// - Returns: Processed data
+    /// - Throws: Error
     public mutating func finish() throws -> Array<UInt8> {
         return try update(withBytes: [], isLast: true)
     }
 
+    /// Finish updates. This may apply padding.
+    /// - parameter bytes: Bytes to process
+    /// - parameter output: Resulting data
+    /// - returns: Processed data.
     public mutating func finish(withBytes bytes: ArraySlice<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
         let processed = try update(withBytes: bytes, isLast: true)
         if !processed.isEmpty {
@@ -69,27 +80,15 @@ extension Updatable {
         }
     }
 
-    public mutating func finish(output: (Array<UInt8>) -> Void) throws {
-        try finish(withBytes: [], output: output)
-    }
-}
-
-extension Updatable {
-    @discardableResult
-    public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
-        return try update(withBytes: bytes.slice, isLast: isLast)
-    }
-
-    public mutating func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
-        return try update(withBytes: bytes.slice, isLast: isLast, output: output)
-    }
-
-    @discardableResult
-    public mutating func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
-        return try finish(withBytes: bytes.slice)
-    }
-
     public mutating func finish(withBytes bytes: Array<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
         return try finish(withBytes: bytes.slice, output: output)
     }
+
+    /// Finish updates. May add padding.
+    ///
+    /// - Parameter output: Processed data
+    /// - Throws: Error
+    public mutating func finish(output: (Array<UInt8>) -> Void) throws {
+        try finish(withBytes: [], output: output)
+    }
 }

+ 3 - 3
Tests/Tests/DigestTests.swift

@@ -232,7 +232,7 @@ final class DigestTests: XCTestCase {
 
             var sha1Partial = SHA1()
             for batch in input.batched(by: 17) {
-                try sha1Partial.update(withBytes: batch.bytes)
+                _ = try sha1Partial.update(withBytes: batch.bytes)
             }
             let sha1Result = try sha1Partial.finish()
             XCTAssertEqual(sha1Once, sha1Result)
@@ -242,7 +242,7 @@ final class DigestTests: XCTestCase {
 
             var sha2Partial = SHA2(variant: .sha224)
             for batch in input.batched(by: 17) {
-                try sha2Partial.update(withBytes: batch.bytes)
+                _ = try sha2Partial.update(withBytes: batch.bytes)
             }
             let sha2Result = try sha2Partial.finish()
             XCTAssertEqual(sha2Once, sha2Result)
@@ -252,7 +252,7 @@ final class DigestTests: XCTestCase {
 
             var sha3Partial = SHA3(variant: .sha224)
             for batch in input.batched(by: 17) {
-                try sha3Partial.update(withBytes: batch.bytes)
+                _ = try sha3Partial.update(withBytes: batch.bytes)
             }
             let sha3Result = try sha3Partial.finish()
             XCTAssertEqual(sha3Once, sha3Result)