diff --git a/Sources/Photochk/Core/ContentHasher.swift b/Sources/Photochk/Core/ContentHasher.swift new file mode 100644 index 0000000..78c1ebc --- /dev/null +++ b/Sources/Photochk/Core/ContentHasher.swift @@ -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() + } +}