EncryptProvider.cs 1008 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace GxPress.Common.Encrypt
  5. {
  6. /// <summary>
  7. /// 加密Provider
  8. /// </summary>
  9. public static class EncryptProvider
  10. {
  11. /// <summary>
  12. /// MD5
  13. /// </summary>
  14. /// <param name="srcStr"></param>
  15. /// <param name="length"></param>
  16. /// <returns></returns>
  17. public static string Md5(string srcStr, Md5Length length = Md5Length.L32)
  18. {
  19. if (string.IsNullOrEmpty(srcStr))
  20. throw new ArgumentException();
  21. using MD5 md5 = MD5.Create();
  22. byte[] bytesMd5In = Encoding.UTF8.GetBytes(srcStr);
  23. byte[] bytesMd5Out = md5.ComputeHash(bytesMd5In);
  24. var strMd5Out = length == Md5Length.L32
  25. ? BitConverter.ToString(bytesMd5Out)
  26. : BitConverter.ToString(bytesMd5Out, 4, 8);
  27. strMd5Out = strMd5Out.Replace("-", "");
  28. return strMd5Out;
  29. }
  30. }
  31. }