MD5.cs 14 KB

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