Skip to content

Commit

Permalink
Upgrade to CryptoKit
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 15, 2023
1 parent a299779 commit f8665ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Nuke 12

## Nuke 12.1.4

*Upcoming*

- Upgrade to [`CryptoKit`](https://developer.apple.com/documentation/cryptokit) from `CommonCrypto` and optimize how cryptographic hashes are converted to strings (used as filenames in `DataCache`)

## Nuke 12.1.3

*Jul 10, 2023*
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Caching/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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.sha1
key.isEmpty ? nil : key.sha1
}

private func didInit() throws {
Expand Down
15 changes: 7 additions & 8 deletions Sources/Nuke/Internal/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2015-2023 Alexander Grebenyuk (github.com/kean).

import Foundation
import CryptoKit
import CommonCrypto

extension String {
Expand All @@ -13,17 +14,15 @@ extension String {
/// // prints "50334ee0b51600df6397ce93ceed4728c37fee4e"
/// ```
var sha1: String? {
guard !isEmpty, let input = self.data(using: .utf8) else {
guard let input = self.data(using: .utf8) else {
return nil
}

let hash = input.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
var hash = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CC_SHA1(bytes.baseAddress, CC_LONG(input.count), &hash)
return hash
let digest = Insecure.SHA1.hash(data: input)
var output = ""
for byte in digest {
output.append(String(format: "%02x", byte))
}

return hash.map({ String(format: "%02x", $0) }).joined()
return output
}
}

Expand Down

0 comments on commit f8665ab

Please sign in to comment.