From 2825a534157000d6546feb59a1dc2a3486b51d38 Mon Sep 17 00:00:00 2001 From: yoshikazu Date: Mon, 27 Jul 2026 15:38:06 +0000 Subject: [PATCH] Add ContentHasher --- Sources/Photochk/Core/ContentHasher.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Sources/Photochk/Core/ContentHasher.swift 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() + } +}