MD5.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Text;
  4. namespace SharpCifs.Util.Sharpen
  5. { // **************************************************************
  6. // * Raw implementation of the MD5 hash algorithm
  7. // * from RFC 1321.
  8. // *
  9. // * Written By: Reid Borsuk and Jenny Zheng
  10. // * Copyright (c) Microsoft Corporation. All rights reserved.
  11. // **************************************************************
  12. // Simple struct for the (a,b,c,d) which is used to compute the mesage digest.
  13. struct AbcdStruct
  14. {
  15. public uint A;
  16. public uint B;
  17. public uint C;
  18. public uint D;
  19. }
  20. public sealed class Md5Core
  21. {
  22. //Prevent CSC from adding a default public constructor
  23. private Md5Core() { }
  24. public static byte[] GetHash(string input, Encoding encoding)
  25. {
  26. if (null == input)
  27. throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
  28. if (null == encoding)
  29. throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHash(string) overload to use UTF8 Encoding");
  30. byte[] target = encoding.GetBytes(input);
  31. return GetHash(target);
  32. }
  33. public static byte[] GetHash(string input)
  34. {
  35. return GetHash(input, new UTF8Encoding());
  36. }
  37. public static string GetHashString(byte[] input)
  38. {
  39. if (null == input)
  40. throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
  41. string retval = BitConverter.ToString(GetHash(input));
  42. retval = retval.Replace("-", "");
  43. return retval;
  44. }
  45. public static string GetHashString(string input, Encoding encoding)
  46. {
  47. if (null == input)
  48. throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
  49. if (null == encoding)
  50. throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHashString(string) overload to use UTF8 Encoding");
  51. byte[] target = encoding.GetBytes(input);
  52. return GetHashString(target);
  53. }
  54. public static string GetHashString(string input)
  55. {
  56. return GetHashString(input, new UTF8Encoding());
  57. }
  58. public static byte[] GetHash(byte[] input)
  59. {
  60. if (null == input)
  61. throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
  62. //Intitial values defined in RFC 1321
  63. AbcdStruct abcd = new AbcdStruct();
  64. abcd.A = 0x67452301;
  65. abcd.B = 0xefcdab89;
  66. abcd.C = 0x98badcfe;
  67. abcd.D = 0x10325476;
  68. //We pass in the input array by block, the final block of data must be handled specialy for padding & length embeding
  69. int startIndex = 0;
  70. while (startIndex <= input.Length - 64)
  71. {
  72. GetHashBlock(input, ref abcd, startIndex);
  73. startIndex += 64;
  74. }
  75. // The final data block.
  76. return GetHashFinalBlock(input, startIndex, input.Length - startIndex, abcd, (Int64)input.Length * 8);
  77. }
  78. internal static byte[] GetHashFinalBlock(byte[] input, int ibStart, int cbSize, AbcdStruct abcd, Int64 len)
  79. {
  80. byte[] working = new byte[64];
  81. byte[] length = BitConverter.GetBytes(len);
  82. //Padding is a single bit 1, followed by the number of 0s required to make size congruent to 448 modulo 512. Step 1 of RFC 1321
  83. //The CLR ensures that our buffer is 0-assigned, we don't need to explicitly set it. This is why it ends up being quicker to just
  84. //use a temporary array rather then doing in-place assignment (5% for small inputs)
  85. Array.Copy(input, ibStart, working, 0, cbSize);
  86. working[cbSize] = 0x80;
  87. //We have enough room to store the length in this chunk
  88. if (cbSize < 56)
  89. {
  90. Array.Copy(length, 0, working, 56, 8);
  91. GetHashBlock(working, ref abcd, 0);
  92. }
  93. else //We need an aditional chunk to store the length
  94. {
  95. GetHashBlock(working, ref abcd, 0);
  96. //Create an entirely new chunk due to the 0-assigned trick mentioned above, to avoid an extra function call clearing the array
  97. working = new byte[64];
  98. Array.Copy(length, 0, working, 56, 8);
  99. GetHashBlock(working, ref abcd, 0);
  100. }
  101. byte[] output = new byte[16];
  102. Array.Copy(BitConverter.GetBytes(abcd.A), 0, output, 0, 4);
  103. Array.Copy(BitConverter.GetBytes(abcd.B), 0, output, 4, 4);
  104. Array.Copy(BitConverter.GetBytes(abcd.C), 0, output, 8, 4);
  105. Array.Copy(BitConverter.GetBytes(abcd.D), 0, output, 12, 4);
  106. return output;
  107. }
  108. // Performs a single block transform of MD5 for a given set of ABCD inputs
  109. /* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321:
  110. // A = 0x67452301;
  111. // B = 0xefcdab89;
  112. // C = 0x98badcfe;
  113. // D = 0x10325476;
  114. */
  115. internal static void GetHashBlock(byte[] input, ref AbcdStruct abcdValue, int ibStart)
  116. {
  117. uint[] temp = Converter(input, ibStart);
  118. uint a = abcdValue.A;
  119. uint b = abcdValue.B;
  120. uint c = abcdValue.C;
  121. uint d = abcdValue.D;
  122. a = R1(a, b, c, d, temp[0], 7, 0xd76aa478);
  123. d = R1(d, a, b, c, temp[1], 12, 0xe8c7b756);
  124. c = R1(c, d, a, b, temp[2], 17, 0x242070db);
  125. b = R1(b, c, d, a, temp[3], 22, 0xc1bdceee);
  126. a = R1(a, b, c, d, temp[4], 7, 0xf57c0faf);
  127. d = R1(d, a, b, c, temp[5], 12, 0x4787c62a);
  128. c = R1(c, d, a, b, temp[6], 17, 0xa8304613);
  129. b = R1(b, c, d, a, temp[7], 22, 0xfd469501);
  130. a = R1(a, b, c, d, temp[8], 7, 0x698098d8);
  131. d = R1(d, a, b, c, temp[9], 12, 0x8b44f7af);
  132. c = R1(c, d, a, b, temp[10], 17, 0xffff5bb1);
  133. b = R1(b, c, d, a, temp[11], 22, 0x895cd7be);
  134. a = R1(a, b, c, d, temp[12], 7, 0x6b901122);
  135. d = R1(d, a, b, c, temp[13], 12, 0xfd987193);
  136. c = R1(c, d, a, b, temp[14], 17, 0xa679438e);
  137. b = R1(b, c, d, a, temp[15], 22, 0x49b40821);
  138. a = R2(a, b, c, d, temp[1], 5, 0xf61e2562);
  139. d = R2(d, a, b, c, temp[6], 9, 0xc040b340);
  140. c = R2(c, d, a, b, temp[11], 14, 0x265e5a51);
  141. b = R2(b, c, d, a, temp[0], 20, 0xe9b6c7aa);
  142. a = R2(a, b, c, d, temp[5], 5, 0xd62f105d);
  143. d = R2(d, a, b, c, temp[10], 9, 0x02441453);
  144. c = R2(c, d, a, b, temp[15], 14, 0xd8a1e681);
  145. b = R2(b, c, d, a, temp[4], 20, 0xe7d3fbc8);
  146. a = R2(a, b, c, d, temp[9], 5, 0x21e1cde6);
  147. d = R2(d, a, b, c, temp[14], 9, 0xc33707d6);
  148. c = R2(c, d, a, b, temp[3], 14, 0xf4d50d87);
  149. b = R2(b, c, d, a, temp[8], 20, 0x455a14ed);
  150. a = R2(a, b, c, d, temp[13], 5, 0xa9e3e905);
  151. d = R2(d, a, b, c, temp[2], 9, 0xfcefa3f8);
  152. c = R2(c, d, a, b, temp[7], 14, 0x676f02d9);
  153. b = R2(b, c, d, a, temp[12], 20, 0x8d2a4c8a);
  154. a = R3(a, b, c, d, temp[5], 4, 0xfffa3942);
  155. d = R3(d, a, b, c, temp[8], 11, 0x8771f681);
  156. c = R3(c, d, a, b, temp[11], 16, 0x6d9d6122);
  157. b = R3(b, c, d, a, temp[14], 23, 0xfde5380c);
  158. a = R3(a, b, c, d, temp[1], 4, 0xa4beea44);
  159. d = R3(d, a, b, c, temp[4], 11, 0x4bdecfa9);
  160. c = R3(c, d, a, b, temp[7], 16, 0xf6bb4b60);
  161. b = R3(b, c, d, a, temp[10], 23, 0xbebfbc70);
  162. a = R3(a, b, c, d, temp[13], 4, 0x289b7ec6);
  163. d = R3(d, a, b, c, temp[0], 11, 0xeaa127fa);
  164. c = R3(c, d, a, b, temp[3], 16, 0xd4ef3085);
  165. b = R3(b, c, d, a, temp[6], 23, 0x04881d05);
  166. a = R3(a, b, c, d, temp[9], 4, 0xd9d4d039);
  167. d = R3(d, a, b, c, temp[12], 11, 0xe6db99e5);
  168. c = R3(c, d, a, b, temp[15], 16, 0x1fa27cf8);
  169. b = R3(b, c, d, a, temp[2], 23, 0xc4ac5665);
  170. a = R4(a, b, c, d, temp[0], 6, 0xf4292244);
  171. d = R4(d, a, b, c, temp[7], 10, 0x432aff97);
  172. c = R4(c, d, a, b, temp[14], 15, 0xab9423a7);
  173. b = R4(b, c, d, a, temp[5], 21, 0xfc93a039);
  174. a = R4(a, b, c, d, temp[12], 6, 0x655b59c3);
  175. d = R4(d, a, b, c, temp[3], 10, 0x8f0ccc92);
  176. c = R4(c, d, a, b, temp[10], 15, 0xffeff47d);
  177. b = R4(b, c, d, a, temp[1], 21, 0x85845dd1);
  178. a = R4(a, b, c, d, temp[8], 6, 0x6fa87e4f);
  179. d = R4(d, a, b, c, temp[15], 10, 0xfe2ce6e0);
  180. c = R4(c, d, a, b, temp[6], 15, 0xa3014314);
  181. b = R4(b, c, d, a, temp[13], 21, 0x4e0811a1);
  182. a = R4(a, b, c, d, temp[4], 6, 0xf7537e82);
  183. d = R4(d, a, b, c, temp[11], 10, 0xbd3af235);
  184. c = R4(c, d, a, b, temp[2], 15, 0x2ad7d2bb);
  185. b = R4(b, c, d, a, temp[9], 21, 0xeb86d391);
  186. abcdValue.A = unchecked(a + abcdValue.A);
  187. abcdValue.B = unchecked(b + abcdValue.B);
  188. abcdValue.C = unchecked(c + abcdValue.C);
  189. abcdValue.D = unchecked(d + abcdValue.D);
  190. }
  191. //Manually unrolling these equations nets us a 20% performance improvement
  192. private static uint R1(uint a, uint b, uint c, uint d, uint x, int s, uint t)
  193. {
  194. // (b + LSR((a + F(b, c, d) + x + t), s))
  195. //F(x, y, z) ((x & y) | ((x ^ 0xFFFFFFFF) & z))
  196. return unchecked(b + Lsr((a + ((b & c) | ((b ^ 0xFFFFFFFF) & d)) + x + t), s));
  197. }
  198. private static uint R2(uint a, uint b, uint c, uint d, uint x, int s, uint t)
  199. {
  200. // (b + LSR((a + G(b, c, d) + x + t), s))
  201. //G(x, y, z) ((x & z) | (y & (z ^ 0xFFFFFFFF)))
  202. return unchecked(b + Lsr((a + ((b & d) | (c & (d ^ 0xFFFFFFFF))) + x + t), s));
  203. }
  204. private static uint R3(uint a, uint b, uint c, uint d, uint x, int s, uint t)
  205. {
  206. // (b + LSR((a + H(b, c, d) + k + i), s))
  207. //H(x, y, z) (x ^ y ^ z)
  208. return unchecked(b + Lsr((a + (b ^ c ^ d) + x + t), s));
  209. }
  210. private static uint R4(uint a, uint b, uint c, uint d, uint x, int s, uint t)
  211. {
  212. // (b + LSR((a + I(b, c, d) + k + i), s))
  213. //I(x, y, z) (y ^ (x | (z ^ 0xFFFFFFFF)))
  214. return unchecked(b + Lsr((a + (c ^ (b | (d ^ 0xFFFFFFFF))) + x + t), s));
  215. }
  216. // Implementation of left rotate
  217. // s is an int instead of a uint becuase the CLR requires the argument passed to >>/<< is of
  218. // type int. Doing the demoting inside this function would add overhead.
  219. private static uint Lsr(uint i, int s)
  220. {
  221. return ((i << s) | (i >> (32 - s)));
  222. }
  223. //Convert input array into array of UInts
  224. private static uint[] Converter(byte[] input, int ibStart)
  225. {
  226. if (null == input)
  227. throw new ArgumentNullException("input", "Unable convert null array to array of uInts");
  228. uint[] result = new uint[16];
  229. for (int i = 0; i < 16; i++)
  230. {
  231. result[i] = input[ibStart + i * 4];
  232. result[i] += (uint)input[ibStart + i * 4 + 1] << 8;
  233. result[i] += (uint)input[ibStart + i * 4 + 2] << 16;
  234. result[i] += (uint)input[ibStart + i * 4 + 3] << 24;
  235. }
  236. return result;
  237. }
  238. }
  239. }