MD5Crypter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.Linq;
  6. namespace Wayne.FDCPOSLibrary
  7. {
  8. public class MD5Crypter
  9. {
  10. System.Security.Cryptography.MD5CryptoServiceProvider cryptic;
  11. public static string passphrase = "DresserFusion}123";
  12. public MD5Crypter()
  13. {
  14. cryptic = new System.Security.Cryptography.MD5CryptoServiceProvider();
  15. cryptic.Initialize();
  16. }
  17. public byte[] getPassphrase()
  18. {
  19. return System.Text.Encoding.ASCII.GetBytes(MD5Crypter.passphrase);
  20. //return UTF8Encoding.UTF8.GetBytes(MD5Crypter.passphrase);
  21. }
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. /// <param name="myString"></param>
  26. /// <returns>hex string</returns>
  27. public string getMD5Hash(string myString)
  28. {
  29. byte[] data = System.Text.Encoding.UTF8.GetBytes(myString);
  30. //byte[] data = UTF8Encoding.UTF8.GetBytes(myString);
  31. var hashBytes = cryptic.ComputeHash(data);
  32. return hashBytes.Select(h => h.ToString("x2")).Aggregate((acc, n) => acc + n);
  33. //return UTF8Encoding.UTF8.GetString(data, 0, data.GetLength(0));
  34. }
  35. public string getMD5Hash(byte[] data)
  36. {
  37. data = cryptic.ComputeHash(data);
  38. return System.Text.Encoding.ASCII.GetString(data, 0, data.GetLength(0));
  39. //return UTF8Encoding.UTF8.GetString(data, 0, data.GetLength(0));
  40. }
  41. public byte[] ComputeHash(byte[] data)
  42. {
  43. return cryptic.ComputeHash(data);
  44. }
  45. // Hash an input string and return the hash as
  46. // a 32 character hexadecimal string.
  47. //public string getMd5Hash(string input)
  48. //{
  49. // // Create a new instance of the MD5CryptoServiceProvider object.
  50. // MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
  51. // // Convert the input string to a byte array and compute the hash.
  52. // byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
  53. // // Create a new Stringbuilder to collect the bytes
  54. // // and create a string.
  55. // StringBuilder sBuilder = new StringBuilder();
  56. // // Loop through each byte of the hashed data
  57. // // and format each one as a hexadecimal string.
  58. // for (int i = 0; i < data.Length; i++)
  59. // {
  60. // sBuilder.Append(data[i].ToString("x2"));
  61. // }
  62. // // Return the hexadecimal string.
  63. // return sBuilder.ToString();
  64. //}
  65. /// <summary>
  66. /// Verify a hash against a string.
  67. /// </summary>
  68. /// <param name="input"></param>
  69. /// <param name="expectingHashHexBytesString">hex string, may contains unreadable char</param>
  70. /// <returns></returns>
  71. public bool verifyMD5Hash(string input, string expectingHashHexBytesString)
  72. {
  73. // Hash the input.
  74. string hashOfInput = getMD5Hash(input);
  75. // Create a StringComparer an compare the hashes.
  76. StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  77. if (0 == comparer.Compare(hashOfInput, expectingHashHexBytesString))
  78. {
  79. return true;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. }
  87. }