MD5Digest.cs 10 KB

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