MD5Digest.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Utilities;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * implementation of MD5 as outlined in "Handbook of Applied Cryptography", pages 346 - 347.
  9. */
  10. public class MD5Digest
  11. : GeneralDigest
  12. {
  13. private const int DigestLength = 16;
  14. private uint H1, H2, H3, H4; // IV's
  15. private uint[] X = new uint[16];
  16. private int xOff;
  17. public MD5Digest()
  18. {
  19. Reset();
  20. }
  21. /**
  22. * Copy constructor. This will copy the state of the provided
  23. * message digest.
  24. */
  25. public MD5Digest(MD5Digest t)
  26. : base(t)
  27. {
  28. CopyIn(t);
  29. }
  30. private void CopyIn(MD5Digest t)
  31. {
  32. base.CopyIn(t);
  33. H1 = t.H1;
  34. H2 = t.H2;
  35. H3 = t.H3;
  36. H4 = t.H4;
  37. Array.Copy(t.X, 0, X, 0, t.X.Length);
  38. xOff = t.xOff;
  39. }
  40. public override string AlgorithmName
  41. {
  42. get { return "MD5"; }
  43. }
  44. public override int GetDigestSize()
  45. {
  46. return DigestLength;
  47. }
  48. internal override void ProcessWord(
  49. byte[] input,
  50. int inOff)
  51. {
  52. X[xOff] = Pack.LE_To_UInt32(input, inOff);
  53. if (++xOff == 16)
  54. {
  55. ProcessBlock();
  56. }
  57. }
  58. internal override void ProcessLength(
  59. long bitLength)
  60. {
  61. if (xOff > 14)
  62. {
  63. if (xOff == 15)
  64. X[15] = 0;
  65. ProcessBlock();
  66. }
  67. for (int i = xOff; i < 14; ++i)
  68. {
  69. X[i] = 0;
  70. }
  71. X[14] = (uint)((ulong)bitLength);
  72. X[15] = (uint)((ulong)bitLength >> 32);
  73. }
  74. public override int DoFinal(
  75. byte[] output,
  76. int outOff)
  77. {
  78. Finish();
  79. Pack.UInt32_To_LE(H1, output, outOff);
  80. Pack.UInt32_To_LE(H2, output, outOff + 4);
  81. Pack.UInt32_To_LE(H3, output, outOff + 8);
  82. Pack.UInt32_To_LE(H4, output, outOff + 12);
  83. Reset();
  84. return DigestLength;
  85. }
  86. /**
  87. * reset the chaining variables to the IV values.
  88. */
  89. public override void Reset()
  90. {
  91. base.Reset();
  92. H1 = 0x67452301;
  93. H2 = 0xefcdab89;
  94. H3 = 0x98badcfe;
  95. H4 = 0x10325476;
  96. xOff = 0;
  97. for (int i = 0; i != X.Length; i++)
  98. {
  99. X[i] = 0;
  100. }
  101. }
  102. //
  103. // round 1 left rotates
  104. //
  105. private static readonly int S11 = 7;
  106. private static readonly int S12 = 12;
  107. private static readonly int S13 = 17;
  108. private static readonly int S14 = 22;
  109. //
  110. // round 2 left rotates
  111. //
  112. private static readonly int S21 = 5;
  113. private static readonly int S22 = 9;
  114. private static readonly int S23 = 14;
  115. private static readonly int S24 = 20;
  116. //
  117. // round 3 left rotates
  118. //
  119. private static readonly int S31 = 4;
  120. private static readonly int S32 = 11;
  121. private static readonly int S33 = 16;
  122. private static readonly int S34 = 23;
  123. //
  124. // round 4 left rotates
  125. //
  126. private static readonly int S41 = 6;
  127. private static readonly int S42 = 10;
  128. private static readonly int S43 = 15;
  129. private static readonly int S44 = 21;
  130. /*
  131. * rotate int x left n bits.
  132. */
  133. private static uint RotateLeft(
  134. uint x,
  135. int n)
  136. {
  137. return (x << n) | (x >> (32 - n));
  138. }
  139. /*
  140. * F, G, H and I are the basic MD5 functions.
  141. */
  142. private static uint F(
  143. uint u,
  144. uint v,
  145. uint w)
  146. {
  147. return (u & v) | (~u & w);
  148. }
  149. private static uint G(
  150. uint u,
  151. uint v,
  152. uint w)
  153. {
  154. return (u & w) | (v & ~w);
  155. }
  156. private static uint H(
  157. uint u,
  158. uint v,
  159. uint w)
  160. {
  161. return u ^ v ^ w;
  162. }
  163. private static uint K(
  164. uint u,
  165. uint v,
  166. uint w)
  167. {
  168. return v ^ (u | ~w);
  169. }
  170. internal override void ProcessBlock()
  171. {
  172. uint a = H1;
  173. uint b = H2;
  174. uint c = H3;
  175. uint d = H4;
  176. //
  177. // Round 1 - F cycle, 16 times.
  178. //
  179. a = RotateLeft((a + F(b, c, d) + X[0] + 0xd76aa478), S11) + b;
  180. d = RotateLeft((d + F(a, b, c) + X[1] + 0xe8c7b756), S12) + a;
  181. c = RotateLeft((c + F(d, a, b) + X[2] + 0x242070db), S13) + d;
  182. b = RotateLeft((b + F(c, d, a) + X[3] + 0xc1bdceee), S14) + c;
  183. a = RotateLeft((a + F(b, c, d) + X[4] + 0xf57c0faf), S11) + b;
  184. d = RotateLeft((d + F(a, b, c) + X[5] + 0x4787c62a), S12) + a;
  185. c = RotateLeft((c + F(d, a, b) + X[6] + 0xa8304613), S13) + d;
  186. b = RotateLeft((b + F(c, d, a) + X[7] + 0xfd469501), S14) + c;
  187. a = RotateLeft((a + F(b, c, d) + X[8] + 0x698098d8), S11) + b;
  188. d = RotateLeft((d + F(a, b, c) + X[9] + 0x8b44f7af), S12) + a;
  189. c = RotateLeft((c + F(d, a, b) + X[10] + 0xffff5bb1), S13) + d;
  190. b = RotateLeft((b + F(c, d, a) + X[11] + 0x895cd7be), S14) + c;
  191. a = RotateLeft((a + F(b, c, d) + X[12] + 0x6b901122), S11) + b;
  192. d = RotateLeft((d + F(a, b, c) + X[13] + 0xfd987193), S12) + a;
  193. c = RotateLeft((c + F(d, a, b) + X[14] + 0xa679438e), S13) + d;
  194. b = RotateLeft((b + F(c, d, a) + X[15] + 0x49b40821), S14) + c;
  195. //
  196. // Round 2 - G cycle, 16 times.
  197. //
  198. a = RotateLeft((a + G(b, c, d) + X[1] + 0xf61e2562), S21) + b;
  199. d = RotateLeft((d + G(a, b, c) + X[6] + 0xc040b340), S22) + a;
  200. c = RotateLeft((c + G(d, a, b) + X[11] + 0x265e5a51), S23) + d;
  201. b = RotateLeft((b + G(c, d, a) + X[0] + 0xe9b6c7aa), S24) + c;
  202. a = RotateLeft((a + G(b, c, d) + X[5] + 0xd62f105d), S21) + b;
  203. d = RotateLeft((d + G(a, b, c) + X[10] + 0x02441453), S22) + a;
  204. c = RotateLeft((c + G(d, a, b) + X[15] + 0xd8a1e681), S23) + d;
  205. b = RotateLeft((b + G(c, d, a) + X[4] + 0xe7d3fbc8), S24) + c;
  206. a = RotateLeft((a + G(b, c, d) + X[9] + 0x21e1cde6), S21) + b;
  207. d = RotateLeft((d + G(a, b, c) + X[14] + 0xc33707d6), S22) + a;
  208. c = RotateLeft((c + G(d, a, b) + X[3] + 0xf4d50d87), S23) + d;
  209. b = RotateLeft((b + G(c, d, a) + X[8] + 0x455a14ed), S24) + c;
  210. a = RotateLeft((a + G(b, c, d) + X[13] + 0xa9e3e905), S21) + b;
  211. d = RotateLeft((d + G(a, b, c) + X[2] + 0xfcefa3f8), S22) + a;
  212. c = RotateLeft((c + G(d, a, b) + X[7] + 0x676f02d9), S23) + d;
  213. b = RotateLeft((b + G(c, d, a) + X[12] + 0x8d2a4c8a), S24) + c;
  214. //
  215. // Round 3 - H cycle, 16 times.
  216. //
  217. a = RotateLeft((a + H(b, c, d) + X[5] + 0xfffa3942), S31) + b;
  218. d = RotateLeft((d + H(a, b, c) + X[8] + 0x8771f681), S32) + a;
  219. c = RotateLeft((c + H(d, a, b) + X[11] + 0x6d9d6122), S33) + d;
  220. b = RotateLeft((b + H(c, d, a) + X[14] + 0xfde5380c), S34) + c;
  221. a = RotateLeft((a + H(b, c, d) + X[1] + 0xa4beea44), S31) + b;
  222. d = RotateLeft((d + H(a, b, c) + X[4] + 0x4bdecfa9), S32) + a;
  223. c = RotateLeft((c + H(d, a, b) + X[7] + 0xf6bb4b60), S33) + d;
  224. b = RotateLeft((b + H(c, d, a) + X[10] + 0xbebfbc70), S34) + c;
  225. a = RotateLeft((a + H(b, c, d) + X[13] + 0x289b7ec6), S31) + b;
  226. d = RotateLeft((d + H(a, b, c) + X[0] + 0xeaa127fa), S32) + a;
  227. c = RotateLeft((c + H(d, a, b) + X[3] + 0xd4ef3085), S33) + d;
  228. b = RotateLeft((b + H(c, d, a) + X[6] + 0x04881d05), S34) + c;
  229. a = RotateLeft((a + H(b, c, d) + X[9] + 0xd9d4d039), S31) + b;
  230. d = RotateLeft((d + H(a, b, c) + X[12] + 0xe6db99e5), S32) + a;
  231. c = RotateLeft((c + H(d, a, b) + X[15] + 0x1fa27cf8), S33) + d;
  232. b = RotateLeft((b + H(c, d, a) + X[2] + 0xc4ac5665), S34) + c;
  233. //
  234. // Round 4 - K cycle, 16 times.
  235. //
  236. a = RotateLeft((a + K(b, c, d) + X[0] + 0xf4292244), S41) + b;
  237. d = RotateLeft((d + K(a, b, c) + X[7] + 0x432aff97), S42) + a;
  238. c = RotateLeft((c + K(d, a, b) + X[14] + 0xab9423a7), S43) + d;
  239. b = RotateLeft((b + K(c, d, a) + X[5] + 0xfc93a039), S44) + c;
  240. a = RotateLeft((a + K(b, c, d) + X[12] + 0x655b59c3), S41) + b;
  241. d = RotateLeft((d + K(a, b, c) + X[3] + 0x8f0ccc92), S42) + a;
  242. c = RotateLeft((c + K(d, a, b) + X[10] + 0xffeff47d), S43) + d;
  243. b = RotateLeft((b + K(c, d, a) + X[1] + 0x85845dd1), S44) + c;
  244. a = RotateLeft((a + K(b, c, d) + X[8] + 0x6fa87e4f), S41) + b;
  245. d = RotateLeft((d + K(a, b, c) + X[15] + 0xfe2ce6e0), S42) + a;
  246. c = RotateLeft((c + K(d, a, b) + X[6] + 0xa3014314), S43) + d;
  247. b = RotateLeft((b + K(c, d, a) + X[13] + 0x4e0811a1), S44) + c;
  248. a = RotateLeft((a + K(b, c, d) + X[4] + 0xf7537e82), S41) + b;
  249. d = RotateLeft((d + K(a, b, c) + X[11] + 0xbd3af235), S42) + a;
  250. c = RotateLeft((c + K(d, a, b) + X[2] + 0x2ad7d2bb), S43) + d;
  251. b = RotateLeft((b + K(c, d, a) + X[9] + 0xeb86d391), S44) + c;
  252. H1 += a;
  253. H2 += b;
  254. H3 += c;
  255. H4 += d;
  256. xOff = 0;
  257. }
  258. public override IMemoable Copy()
  259. {
  260. return new MD5Digest(this);
  261. }
  262. public override void Reset(IMemoable other)
  263. {
  264. MD5Digest d = (MD5Digest)other;
  265. CopyIn(d);
  266. }
  267. }
  268. }
  269. #endif