Pārlūkot izejas kodu

Improve hash calculation for large datasets by reducing number of calls of "array.removeFirst()". Update digests tests

Marcin Krzyżanowski 8 gadi atpakaļ
vecāks
revīzija
3e6c13073f

+ 8 - 6
Sources/CryptoSwift/MD5.swift

@@ -12,7 +12,7 @@ public final class MD5: DigestType  {
     fileprivate static let hashInitialValue:Array<UInt32> = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
 
     fileprivate var accumulated = Array<UInt8>()
-    fileprivate var accumulatedLength: Int = 0
+    fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var accumulatedHash:Array<UInt32> = MD5.hashInitialValue
 
     /** specifies the per-round shift amounts */
@@ -107,26 +107,28 @@ public final class MD5: DigestType  {
 
 extension MD5: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
-        let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength += self.accumulated.count - prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
+            let lengthInBits = (self.processedBytesTotalCount + self.accumulated.count) * 8
+            let lengthBytes = lengthInBits.bytes(totalBytes: 64 / 8) // A 64-bit representation of b
+
             // Step 1. Append padding
             bitPadding(to: &self.accumulated, blockSize: MD5.blockSize, allowance: 64 / 8)
 
             // Step 2. Append Length a 64-bit representation of lengthInBits
-            let lengthInBits = self.accumulatedLength * 8
-            let lengthBytes = lengthInBits.bytes(totalBytes: 64 / 8) // A 64-bit representation of b
             self.accumulated += lengthBytes.reversed()
         }
 
+        var processedBytes = 0
         for chunk in BytesSequence(chunkSize: MD5.blockSize, data: self.accumulated) {
             if (isLast || self.accumulated.count >= MD5.blockSize) {
                 self.process(block: chunk, currentHash: &self.accumulatedHash)
-                self.accumulated.removeFirst(chunk.count)
+                processedBytes += chunk.count
             }
         }
+        self.accumulated.removeFirst(processedBytes)
+        self.processedBytesTotalCount += processedBytes
 
         // output current hash
         var result = Array<UInt8>()

+ 9 - 7
Sources/CryptoSwift/SHA1.swift

@@ -12,7 +12,7 @@ public final class SHA1: DigestType {
     fileprivate static let hashInitialValue: ContiguousArray<UInt32> = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
 
     fileprivate var accumulated = Array<UInt8>()
-    fileprivate var accumulatedLength: Int = 0
+    fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var accumulatedHash: ContiguousArray<UInt32> = SHA1.hashInitialValue
 
     public init() {
@@ -93,26 +93,28 @@ public final class SHA1: DigestType {
 
 extension SHA1: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
-        let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
+            let lengthInBits = (self.processedBytesTotalCount + self.accumulated.count) * 8
+            let lengthBytes = lengthInBits.bytes(totalBytes: 64 / 8) // A 64-bit representation of b
+
             // Step 1. Append padding
             bitPadding(to: &self.accumulated, blockSize: SHA1.blockSize, allowance: 64 / 8)
 
             // Step 2. Append Length a 64-bit representation of lengthInBits
-            let lengthInBits = self.accumulatedLength * 8
-            let lengthBytes = lengthInBits.bytes(totalBytes: 64 / 8) // A 64-bit representation of b
             self.accumulated += lengthBytes
         }
 
+        var processedBytes = 0
         for chunk in BytesSequence(chunkSize: SHA1.blockSize, data: self.accumulated) {
-            if (isLast || self.accumulated.count >= SHA1.blockSize) {
+            if (isLast || (self.accumulated.count - processedBytes) >= SHA1.blockSize) {
                 self.process(block: chunk, currentHash: &self.accumulatedHash)
-                self.accumulated.removeFirst(chunk.count)
+                processedBytes += chunk.count
             }
         }
+        self.accumulated.removeFirst(processedBytes)
+        self.processedBytesTotalCount += processedBytes
 
         // output current hash
         var result = Array<UInt8>(repeating: 0, count: SHA1.digestLength)

+ 9 - 6
Sources/CryptoSwift/SHA2.swift

@@ -16,7 +16,7 @@ public final class SHA2: DigestType {
     private let k:Array<UInt64>
 
     fileprivate var accumulated = Array<UInt8>()
-    fileprivate var accumulatedLength: Int = 0
+    fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var accumulatedHash32 = Array<UInt32>()
     fileprivate var accumulatedHash64 = Array<UInt64>()
 
@@ -259,19 +259,20 @@ public final class SHA2: DigestType {
 extension SHA2: Updatable {
 
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
-        let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
+            let lengthInBits = (self.processedBytesTotalCount + self.accumulated.count) * 8
+            let lengthBytes = lengthInBits.bytes(totalBytes: self.blockSize / 8) // A 64-bit/128-bit representation of b. blockSize fit by accident.
+
             // Step 1. Append padding
             bitPadding(to: &self.accumulated, blockSize: self.blockSize, allowance: self.blockSize / 8)
 
             // Step 2. Append Length a 64-bit representation of lengthInBits
-            let lengthInBits = self.accumulatedLength * 8
-            self.accumulated += lengthInBits.bytes(totalBytes: self.blockSize / 8) // A 64-bit/128-bit representation of b. blockSize fit by accident.
+            self.accumulated += lengthBytes
         }
 
+        var processedBytes = 0
         for chunk in BytesSequence(chunkSize: self.blockSize, data: self.accumulated) {
             if (isLast || self.accumulated.count >= self.blockSize) {
                 switch self.variant {
@@ -280,9 +281,11 @@ extension SHA2: Updatable {
                     case .sha384, .sha512:
                         self.process64(block: chunk, currentHash: &self.accumulatedHash64)
                 }
-                self.accumulated.removeFirst(chunk.count)
+                processedBytes += chunk.count
             }
         }
+        self.accumulated.removeFirst(processedBytes)
+        self.processedBytesTotalCount += processedBytes
 
         // output current hash
         var result = Array<UInt8>(repeating: 0, count: self.variant.digestLength)

+ 7 - 5
Sources/CryptoSwift/SHA3.swift

@@ -26,7 +26,7 @@ public final class SHA3: DigestType {
     let variant: Variant
 
     fileprivate var accumulated = Array<UInt8>()
-    fileprivate var accumulatedLength: Int = 0
+    fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var accumulatedHash:Array<UInt64>
 
     public enum Variant: RawRepresentable {
@@ -239,28 +239,30 @@ public final class SHA3: DigestType {
 
 extension SHA3: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
-        let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
             // Add padding
+            let markByteIndex = self.processedBytesTotalCount + self.accumulated.count
             if self.accumulated.count == 0 || self.accumulated.count % self.variant.blockSize != 0 {
                 let r = self.variant.blockSize * 8
                 let q = (r/8) - (self.accumulated.count % (r/8))
                 self.accumulated += Array<UInt8>(repeating: 0, count: q)
             }
 
-            self.accumulated[self.accumulatedLength] |= 0x06 // 0x1F for SHAKE
+            self.accumulated[markByteIndex] |= 0x06 // 0x1F for SHAKE
             self.accumulated[self.accumulated.count - 1] |= 0x80
         }
 
+        var processedBytes = 0
         for chunk in BytesSequence(chunkSize: self.variant.blockSize, data: self.accumulated) {
             if (isLast || self.accumulated.count >= self.variant.blockSize) {
                 self.process(block: chunk.toUInt64Array(), currentHash: &self.accumulatedHash)
-                self.accumulated.removeFirst(chunk.count)
+                processedBytes += chunk.count
             }
         }
+        self.accumulated.removeFirst(processedBytes)
+        self.processedBytesTotalCount += processedBytes
 
         //TODO: verify performance, reduce vs for..in
         let result = self.accumulatedHash.reduce(Array<UInt8>()) { (result, value) -> Array<UInt8> in

+ 79 - 57
Tests/CryptoSwiftTests/DigestTests.swift

@@ -13,13 +13,8 @@ import Foundation
 @testable import CryptoSwift
 
 final class DigestTests: XCTestCase {
-    
-    func testMD5Data() {
-        let data = [0x31, 0x32, 0x33] as Array<UInt8> // "1", "2", "3"
-        XCTAssertEqual(Digest.md5(data), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
-    }
 
-    func testMD5String() {
+    func testMD5() {
         XCTAssertEqual("123".md5(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
         XCTAssertEqual("".md5(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
         XCTAssertEqual("a".md5(), "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
@@ -30,24 +25,12 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual("12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5(), "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
     }
 
-    func testMD5Updates() {
-        do {
-            var hash = MD5()
-            let _ = try hash.update(withBytes: [0x31, 0x32])
-            let _ = try hash.update(withBytes: [0x33])
-            let result = try hash.finish()
-            XCTAssertEqual(result, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70])
-        } catch {
-            XCTFail()
-        }
-    }
-
     func testSHA1() {
-        XCTAssertEqual([0x31, 0x32, 0x33].sha1().toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
-
-        XCTAssertEqual("abc".sha1(), "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
-        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
-        XCTAssertEqual("".sha1(), "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
+        XCTAssertEqual(SHA1().calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "a9993e364706816aba3e25717850c26c9cd0d89d")
+        XCTAssertEqual(SHA1().calculate(for: Array<UInt8>(hex: "")).toHexString(), "da39a3ee5e6b4b0d3255bfef95601890afd80709")
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
+        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha1(), "a49b2446a02c645bf419f995b67091253a04a259")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha1(), Array<UInt8>(hex: "34aa973cd4c4daa4f61eeb2bdbad27316534016f"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
 
         // https://github.com/krzyzanowskim/CryptoSwift/issues/363
         XCTAssertEqual("1477304791&1XpnGSRhOlZz2etXMeOdfNaHILTjW16U&8mpBBbzwsgs".sha1(), "0809bbf8489c594131c2030a84be364a0851a635", "Failed")
@@ -55,35 +38,35 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual("1477304791&1XpnGSRhOlZz2etXMeOdfNaHILTjW16U&8mpBBbzwsg".sha1(), "c106aa0a98606294ee35fd2d937e928ebb5339e0", "Failed")
     }
 
-    func testSHA224() {
-        let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
-        XCTAssertEqual(data.sha224().toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
-    }
 
-    func testSHA256() {
-        let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
-        XCTAssertEqual(data.sha256().toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
+    func testSHA2() {
+        XCTAssertEqual(SHA2(variant: .sha224).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
+        XCTAssertEqual(SHA2(variant: .sha256).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
+        XCTAssertEqual(SHA2(variant: .sha384).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7")
+        XCTAssertEqual(SHA2(variant: .sha512).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
 
-        XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
-        XCTAssertEqual("".sha256(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
-    }
+        XCTAssertEqual(SHA2(variant: .sha224).calculate(for: Array<UInt8>(hex: "")).toHexString(), "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
+        XCTAssertEqual(SHA2(variant: .sha256).calculate(for: Array<UInt8>(hex: "")).toHexString(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
+        XCTAssertEqual(SHA2(variant: .sha384).calculate(for: Array<UInt8>(hex: "")).toHexString(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b")
+        XCTAssertEqual(SHA2(variant: .sha512).calculate(for: Array<UInt8>(hex: "")).toHexString(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
 
-    func testSHA384() {
-        let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
-        XCTAssertEqual(data.sha384().toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha224(), "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha256(), "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha384(), "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b")
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha512(), "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445")
 
-        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
-        XCTAssertEqual("".sha384(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
-        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha384(), "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", "SHA384 calculation failed")
-    }
+        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha224(), "c97ca9a559850ce97a04a96def6d99a9e0e0e2ab14e6b8df265fc0b3")
+        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha256(), "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1")
+        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha384(), "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039")
+        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha512(), "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
 
-    func testSHA512() {
-        XCTAssertEqual("abc".sha512(), "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", "length 24 bits");
-        XCTAssertEqual("".sha512(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "the bit string of length 0");
-        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha512(), "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445", "length 448 bits");
-        XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha512(), "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", "length 896 bits");
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha224(), Array<UInt8>(hex: "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha256(), Array<UInt8>(hex: "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha384(), Array<UInt8>(hex: "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha512(), Array<UInt8>(hex: "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
 
-        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha512(), Array<UInt8>(hex: "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
+        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
     }
 
     func testSHA3() {
@@ -107,10 +90,50 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha3(.sha384), "79407d3b5916b59c3e30b09822974791c313fb9ecc849e406f23592d04f625dc8c709b98b43b3852b337216179aa7fc7")
         XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha3(.sha512), "afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185")
 
-        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha3(.sha224), Array<UInt8>(hex: "d69335b93325192e516a912e6d19a15cb51c6ed5c15243e7a7fd653c"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
-        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha3(.sha256), Array<UInt8>(hex: "5c8875ae474a3634ba4fd55ec85bffd661f32aca75c6d699d0cdcb6c115891c1"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
-        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha3(.sha384), Array<UInt8>(hex: "eee9e24d78c1855337983451df97c8ad9eedf256c6334f8e948d252d5e0e76847aa0774ddb90a842190d2c558b4b8340"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
-        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha3(.sha512), Array<UInt8>(hex: "3c3a876da14034ab60627c077bb98f7e120a2a5370212dffb3385a18d4f38859ed311d0a9d5141ce9cc5c66ee689b266a8aa18ace8282a0e0db596c90b0a7b87"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha224), Array<UInt8>(hex: "d69335b93325192e516a912e6d19a15cb51c6ed5c15243e7a7fd653c"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha256), Array<UInt8>(hex: "5c8875ae474a3634ba4fd55ec85bffd661f32aca75c6d699d0cdcb6c115891c1"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha384), Array<UInt8>(hex: "eee9e24d78c1855337983451df97c8ad9eedf256c6334f8e948d252d5e0e76847aa0774ddb90a842190d2c558b4b8340"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+        XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha512), Array<UInt8>(hex: "3c3a876da14034ab60627c077bb98f7e120a2a5370212dffb3385a18d4f38859ed311d0a9d5141ce9cc5c66ee689b266a8aa18ace8282a0e0db596c90b0a7b87"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
+    }
+    
+    func testMD5Data() {
+        let data = [0x31, 0x32, 0x33] as Array<UInt8> // "1", "2", "3"
+        XCTAssertEqual(Digest.md5(data), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
+    }
+
+    func testMD5Updates() {
+        do {
+            var hash = MD5()
+            let _ = try hash.update(withBytes: [0x31, 0x32])
+            let _ = try hash.update(withBytes: [0x33])
+            let result = try hash.finish()
+            XCTAssertEqual(result, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70])
+        } catch {
+            XCTFail()
+        }
+    }
+
+    func testSHA1Updatable1() {
+        do {
+            var hash = SHA1()
+            let _ = try hash.update(withBytes: [0x31, 0x32])
+            let _ = try hash.update(withBytes: [0x33])
+            XCTAssertEqual(try hash.finish().toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "Failed")
+        } catch {
+            XCTFail()
+        }
+    }
+
+    func testSHA1Updatable2() {
+        do {
+            var hash = SHA1()
+            let _ = try hash.update(withBytes: [0x31, 0x32])
+            let _ = try hash.update(withBytes: [0x33])
+            let _ = try hash.update(withBytes: Array<UInt8>.init(repeating: 0x33, count: 64))
+            XCTAssertEqual(try hash.finish().toHexString(), "0e659367eff83a6b868a35b96ac305b270025e86", "Failed")
+        } catch {
+            XCTFail()
+        }
     }
 
     func testCRC32() {
@@ -163,15 +186,14 @@ extension DigestTests {
 extension DigestTests {
     static func allTests() -> [(String, (DigestTests) -> () -> Void)] {
         var tests = [
-            ("testMD5Data", testMD5Data),
-            ("testMD5String", testMD5String),
-            ("testMD5Updates", testMD5Updates),
+            ("testMD5", testMD5),
             ("testSHA1", testSHA1),
-            ("testSHA224", testSHA224),
-            ("testSHA256", testSHA256),
-            ("testSHA384", testSHA384),
-            ("testSHA512", testSHA512),
+            ("testSHA2", testSHA2),
             ("testSHA3", testSHA3),
+            ("testMD5Data", testMD5Data),
+            ("testMD5Updates", testMD5Updates),
+            ("testSHA1Updatable1", testSHA1Updatable1),
+            ("testSHA1Updatable2", testSHA1Updatable2),
             ("testCRC32", testCRC32),
             ("testCRC32NotReflected", testCRC32NotReflected),
             ("testCRC15", testCRC16),