I want to calculate hash of an image, first I convert image to data and then with help of this function I will calculate hash of image file (Data), but the generated hash doesn't match with online generator and other language convertors like (Java), even I tried other libraries but i get same result, I think while Im converting to Data something happend to my file so hash doesn't match with other convertors.
but when i calculate a plain text hash, it matches with all online convertor and other languages convertor but its not the same with Image?
generated hash in terminal is different
thanks for any help
func md5(url: URL) {
let bufferSize = 1024*1024
do {
let file = try FileHandle.init(forReadingFrom: url)
defer {
file.closeFile()
}
var context = CC_MD5_CTX.init()
CC_MD5_Init(&context)
while case let data = file.readData(ofLength: bufferSize), data.count > 0 {
data.withUnsafeBytes { (poiner) -> Void in
_ = CC_MD5_Update(&context, poiner, CC_LONG(data.count))
}
}
// Calculate the MD5 summary
var digest = Data(count: Int(CC_MD5_DIGEST_LENGTH))
digest.withUnsafeMutableBytes { (pointer) -> Void in
_ = CC_MD5_Final(pointer, &context)
}
let result = digest.map { (byte) -> String in
String.init(format: "%02hhx", byte)
}.joined()
print("result: \(result)")
} catch let error as Error {
print("calculation error: \(error.localizedDescription)") // Where is the try, where is the error?
}
}