In my app, I download videos from the Amazon S3 cloud to the sandbox. In order to make sure that the downloaded files are not corrupt, I compare the eTag of the object (delivered by Amazon) with the MD5 hash of the downloaded object which resides in the local file system. For small videos (< 5MB) my algorithm works fine - eTag and MD5 hash are identical.
For bigger files, both parameters no longer match - as far as I know, Amazon generates the eTag differently for files > 5MB - the eTag also has a trailing hyphen with a number behind (maybe it's the number of chunks?):
8c18c4ed68bc9db377cb2d3225c0ee31-4
In the Internet, I could find no solution or code snippet calculating the correct MD5 hash for bigger files.
Calculating the MD5 hash, I tried both
localData.md5().toHexString() // CryptoSwift
both
var md5: String? {
let hash = localData.withUnsafeBytes { (bytes: UnsafePointer<Data>) -> [UInt8] in
var hash: [UInt8] = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
CC_MD5(bytes, CC_LONG(localData.count), &hash)
return hash
}
return hash.map { String(format: "%02x", $0) }.joined()
}
Has anyone an idea how to resolve this? Maybe I should focus on another approach - for example checking if the downloaded video can be opened?