Encrypt.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ropin.Inspection.Common
  8. {
  9. public class EncryptUtil
  10. {
  11. /// <summary>
  12. /// 通过创建哈希字符串适用于任何 MD5 哈希函数 (在任何平台) 上创建 32 个字符的十六进制格式哈希字符串
  13. /// </summary>
  14. /// <param name="source"></param>
  15. /// <returns></returns>
  16. public static string Encrypt(string source)
  17. {
  18. using MD5 md5Hash = MD5.Create();
  19. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source));
  20. StringBuilder sBuilder = new StringBuilder();
  21. foreach (byte t in data)
  22. {
  23. sBuilder.Append(t.ToString("x2"));
  24. }
  25. string hash = sBuilder.ToString();
  26. return hash.ToUpper();
  27. }
  28. /// <summary>
  29. /// 验证source加密码后是否生成mdPwd
  30. /// </summary>
  31. /// <param name="mdPwd">Md5生成的代码</param>
  32. /// <param name="source"></param>
  33. /// <returns></returns>
  34. public static bool Verify(string mdPwd, string source)
  35. {
  36. return mdPwd == Encrypt(source).ToUpper();
  37. }
  38. }
  39. }