HashHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. namespace SAGA.DotNetUtils
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. public sealed class HashHelper
  8. {
  9. public static string ComputeCRC32(string fileName)
  10. {
  11. string str = string.Empty;
  12. if (!File.Exists(fileName))
  13. {
  14. return str;
  15. }
  16. using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  17. {
  18. Crc32 crc = new Crc32();
  19. byte[] buffer = crc.ComputeHash(stream);
  20. crc.Clear();
  21. StringBuilder builder = new StringBuilder();
  22. for (int i = 0; i < buffer.Length; i++)
  23. {
  24. builder.Append(buffer[i].ToString("x2"));
  25. }
  26. return builder.ToString();
  27. }
  28. }
  29. public static string ComputeHash(HashAlgorithm calculator, byte[] s)
  30. {
  31. using (MemoryStream stream = new MemoryStream(s))
  32. {
  33. byte[] buffer = calculator.ComputeHash(stream);
  34. calculator.Clear();
  35. StringBuilder builder = new StringBuilder();
  36. for (int i = 0; i < buffer.Length; i++)
  37. {
  38. builder.Append(buffer[i].ToString("x2"));
  39. }
  40. return builder.ToString();
  41. }
  42. }
  43. public static string ComputeHash(ComputeOptions option, byte[] s)
  44. {
  45. using (MemoryStream stream = new MemoryStream(s))
  46. {
  47. HashAlgorithm algorithm = null;
  48. switch (option)
  49. {
  50. case ComputeOptions.ComputeMD5:
  51. algorithm = MD5.Create();
  52. break;
  53. case ComputeOptions.ComputeSHA1:
  54. algorithm = SHA1.Create();
  55. break;
  56. default:
  57. algorithm = HashAlgorithm.Create();
  58. break;
  59. }
  60. byte[] buffer = algorithm.ComputeHash(stream);
  61. algorithm.Clear();
  62. StringBuilder builder = new StringBuilder();
  63. for (int i = 0; i < buffer.Length; i++)
  64. {
  65. builder.Append(buffer[i].ToString("x2"));
  66. }
  67. return builder.ToString();
  68. }
  69. }
  70. public static string ComputeMD5(Stream fs)
  71. {
  72. using (fs)
  73. {
  74. MD5 md = MD5.Create();
  75. byte[] buffer = md.ComputeHash(fs);
  76. md.Clear();
  77. StringBuilder builder = new StringBuilder();
  78. for (int i = 0; i < buffer.Length; i++)
  79. {
  80. builder.Append(buffer[i].ToString("x2"));
  81. }
  82. return builder.ToString();
  83. }
  84. }
  85. public static string ComputeMD5(string fileName)
  86. {
  87. string str = string.Empty;
  88. if (!File.Exists(fileName))
  89. {
  90. return str;
  91. }
  92. using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  93. {
  94. MD5 md = MD5.Create();
  95. byte[] buffer = md.ComputeHash(stream);
  96. md.Clear();
  97. StringBuilder builder = new StringBuilder();
  98. for (int i = 0; i < buffer.Length; i++)
  99. {
  100. builder.Append(buffer[i].ToString("x2"));
  101. }
  102. return builder.ToString();
  103. }
  104. }
  105. public static string ComputeSHA1(string fileName)
  106. {
  107. string str = string.Empty;
  108. if (!File.Exists(fileName))
  109. {
  110. return str;
  111. }
  112. using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  113. {
  114. SHA1 sha = SHA1.Create();
  115. byte[] buffer = sha.ComputeHash(stream);
  116. sha.Clear();
  117. StringBuilder builder = new StringBuilder();
  118. for (int i = 0; i < buffer.Length; i++)
  119. {
  120. builder.Append(buffer[i].ToString("x2"));
  121. }
  122. return builder.ToString();
  123. }
  124. }
  125. }
  126. }