MD5Digest.cs 11 KB

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