You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. //-----------------------------------------------------------------------
  2. // <copyright file="HashMgr.cs" company="Origtek">
  3. // HashMgr belongs to Copyright (c) Origtek. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. namespace OT.COM.Encryption
  7. {
  8. using System;
  9. using System.Security.Cryptography;
  10. /// <summary>
  11. /// Manager of Hash function
  12. /// </summary>
  13. public partial class HashMgr
  14. {
  15. /// <summary>
  16. /// For choose hash type
  17. /// </summary>
  18. public enum HashType
  19. {
  20. /// <summary>
  21. /// MD5 algorithm
  22. /// </summary>
  23. HT_MD5 = 0,
  24. /// <summary>
  25. /// SHA1 algorithm
  26. /// </summary>
  27. HT_SHA1 = 1,
  28. /// <summary>
  29. /// SHA256 algorithm
  30. /// </summary>
  31. HT_SHA256 = 2,
  32. /// <summary>
  33. /// SHA384 algorithm
  34. /// </summary>
  35. HT_SHA384 = 3,
  36. /// <summary>
  37. /// SHA512 algorithm
  38. /// </summary>
  39. HT_SHA512 = 4,
  40. }
  41. /// <summary>
  42. /// Generate hash function
  43. /// </summary>
  44. /// <param name="i_htType">
  45. /// Hash type
  46. /// </param>
  47. /// <param name="i_sData">
  48. /// string data
  49. /// </param>
  50. /// <returns>
  51. /// Result string
  52. /// </returns>
  53. public string Generate(HashType i_htType, string i_sData)
  54. {
  55. return System.Text.Encoding.Unicode.GetString(
  56. this.Gernerate(
  57. i_htType,
  58. System.Text.Encoding.Unicode.GetBytes(i_sData)));
  59. }
  60. /// <summary>
  61. /// Base function of Generate
  62. /// </summary>
  63. /// <param name="i_htType">
  64. /// Hash type
  65. /// </param>
  66. /// <param name="i_byData">
  67. /// Byte array data
  68. /// </param>
  69. /// <returns>
  70. /// Result byte array
  71. /// </returns>
  72. public byte[] Gernerate(HashType i_htType, byte[] i_byData)
  73. {
  74. // This is one implementation of the abstract class MD5.
  75. string sName = Enum.GetName(typeof(HashType), i_htType);
  76. byte[] result = null;
  77. object oRes = null;
  78. string s = string.Format("System.Security.Cryptography.{0}CryptoServiceProvider", sName.Substring(3));
  79. switch (i_htType)
  80. {
  81. case HashType.HT_SHA256:
  82. {
  83. oRes = new SHA256CryptoServiceProvider();
  84. }
  85. break;
  86. case HashType.HT_SHA384:
  87. {
  88. oRes = new SHA384CryptoServiceProvider();
  89. }
  90. break;
  91. case HashType.HT_SHA512:
  92. {
  93. oRes = new SHA512CryptoServiceProvider();
  94. }
  95. break;
  96. default:
  97. {
  98. }
  99. break;
  100. }
  101. if (oRes != null && oRes is HashAlgorithm)
  102. {
  103. result = (oRes as HashAlgorithm).ComputeHash(i_byData);
  104. }
  105. return result;
  106. }
  107. }
  108. }