I have got the exact same problem as in this thread:
MD5 of an UTF16LE (without BOM and 0-Byte End) in C#
However, I am trying to achieve this in a Swift 4 based iOS app. I tried all options discussed in
How to convert string to MD5 hash using ios swift
and also https://github.com/krzyzanowskim/CryptoSwift
but I am not able to generate the correct MD5 hash. I generated a byte array of the input string and removed the 0-byte at the end of the string and used the functions mentioned in the thread above. If someone could please point me in the right direction. Basically the utf8 string "1234567z-äbc" should become "9e224a41eeefa284df7bb0f26c2913e2"
That's what I tried so far:
let str = "1234567z" + "-" + "äbc"
let data = str.data(using: .utf16LittleEndian)!
let bytesArray = data.map { $0 }
let bytesArrayNoZero = bytesArray.filter{ $0 != 0}
let str2 = String(bytes: bytesArrayNoZero, encoding: String.Encoding.utf16LittleEndian)
print (fritz_01.MD5(str2!))
func MD5(string: String) -> Data {
let messageData = string.data(using:.utf8)!
var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes {digestBytes in
messageData.withUnsafeBytes {messageBytes in
CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes)
}
}
return digestData
}