Md5Helper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. public static string GetMd5Hash(string input)
  26. {
  27. MD5 md5Hasher = MD5.Create();
  28. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
  29. StringBuilder sBuilder = new StringBuilder();
  30. for (int i = 0; i < data.Length; i++)
  31. {
  32. sBuilder.Append(data[i].ToString("x2"));
  33. }
  34. return sBuilder.ToString();
  35. }
  36. public static string GenerateDynamicPassword(string serialNum)
  37. {
  38. try
  39. {
  40. string genString = "aitex" + serialNum + DateTime.Now.ToString("yyyyMMdd");
  41. string hash = Md5Helper.GetMd5Hash(genString);
  42. return hash.Substring(0, 8);
  43. }
  44. catch (Exception ex)
  45. {
  46. LOG.Write(ex);
  47. return "";
  48. }
  49. }
  50. }
  51. }