Skip to content

Commit ad776c6

Browse files
committed
Replace safeFilename with sha1
1 parent 39b4200 commit ad776c6

File tree

4 files changed

+27
-53
lines changed

4 files changed

+27
-53
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
import CryptoKit
3+
4+
extension String {
5+
/// Calculates SHA1 from the given string and returns its hex representation.
6+
///
7+
/// ```swift
8+
/// print("http://test.com".sha1)
9+
/// // prints "50334ee0b51600df6397ce93ceed4728c37fee4e"
10+
/// ```
11+
var sha1: String {
12+
guard let input = self.data(using: .utf8) else {
13+
assertionFailure("Failed to generate data for the string")
14+
return "" // The conversion to .utf8 should never fail
15+
}
16+
let digest = Insecure.SHA1.hash(data: input)
17+
var output = ""
18+
for byte in digest {
19+
output.append(String(format: "%02x", byte))
20+
}
21+
return output
22+
}
23+
}

ios/Sources/GutenbergKit/Sources/Helpers/String+SafeDirectoryName.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

ios/Sources/GutenbergKit/Sources/Service/EditorService.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ actor EditorService {
4444

4545
self.storeURL = URL.applicationDirectory
4646
.appendingPathComponent("GutenbergKit", isDirectory: true)
47-
.appendingPathComponent(siteURL.safeFilename, isDirectory: true)
47+
.appendingPathComponent(siteURL.sha1, isDirectory: true)
4848
}
4949

5050
/// Set up the editor for the given site.
@@ -356,10 +356,7 @@ actor EditorService {
356356

357357
/// Generates a cached filename from an asset URL using SHA256 hash
358358
private func cachedFilename(for urlString: String) -> String {
359-
let hash = SHA256.hash(data: Data(urlString.utf8))
360-
.compactMap { String(format: "%02x", $0) }
361-
.joined()
362-
359+
let hash = urlString.sha1
363360
// Preserve file extension if present
364361
if let url = URL(string: urlString) {
365362
let ext = url.pathExtension

ios/Sources/GutenbergKit/Sources/Views/HTMLPreview/HTMLPreviewManager.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class HTMLPreviewManager: ObservableObject {
6161
// for caching purposes. This way, if you make any changes to the template,
6262
// it will automatically invaliate the previous caches.
6363
let template = makePatternHTML(content: "", viewportWidth: 0, editorStyles: gutenbergCSS, themeStyles: themeStyles)
64-
self.templateHash = template.sha256
64+
self.templateHash = template.sha1
6565

6666
self.urlCache = HTMLPreviewManager.makeCache()
6767
}
@@ -247,7 +247,7 @@ public final class HTMLPreviewManager: ObservableObject {
247247
// MARK: - Private Helper Functions
248248

249249
private func makeDiskCacheKey(content: String, viewportWidth: Int, templateHash: String) -> String {
250-
"\(content)-\(viewportWidth)-\(templateHash)".sha256
250+
"\(content)-\(viewportWidth)-\(templateHash)".sha1
251251
}
252252

253253
/// Creates the HTML for rendering the pattern preview.
@@ -324,11 +324,3 @@ private func encode(_ image: UIImage) -> Data? {
324324
return data as Data
325325
}
326326

327-
private extension String {
328-
/// Creates a SHA256 hash of the string
329-
var sha256: String {
330-
let data = Data(self.utf8)
331-
let hash = SHA256.hash(data: data)
332-
return hash.compactMap { String(format: "%02x", $0) }.joined()
333-
}
334-
}

0 commit comments

Comments
 (0)