image

Are you using MD5 correctly in .net core?

Are you using MD5 correctly in .net core? The project environment of this article is .net 6.0 (.net 5.0 and above are supported) I believe it is very easy to obtain the MD5 of a string in .net, but a casual search on the Internet reveals that there are many versions circulating, such as: StringBuilder version (should be considered the official version, used by the most people, I found that this is also used in ABP) BitConverter version StringConcat version (string concatenation, few people use it, probably everyone knows that the performance is not good) But are they the best implementation? Let’s test it StringBuilder version public static string Md5_StringBuilder(string input) { using var md5 = MD5.Create(); var inputBytes = Encoding.UTF8.GetBytes(input); var hashBytes = md5.ComputeHash(inputBytes); var sb = new StringBuilder(); foreach (var hashByte in hashBytes) { sb.Append(hashByte.ToString(“X2”)); } return sb.ToString(); } BitConverter version public static string Md5_BitConverter(string input) { using var md5 = MD5.Create(); var inputBytes = Encoding.UTF8.GetBytes(input); var hashBytes = md5.ComputeHash(inputBytes); return BitConverter.ToString(hashBytes).Replace(“-“, “”); } StringConcat version public static string Md5_StringConcat(string input) { using var md5 = MD5.Create(); var inputBytes = Encoding.UTF8.GetBytes(input); var hashBytes = md5.ComputeHash(inputBytes); var output = string.Empty; foreach (var hashByte in hashBytes) { output += hashByte.ToString(“X2”);…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索