MD5 of an UTF16LE (without BOM and 0-Byte End) in C#

谁都会走 提交于 2019-12-04 17:55:40

You can use the UnicodeEncoding Class and its GetBytes Method to convert a string to bytes in UTF16-LE format:

var bytes = Encoding.Unicode.GetBytes("1234567z-äbc");

Then you can use the MD5Cng Class and its ComputeHash Method to calculate the MD5 hash for the bytes:

var hash = new MD5Cng().ComputeHash(bytes);

Finally, you can convert the hash to a string as follows:

var result = hash.Aggregate(
    new StringBuilder(), (sb, x) => sb.Append(x.ToString("x2"))).ToString();

// result == "9e224a41eeefa284df7bb0f26c2913e2"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!