Add ContentHasher
This commit is contained in:
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user