I'm downloading file (~ 300MB) using Alamofire. After download and save, I have to calculate and compare SHA1 from this file, so I do this:
let data = Data(contentsOf: url)
let sha1 = data.sha1
That generate a problem. All bytes are coped to ram memory. How can I avoid this?
function to calculate:
extension Data {
func sha1() -> String {
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
self.withUnsafeBytes {
_ = CC_SHA1($0, CC_LONG(self.count), &digest)
}
let hexBytes = digest.map { String(format: "%02hhx", $0) }
return hexBytes.joined()
}
}