From 4eb9c9e1be87283737510870d4275fd9d80ced9c Mon Sep 17 00:00:00 2001 From: Jacob Myers Date: Thu, 17 Oct 2024 15:32:28 -0400 Subject: [PATCH 1/3] Update SHA1 to SHA256 Updating the Data Cache to utilize a more up-to-date with security standards hashing algorithm. Tests have also been updated to utilize and confirm the functionality. All Tests passing --- Sources/Nuke/Caching/DataCache.swift | 2 +- Sources/Nuke/Internal/Extensions.swift | 4 ++-- Tests/NukeTests/DataCacheTests.swift | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/Nuke/Caching/DataCache.swift b/Sources/Nuke/Caching/DataCache.swift index 8a63cf1c3..8b3fd334f 100644 --- a/Sources/Nuke/Caching/DataCache.swift +++ b/Sources/Nuke/Caching/DataCache.swift @@ -106,7 +106,7 @@ public final class DataCache: DataCaching, @unchecked Sendable { /// A `FilenameGenerator` implementation which uses SHA1 hash function to /// generate a filename from the given key. public static func filename(for key: String) -> String? { - key.isEmpty ? nil : key.sha1 + key.isEmpty ? nil : key.sha256 } private func didInit() throws { diff --git a/Sources/Nuke/Internal/Extensions.swift b/Sources/Nuke/Internal/Extensions.swift index 03b46dc5f..6d399c8d4 100644 --- a/Sources/Nuke/Internal/Extensions.swift +++ b/Sources/Nuke/Internal/Extensions.swift @@ -12,11 +12,11 @@ extension String { /// print("http://test.com".sha1) /// // prints "50334ee0b51600df6397ce93ceed4728c37fee4e" /// ``` - var sha1: String? { + var sha256: String? { guard let input = self.data(using: .utf8) else { return nil // The conversion to .utf8 should never fail } - let digest = Insecure.SHA1.hash(data: input) + let digest = CryptoKit.SHA256.hash(data: input) var output = "" for byte in digest { output.append(String(format: "%02x", byte)) diff --git a/Tests/NukeTests/DataCacheTests.swift b/Tests/NukeTests/DataCacheTests.swift index b5fb25758..468e1af02 100644 --- a/Tests/NukeTests/DataCacheTests.swift +++ b/Tests/NukeTests/DataCacheTests.swift @@ -61,11 +61,11 @@ class DataCacheTests: XCTestCase { func testDefaultKeyEncoder() { let cache = try! DataCache(name: UUID().uuidString) let filename = cache.filename(for: "http://test.com") - XCTAssertEqual(filename, "50334ee0b51600df6397ce93ceed4728c37fee4e") + XCTAssertEqual(filename, "8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165") } func testSHA1() { - XCTAssertEqual("http://test.com".sha1, "50334ee0b51600df6397ce93ceed4728c37fee4e") + XCTAssertEqual("http://test.com".sha256, "8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165") } // MARK: Add From f888feb608bde32ebd4614ecfd13a53e6052e03b Mon Sep 17 00:00:00 2001 From: Jacob Myers Date: Thu, 17 Oct 2024 15:41:58 -0400 Subject: [PATCH 2/3] updating comment above that describes the new hash fxn --- Sources/Nuke/Internal/Extensions.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Nuke/Internal/Extensions.swift b/Sources/Nuke/Internal/Extensions.swift index 6d399c8d4..a7de70722 100644 --- a/Sources/Nuke/Internal/Extensions.swift +++ b/Sources/Nuke/Internal/Extensions.swift @@ -6,11 +6,11 @@ import Foundation import CryptoKit extension String { - /// Calculates SHA1 from the given string and returns its hex representation. + /// Calculates SHA256 from the given string and returns its hex representation. /// /// ```swift - /// print("http://test.com".sha1) - /// // prints "50334ee0b51600df6397ce93ceed4728c37fee4e" + /// print("http://test.com".256) + /// // prints "8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165" /// ``` var sha256: String? { guard let input = self.data(using: .utf8) else { From fe620ef9694050c7089954654bf8d8aced22ac88 Mon Sep 17 00:00:00 2001 From: Jacob Myers Date: Tue, 29 Oct 2024 16:07:11 -0400 Subject: [PATCH 3/3] fixing comment about .sha256 function should result in --- Sources/Nuke/Internal/Extensions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Nuke/Internal/Extensions.swift b/Sources/Nuke/Internal/Extensions.swift index a7de70722..275faa60d 100644 --- a/Sources/Nuke/Internal/Extensions.swift +++ b/Sources/Nuke/Internal/Extensions.swift @@ -9,7 +9,7 @@ extension String { /// Calculates SHA256 from the given string and returns its hex representation. /// /// ```swift - /// print("http://test.com".256) + /// print("http://test.com".sha256) /// // prints "8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165" /// ``` var sha256: String? {