19 lines
579 B
Swift
19 lines
579 B
Swift
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()
|
|
}
|
|
}
|