Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SHA1 to SHA256 #819

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Nuke/Caching/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Nuke/Internal/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be /// print("http://test.com".sha256)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof good catch. let me revise

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duguyihou fixed, thank you

/// // prints "8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165"
/// ```
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))
Expand Down
4 changes: 2 additions & 2 deletions Tests/NukeTests/DataCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down