MD5OTool.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //引用:https://blog.csdn.net/scimence/article/details/51593817
  2. using System;
  3. using System.IO;
  4. namespace Azylee.Core.DataUtils.EncryptUtils
  5. {
  6. //示例:
  7. // MD5.Encrypt("a"); // 计算字符串MD5值
  8. // MD5.Encrypt(new FileInfo("D:\\1.rar")); // 计算文件MD5值
  9. // MD5.Encrypt(byte[] Bytes); // 计算Byte数组MD5值
  10. //MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
  11. //MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
  12. //MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
  13. //MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
  14. //MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
  15. public class MD5OTool
  16. {
  17. #region MD5调用接口
  18. /// <summary>
  19. /// 计算data的MD5值
  20. /// </summary>
  21. public static string Encrypt(string data)
  22. {
  23. uint[] X = To16Array(data);
  24. return calculate(X);
  25. }
  26. /// <summary>
  27. /// 计算byte数组的MD5值
  28. /// </summary>
  29. public static string Encrypt(byte[] Bytes)
  30. {
  31. uint[] X = To16Array(Bytes);
  32. return calculate(X);
  33. }
  34. /// <summary>
  35. /// 计算文件的MD5值
  36. /// </summary>
  37. public static string Encrypt(FileInfo file)
  38. {
  39. uint[] X = To16Array(file);
  40. return calculate(X);
  41. }
  42. #endregion
  43. #region MD5计算逻辑
  44. /// <summary>
  45. /// 转化byte数组为uint数组,数组长度为16的倍数
  46. ///
  47. /// 1、字符串转化为字节数组,每4个字节转化为一个uint,依次存储到uint数组
  48. /// 2、附加0x80作为最后一个字节
  49. /// 3、在uint数组最后位置记录文件字节长度信息
  50. /// </summary>
  51. public static uint[] To16Array(byte[] Bytes)
  52. {
  53. uint DataLen = (uint)Bytes.Length;
  54. // 计算FileLen对应的uint长度(要求为16的倍数、预留2个uint、最小为16)
  55. uint ArrayLen = (((DataLen + 8) / 64) + 1) * 16;
  56. uint[] Array = new uint[ArrayLen];
  57. uint ArrayPos = 0;
  58. int pos = 0;
  59. uint ByteCount = 0;
  60. for (ByteCount = 0; ByteCount < DataLen; ByteCount++)
  61. {
  62. // 每4个Byte转化为1个uint
  63. ArrayPos = ByteCount / 4;
  64. pos = (int)(ByteCount % 4) * 8;
  65. Array[ArrayPos] = Array[ArrayPos] | ((uint)Bytes[ByteCount] << pos);
  66. }
  67. // 附加0x80作为最后一个字节,添加到uint数组对应位置
  68. ArrayPos = ByteCount / 4;
  69. pos = (int)(ByteCount % 4) * 8;
  70. Array[ArrayPos] = Array[ArrayPos] | ((uint)0x80 << pos);
  71. // 记录总长度信息
  72. Array[ArrayLen - 2] = (DataLen << 3);
  73. Array[ArrayLen - 1] = (DataLen >> 29);
  74. return Array;
  75. }
  76. /// <summary>
  77. /// 转化字符串为uint数组,数组长度为16的倍数
  78. ///
  79. /// 1、字符串转化为字节数组,每4个字节转化为一个uint,依次存储到uint数组
  80. /// 2、附加0x80作为最后一个字节
  81. /// 3、在uint数组最后位置记录文件字节长度信息
  82. /// </summary>
  83. public static uint[] To16Array(string data)
  84. {
  85. byte[] datas = System.Text.Encoding.Default.GetBytes(data);
  86. return To16Array(datas);
  87. }
  88. /// <summary>
  89. /// 转化文件为uint数组,数组长度为16的倍数
  90. ///
  91. /// 1、读取文件字节信息,每4个字节转化为一个uint,依次存储到uint数组
  92. /// 2、附加0x80作为最后一个字节
  93. /// 3、在uint数组最后位置记录文件字节长度信息
  94. /// </summary>
  95. public static uint[] To16Array(FileInfo info)
  96. {
  97. FileStream fs = new FileStream(info.FullName, FileMode.Open);// 读取方式打开,得到流
  98. int SIZE = 1024 * 1024 * 10; // 10M缓存
  99. byte[] datas = new byte[SIZE]; // 要读取的内容会放到这个数组里
  100. int countI = 0;
  101. long offset = 0;
  102. // 计算FileLen对应的uint长度(要求为16的倍数、预留2个uint、最小为16)
  103. uint FileLen = (uint)info.Length;
  104. uint ArrayLen = (((FileLen + 8) / 64) + 1) * 16;
  105. uint[] Array = new uint[ArrayLen];
  106. int pos = 0;
  107. uint ByteCount = 0;
  108. uint ArrayPos = 0;
  109. while (ByteCount < FileLen)
  110. {
  111. if (countI == 0)
  112. {
  113. fs.Seek(offset, SeekOrigin.Begin);// 定位到指定字节
  114. fs.Read(datas, 0, datas.Length);
  115. offset += SIZE;
  116. }
  117. // 每4个Byte转化为1个uint
  118. ArrayPos = ByteCount / 4;
  119. pos = (int)(ByteCount % 4) * 8;
  120. Array[ArrayPos] = Array[ArrayPos] | ((uint)datas[countI] << pos);
  121. ByteCount = ByteCount + 1;
  122. countI++;
  123. if (countI == SIZE) countI = 0;
  124. }
  125. // 附加0x80作为最后一个字节,添加到uint数组对应位置
  126. ArrayPos = ByteCount / 4;
  127. pos = (int)(ByteCount % 4) * 8;
  128. Array[ArrayPos] = Array[ArrayPos] | ((uint)0x80 << pos);
  129. // 记录总长度信息
  130. Array[ArrayLen - 2] = (FileLen << 3);
  131. Array[ArrayLen - 1] = (FileLen >> 29);
  132. fs.Close();
  133. return Array;
  134. }
  135. private static uint F(uint x, uint y, uint z)
  136. {
  137. return (x & y) | ((~x) & z);
  138. }
  139. private static uint G(uint x, uint y, uint z)
  140. {
  141. return (x & z) | (y & (~z));
  142. }
  143. // 0^0^0 = 0
  144. // 0^0^1 = 1
  145. // 0^1^0 = 1
  146. // 0^1^1 = 0
  147. // 1^0^0 = 1
  148. // 1^0^1 = 0
  149. // 1^1^0 = 0
  150. // 1^1^1 = 1
  151. private static uint H(uint x, uint y, uint z)
  152. {
  153. return (x ^ y ^ z);
  154. }
  155. private static uint I(uint x, uint y, uint z)
  156. {
  157. return (y ^ (x | (~z)));
  158. }
  159. // 循环左移
  160. private static uint RL(uint x, int y)
  161. {
  162. y = y % 32;
  163. return (x << y) | (x >> (32 - y));
  164. }
  165. private static void md5_FF(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
  166. {
  167. uint f = F(b, c, d);
  168. a = x + ac + a + f;
  169. a = RL(a, s);
  170. a = a + b;
  171. }
  172. private static void md5_GG(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
  173. {
  174. uint g = G(b, c, d);
  175. a = x + ac + a + g;
  176. a = RL(a, s);
  177. a = a + b;
  178. }
  179. private static void md5_HH(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
  180. {
  181. uint h = H(b, c, d);
  182. a = x + ac + a + h;
  183. a = RL(a, s);
  184. a = a + b;
  185. }
  186. private static void md5_II(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
  187. {
  188. uint i = I(b, c, d);
  189. a = x + ac + a + i;
  190. a = RL(a, s);
  191. a = a + b;
  192. }
  193. private static string RHex(uint n)
  194. {
  195. string S = Convert.ToString(n, 16);
  196. return ReOrder(S);
  197. }
  198. // 16进制串重排序 67452301 -> 01234567
  199. private static string ReOrder(String S)
  200. {
  201. string T = "";
  202. for (int i = S.Length - 2; i >= 0; i = i - 2)
  203. {
  204. if (i == -1) T = T + "0" + S[i + 1];
  205. else T = T + "" + S[i] + S[i + 1];
  206. }
  207. return T;
  208. }
  209. /// <summary>
  210. /// 对长度为16倍数的uint数组,执行md5数据摘要,输出md5信息
  211. /// </summary>
  212. public static string calculate(uint[] x)
  213. {
  214. //uint time1 = DateTime.Now.Ticks;
  215. // 7 12 17 22
  216. // 5 9 14 20
  217. // 4 11 16 23
  218. // 6 10 15 21
  219. const int S11 = 7;
  220. const int S12 = 12;
  221. const int S13 = 17;
  222. const int S14 = 22;
  223. const int S21 = 5;
  224. const int S22 = 9;
  225. const int S23 = 14;
  226. const int S24 = 20;
  227. const int S31 = 4;
  228. const int S32 = 11;
  229. const int S33 = 16;
  230. const int S34 = 23;
  231. const int S41 = 6;
  232. const int S42 = 10;
  233. const int S43 = 15;
  234. const int S44 = 21;
  235. uint a = 0x67452301;
  236. uint b = 0xEFCDAB89;
  237. uint c = 0x98BADCFE;
  238. uint d = 0x10325476;
  239. for (int k = 0; k < x.Length; k += 16)
  240. {
  241. uint AA = a;
  242. uint BB = b;
  243. uint CC = c;
  244. uint DD = d;
  245. md5_FF(ref a, b, c, d, x[k + 0], S11, 0xD76AA478); // 3604027302
  246. md5_FF(ref d, a, b, c, x[k + 1], S12, 0xE8C7B756); // 877880356
  247. md5_FF(ref c, d, a, b, x[k + 2], S13, 0x242070DB); // 2562383102
  248. md5_FF(ref b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
  249. md5_FF(ref a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
  250. md5_FF(ref d, a, b, c, x[k + 5], S12, 0x4787C62A);
  251. md5_FF(ref c, d, a, b, x[k + 6], S13, 0xA8304613);
  252. md5_FF(ref b, c, d, a, x[k + 7], S14, 0xFD469501);
  253. md5_FF(ref a, b, c, d, x[k + 8], S11, 0x698098D8);
  254. md5_FF(ref d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
  255. md5_FF(ref c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
  256. md5_FF(ref b, c, d, a, x[k + 11], S14, 0x895CD7BE);
  257. md5_FF(ref a, b, c, d, x[k + 12], S11, 0x6B901122);
  258. md5_FF(ref d, a, b, c, x[k + 13], S12, 0xFD987193);
  259. md5_FF(ref c, d, a, b, x[k + 14], S13, 0xA679438E);
  260. md5_FF(ref b, c, d, a, x[k + 15], S14, 0x49B40821); //3526238649
  261. md5_GG(ref a, b, c, d, x[k + 1], S21, 0xF61E2562);
  262. md5_GG(ref d, a, b, c, x[k + 6], S22, 0xC040B340); //1572812400
  263. md5_GG(ref c, d, a, b, x[k + 11], S23, 0x265E5A51);
  264. md5_GG(ref b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
  265. md5_GG(ref a, b, c, d, x[k + 5], S21, 0xD62F105D);
  266. md5_GG(ref d, a, b, c, x[k + 10], S22, 0x2441453);
  267. md5_GG(ref c, d, a, b, x[k + 15], S23, 0xD8A1E681);
  268. md5_GG(ref b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
  269. md5_GG(ref a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
  270. md5_GG(ref d, a, b, c, x[k + 14], S22, 0xC33707D6);
  271. md5_GG(ref c, d, a, b, x[k + 3], S23, 0xF4D50D87);
  272. md5_GG(ref b, c, d, a, x[k + 8], S24, 0x455A14ED);
  273. md5_GG(ref a, b, c, d, x[k + 13], S21, 0xA9E3E905);
  274. md5_GG(ref d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
  275. md5_GG(ref c, d, a, b, x[k + 7], S23, 0x676F02D9);
  276. md5_GG(ref b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
  277. md5_HH(ref a, b, c, d, x[k + 5], S31, 0xFFFA3942); // 3750198684 2314002400 1089690627 990001115 0 4 -> 2749600077
  278. md5_HH(ref d, a, b, c, x[k + 8], S32, 0x8771F681); // 990001115
  279. md5_HH(ref c, d, a, b, x[k + 11], S33, 0x6D9D6122); // 1089690627
  280. md5_HH(ref b, c, d, a, x[k + 14], S34, 0xFDE5380C); // 2314002400
  281. md5_HH(ref a, b, c, d, x[k + 1], S31, 0xA4BEEA44); // 555633090
  282. md5_HH(ref d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
  283. md5_HH(ref c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
  284. md5_HH(ref b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
  285. md5_HH(ref a, b, c, d, x[k + 13], S31, 0x289B7EC6);
  286. md5_HH(ref d, a, b, c, x[k + 0], S32, 0xEAA127FA);
  287. md5_HH(ref c, d, a, b, x[k + 3], S33, 0xD4EF3085);
  288. md5_HH(ref b, c, d, a, x[k + 6], S34, 0x4881D05);
  289. md5_HH(ref a, b, c, d, x[k + 9], S31, 0xD9D4D039);
  290. md5_HH(ref d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
  291. md5_HH(ref c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
  292. md5_HH(ref b, c, d, a, x[k + 2], S34, 0xC4AC5665); // 1444303940
  293. md5_II(ref a, b, c, d, x[k + 0], S41, 0xF4292244); // 808311156
  294. md5_II(ref d, a, b, c, x[k + 7], S42, 0x432AFF97);
  295. md5_II(ref c, d, a, b, x[k + 14], S43, 0xAB9423A7);
  296. md5_II(ref b, c, d, a, x[k + 5], S44, 0xFC93A039);
  297. md5_II(ref a, b, c, d, x[k + 12], S41, 0x655B59C3);
  298. md5_II(ref d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
  299. md5_II(ref c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
  300. md5_II(ref b, c, d, a, x[k + 1], S44, 0x85845DD1);
  301. md5_II(ref a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
  302. md5_II(ref d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
  303. md5_II(ref c, d, a, b, x[k + 6], S43, 0xA3014314);
  304. md5_II(ref b, c, d, a, x[k + 13], S44, 0x4E0811A1);
  305. md5_II(ref a, b, c, d, x[k + 4], S41, 0xF7537E82);
  306. md5_II(ref d, a, b, c, x[k + 11], S42, 0xBD3AF235);
  307. md5_II(ref c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
  308. md5_II(ref b, c, d, a, x[k + 9], S44, 0xEB86D391); // 4120542881
  309. a = a + AA; //3844921825
  310. b = b + BB;
  311. c = c + CC;
  312. d = d + DD;
  313. }
  314. string MD5 = RHex(a) + RHex(b) + RHex(c) + RHex(d);
  315. //uint time2 = DateTime.Now.Ticks;
  316. //MessageBox.Show("MD5计算耗时:" + ((time2 - time1) / 10000000f) + "秒");
  317. return MD5;
  318. }
  319. #endregion
  320. }
  321. }