EncodeUtility.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. namespace EZXR.Glass.Network.WebRequest
  8. {
  9. public static class EncodeUtility
  10. {
  11. /// <summary>
  12. /// 返回md5字符串
  13. /// </summary>
  14. /// <param name="str"></param>
  15. /// <returns></returns>
  16. public static string MD5(string strToEncrypt)
  17. {
  18. UTF8Encoding ue = new UTF8Encoding();
  19. byte[] bytes = ue.GetBytes(strToEncrypt);
  20. // encrypt bytes
  21. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  22. byte[] hashBytes = md5.ComputeHash(bytes);
  23. // Convert the encrypted bytes back to a string (base 16)
  24. string hashString = "";
  25. for (int i = 0; i < hashBytes.Length; i++)
  26. {
  27. hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  28. }
  29. return hashString.PadLeft(32, '0');
  30. }
  31. /// <summary>
  32. /// sha256
  33. /// </summary>
  34. /// <param name="pass"></param>
  35. /// <returns></returns>
  36. public static string Sha256(string pass)
  37. {
  38. if (pass == null || pass == string.Empty) { return null; }
  39. byte[] buffer = Encoding.UTF8.GetBytes(pass);
  40. byte[] hash = SHA256Managed.Create().ComputeHash(buffer);
  41. StringBuilder builder = new StringBuilder();
  42. for (int i = 0; i < hash.Length; i++)
  43. {
  44. builder.Append(hash[i].ToString("X2"));
  45. }
  46. return builder.ToString();
  47. }
  48. }
  49. }