DigestUtils.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. namespace COSXML.Utils
  7. {
  8. public sealed class DigestUtils
  9. {
  10. public static string GetSha1ToHexString(string content, Encoding encode)
  11. {
  12. return GetSha1ToHexString(encode.GetBytes(content));
  13. }
  14. public static string GetSha1ToHexString(byte[] content)
  15. {
  16. SHA1 sha1 = new SHA1CryptoServiceProvider();
  17. byte[] result = sha1.ComputeHash(content);
  18. sha1.Clear();
  19. var hexStr = new StringBuilder();
  20. foreach (byte b in result)
  21. {
  22. // to lower
  23. // to lower
  24. hexStr.Append(b.ToString("x2"));
  25. }
  26. return hexStr.ToString();
  27. }
  28. // public static string GetSha1ToHexString(Stream inputStream)
  29. // {
  30. // SHA1 sha1 = new SHA1CryptoServiceProvider();
  31. // byte[] result = sha1.ComputeHash(inputStream);
  32. // sha1.Clear();
  33. // var hexStr = new StringBuilder();
  34. // foreach (byte b in result)
  35. // {
  36. // // to lower
  37. // // to lower
  38. // hexStr.Append(b.ToString("x2"));
  39. // }
  40. // return hexStr.ToString();
  41. // }
  42. public static string GetHamcSha1ToHexString(string content, Encoding contentEncoding, string key, Encoding keyEncoding)
  43. {
  44. HMACSHA1 hmacSha1 = new HMACSHA1(keyEncoding.GetBytes(key));
  45. byte[] result = hmacSha1.ComputeHash(contentEncoding.GetBytes(content));
  46. hmacSha1.Clear();
  47. var hexStr = new StringBuilder();
  48. foreach (byte b in result)
  49. {
  50. // to lower
  51. // to lower
  52. hexStr.Append(b.ToString("x2"));
  53. }
  54. return hexStr.ToString();
  55. }
  56. public static string GetHamcSha1ToBase64(string content, Encoding contentEncoding, string key, Encoding keyEncoding)
  57. {
  58. HMACSHA1 hmacSha1 = new HMACSHA1(keyEncoding.GetBytes(key));
  59. byte[] result = hmacSha1.ComputeHash(contentEncoding.GetBytes(content));
  60. hmacSha1.Clear();
  61. return Convert.ToBase64String(result);
  62. }
  63. public static string GetMd5ToBase64(string content, Encoding encoding)
  64. {
  65. return GetMd5ToBase64(encoding.GetBytes(content));
  66. }
  67. public static string GetMd5ToBase64(byte[] content)
  68. {
  69. MD5 md5 = MD5.Create();
  70. byte[] result = md5.ComputeHash(content);
  71. md5.Clear();
  72. return Convert.ToBase64String(result);
  73. }
  74. public static string GetMd5ToBase64(Stream inStream)
  75. {
  76. MD5 md5 = MD5.Create();
  77. byte[] result = md5.ComputeHash(inStream);
  78. md5.Clear();
  79. return Convert.ToBase64String(result);
  80. }
  81. public static string GetMd5ToBase64(Stream inStream, long size)
  82. {
  83. int bufferSize = 1024 * 16;
  84. byte[] buffer = new byte[bufferSize];
  85. int readLength = 0;
  86. long total = 0L;
  87. byte[] data = new byte[bufferSize];
  88. MD5 md5 = MD5.Create();
  89. int count = (int)(size - total);
  90. while ((readLength = inStream.Read(buffer, 0, count > buffer.Length ? buffer.Length : count)) > 0)
  91. {
  92. md5.TransformBlock(buffer, 0, readLength, data, 0);
  93. total += readLength;
  94. count = (int)(size - total);
  95. if (count <= 0)
  96. {
  97. break;
  98. }
  99. }
  100. md5.TransformFinalBlock(buffer, 0, 0);
  101. string result = Convert.ToBase64String(md5.Hash);
  102. md5.Clear();
  103. return result;
  104. }
  105. public static string GetMD5HexString(Stream inStream, long size)
  106. {
  107. int bufferSize = 1024 * 16;
  108. byte[] buffer = new byte[bufferSize];
  109. int readLength = 0;
  110. long total = 0L;
  111. byte[] data = new byte[bufferSize];
  112. MD5 md5 = MD5.Create();
  113. int count = (int)(size - total);
  114. while ((readLength = inStream.Read(buffer, 0, count > buffer.Length ? buffer.Length : count)) > 0)
  115. {
  116. md5.TransformBlock(buffer, 0, readLength, data, 0);
  117. total += readLength;
  118. count = (int)(size - total);
  119. if (count <= 0)
  120. {
  121. break;
  122. }
  123. }
  124. md5.TransformFinalBlock(buffer, 0, 0);
  125. string result = ByteArrayToHex(md5.Hash).ToLower();
  126. md5.Clear();
  127. return result;
  128. }
  129. public static string ByteArrayToHex(byte[] barray)
  130. {
  131. char[] c = new char[barray.Length * 2];
  132. byte b;
  133. for (int i = 0; i < barray.Length; ++i)
  134. {
  135. b = ((byte)(barray[i] >> 4));
  136. c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
  137. b = ((byte)(barray[i] & 0xF));
  138. c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
  139. }
  140. return new string(c);
  141. }
  142. public static string GetBase64(string content, Encoding encoding)
  143. {
  144. byte[] result = encoding.GetBytes(content);
  145. return Convert.ToBase64String(result);
  146. }
  147. }
  148. }