MD5.cs 12 KB

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