GeneralDigest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * base implementation of MD4 family style digest as outlined in
  9. * "Handbook of Applied Cryptography", pages 344 - 347.
  10. */
  11. public abstract class GeneralDigest
  12. : IDigest, IMemoable
  13. {
  14. private const int BYTE_LENGTH = 64;
  15. private byte[] xBuf;
  16. private int xBufOff;
  17. private long byteCount;
  18. internal GeneralDigest()
  19. {
  20. xBuf = new byte[4];
  21. }
  22. internal GeneralDigest(GeneralDigest t)
  23. {
  24. xBuf = new byte[t.xBuf.Length];
  25. CopyIn(t);
  26. }
  27. protected void CopyIn(GeneralDigest t)
  28. {
  29. Array.Copy(t.xBuf, 0, xBuf, 0, t.xBuf.Length);
  30. xBufOff = t.xBufOff;
  31. byteCount = t.byteCount;
  32. }
  33. public void Update(byte input)
  34. {
  35. xBuf[xBufOff++] = input;
  36. if (xBufOff == xBuf.Length)
  37. {
  38. ProcessWord(xBuf, 0);
  39. xBufOff = 0;
  40. }
  41. byteCount++;
  42. }
  43. public void BlockUpdate(
  44. byte[] input,
  45. int inOff,
  46. int length)
  47. {
  48. length = System.Math.Max(0, length);
  49. //
  50. // fill the current word
  51. //
  52. int i = 0;
  53. if (xBufOff != 0)
  54. {
  55. while (i < length)
  56. {
  57. xBuf[xBufOff++] = input[inOff + i++];
  58. if (xBufOff == 4)
  59. {
  60. ProcessWord(xBuf, 0);
  61. xBufOff = 0;
  62. break;
  63. }
  64. }
  65. }
  66. //
  67. // process whole words.
  68. //
  69. int limit = length - 3;
  70. for (; i < limit; i += 4)
  71. {
  72. ProcessWord(input, inOff + i);
  73. }
  74. //
  75. // load in the remainder.
  76. //
  77. while (i < length)
  78. {
  79. xBuf[xBufOff++] = input[inOff + i++];
  80. }
  81. byteCount += length;
  82. }
  83. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
  84. public void BlockUpdate(ReadOnlySpan<byte> input)
  85. {
  86. int length = input.Length;
  87. //
  88. // fill the current word
  89. //
  90. int i = 0;
  91. if (xBufOff != 0)
  92. {
  93. while (i < length)
  94. {
  95. xBuf[xBufOff++] = input[i++];
  96. if (xBufOff == 4)
  97. {
  98. ProcessWord(xBuf, 0);
  99. xBufOff = 0;
  100. break;
  101. }
  102. }
  103. }
  104. //
  105. // process whole words.
  106. //
  107. int limit = length - 3;
  108. for (; i < limit; i += 4)
  109. {
  110. ProcessWord(input.Slice(i, 4));
  111. }
  112. //
  113. // load in the remainder.
  114. //
  115. while (i < length)
  116. {
  117. xBuf[xBufOff++] = input[i++];
  118. }
  119. byteCount += length;
  120. }
  121. #endif
  122. public void Finish()
  123. {
  124. long bitLength = (byteCount << 3);
  125. //
  126. // add the pad bytes.
  127. //
  128. Update((byte)128);
  129. while (xBufOff != 0) Update((byte)0);
  130. ProcessLength(bitLength);
  131. ProcessBlock();
  132. }
  133. public virtual void Reset()
  134. {
  135. byteCount = 0;
  136. xBufOff = 0;
  137. Array.Clear(xBuf, 0, xBuf.Length);
  138. }
  139. public int GetByteLength()
  140. {
  141. return BYTE_LENGTH;
  142. }
  143. internal abstract void ProcessWord(byte[] input, int inOff);
  144. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
  145. internal abstract void ProcessWord(ReadOnlySpan<byte> word);
  146. #endif
  147. internal abstract void ProcessLength(long bitLength);
  148. internal abstract void ProcessBlock();
  149. public abstract string AlgorithmName { get; }
  150. public abstract int GetDigestSize();
  151. public abstract int DoFinal(byte[] output, int outOff);
  152. public abstract IMemoable Copy();
  153. public abstract void Reset(IMemoable t);
  154. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
  155. public abstract int DoFinal(Span<byte> output);
  156. #endif
  157. }
  158. }
  159. #pragma warning restore
  160. #endif