MD5Crypter.cs 2.9 KB

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