Md5Helper.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.Utilities
  8. {
  9. public class Md5Helper
  10. {
  11. /// <summary>
  12. /// md5 string compare
  13. /// </summary>
  14. /// <param name="input"></param>
  15. /// <param name="hash"></param>
  16. /// <returns></returns>
  17. public static bool VerifyMd5Hash(string input, string hash)
  18. {
  19. string hashOfInput = GetMd5Hash(input);
  20. StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  21. if (0 == comparer.Compare(hashOfInput, hash))
  22. return true;
  23. return false;
  24. }
  25. /// <summary>
  26. /// get md5 string
  27. /// </summary>
  28. /// <param name="input"></param>
  29. /// <returns></returns>
  30. public static string GetMd5Hash(string input)
  31. {
  32. MD5 md5Hasher = MD5.Create();
  33. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
  34. StringBuilder sBuilder = new StringBuilder();
  35. for (int i = 0; i < data.Length; i++)
  36. {
  37. sBuilder.Append(data[i].ToString("x2"));
  38. }
  39. return sBuilder.ToString();
  40. }
  41. public static string GenerateDynamicPassword(string serialNum)
  42. {
  43. try
  44. {
  45. string genString = "promaxy" + serialNum + DateTime.Now.ToString("yyyyMMdd");
  46. string hash = Md5Helper.GetMd5Hash(genString);
  47. return hash.Substring(0, 8);
  48. }
  49. catch (Exception ex)
  50. {
  51. LOG.Write(ex);
  52. return "";
  53. }
  54. }
  55. }
  56. }