Add ContentHasher

This commit is contained in:
2026-07-27 15:38:06 +00:00
parent 09a38929d2
commit 2825a53415
+18
View File
@@ -0,0 +1,18 @@
import CryptoKit
import Foundation
enum ContentHasher {
private static let chunkSize = 1 << 20 // 1 MB
/// ファイル全体を読み込まずに SHA-256 を求める。
static func sha256(of url: URL) throws -> String {
let handle = try FileHandle(forReadingFrom: url)
defer { try? handle.close() }
var hasher = SHA256()
while let chunk = try handle.read(upToCount: chunkSize), !chunk.isEmpty {
hasher.update(data: chunk)
}
return hasher.finalize().map { String(format: "%02x", $0) }.joined()
}
}