MD5Util.cs 998 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GeekDesk.Util
  7. {
  8. internal class MD5Util
  9. {
  10. public static string CreateMD5(string input)
  11. {
  12. // Use input string to calculate MD5 hash
  13. using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
  14. {
  15. byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  16. byte[] hashBytes = md5.ComputeHash(inputBytes);
  17. //return Convert.ToHexString(hashBytes); // .NET 5 +
  18. //Convert the byte array to hexadecimal string prior to.NET 5
  19. StringBuilder sb = new System.Text.StringBuilder();
  20. for (int i = 0; i < hashBytes.Length; i++)
  21. {
  22. sb.Append(hashBytes[i].ToString("X2"));
  23. }
  24. return sb.ToString();
  25. }
  26. }
  27. }
  28. }