In Swift 4 we could use
var md5: String? {
guard let data = self.data(using: .utf8) else { return nil }
let hash = data.withUnsafeBytes { (bytes: UnsafePointer<Data>) -> [UInt8] in
var hash: [UInt8] = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
CC_MD5(bytes, CC_LONG(data.count), &hash)
return hash
}
return hash.map { String(format: "%02x", $0) }.joined()
}
But in Swift 5 withUnsafeBytes
uses UnsafeRawBufferPointer
instead of UnsafePointer
. How to change md5 function?
UnsafePointer<Data>
in your Swift 4 code makes no sense, it should beUnsafePointer<UInt8>
– it works only because the closure does not depend on the actual pointer type. – Oxen