GeneralDigest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 - i) & ~3) + i;
  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. public void Finish()
  84. {
  85. long bitLength = (byteCount << 3);
  86. //
  87. // add the pad bytes.
  88. //
  89. Update((byte)128);
  90. while (xBufOff != 0) Update((byte)0);
  91. ProcessLength(bitLength);
  92. ProcessBlock();
  93. }
  94. public virtual void Reset()
  95. {
  96. byteCount = 0;
  97. xBufOff = 0;
  98. Array.Clear(xBuf, 0, xBuf.Length);
  99. }
  100. public int GetByteLength()
  101. {
  102. return BYTE_LENGTH;
  103. }
  104. internal abstract void ProcessWord(byte[] input, int inOff);
  105. internal abstract void ProcessLength(long bitLength);
  106. internal abstract void ProcessBlock();
  107. public abstract string AlgorithmName { get; }
  108. public abstract int GetDigestSize();
  109. public abstract int DoFinal(byte[] output, int outOff);
  110. public abstract IMemoable Copy();
  111. public abstract void Reset(IMemoable t);
  112. }
  113. }
  114. #pragma warning restore
  115. #endif