Functions.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq.Expressions;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. namespace Update.Utils
  10. {
  11. public static class Functions
  12. {
  13. /// <summary>
  14. /// 字符串生成MD5
  15. /// </summary>
  16. /// <param name="s"></param>
  17. /// <returns></returns>
  18. public static string GenMd5(string s)
  19. {
  20. //就是比string往后一直加要好的优化容器
  21. StringBuilder sb = new StringBuilder();
  22. using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
  23. {
  24. //将输入字符串转换为字节数组并计算哈希。
  25. byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
  26. //X为 十六进制 X都是大写 x都为小写
  27. //2为 每次都是两位数
  28. //假设有两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,可以指定"X2",这样显示出来就是:0x0A、0x1A。
  29. //遍历哈希数据的每个字节
  30. //并将每个字符串格式化为十六进制字符串。
  31. int length = data.Length;
  32. for (int i = 0; i < length; i++)
  33. {
  34. sb.Append(data[i].ToString("X2"));
  35. }
  36. }
  37. return sb.ToString();
  38. }
  39. }
  40. }