I have a number of files that will live on a server. Users have the ability to create these kinds of files (plists) on-device which will then upload to said server (CloudKit). I would like to unique them by content (the uniquing methodology should be resilient to variations in creation date). My understanding is that I should hash these files in order to obtain unique file names for them. My questions are:
Thanks so much!
An update using Apple's CryptoKit:
You could use a FileHandle
to read the data in chunks, and pass these into the hasher:
import CryptoKit
func getSHA256(forFile url: URL) throws -> SHA256.Digest {
let handle = try FileHandle(forReadingFrom: url)
var hasher = SHA256()
while autoreleasepool(invoking: {
let nextChunk = handle.readData(ofLength: SHA256.blockByteCount)
guard !nextChunk.isEmpty else { return false }
hasher.update(data: nextChunk)
return true
}) { }
let digest = hasher.finalize()
return digest
// Here's how to convert to string form
//return digest.map { String(format: "%02hhx", $0) }.joined()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With