DigestRandomGenerator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.Prng
  7. {
  8. /**
  9. * Random generation based on the digest with counter. Calling AddSeedMaterial will
  10. * always increase the entropy of the hash.
  11. * <p>
  12. * Internal access to the digest is synchronized so a single one of these can be shared.
  13. * </p>
  14. */
  15. public sealed class DigestRandomGenerator
  16. : IRandomGenerator
  17. {
  18. private const long CYCLE_COUNT = 10;
  19. private long stateCounter;
  20. private long seedCounter;
  21. private IDigest digest;
  22. private byte[] state;
  23. private byte[] seed;
  24. public DigestRandomGenerator(IDigest digest)
  25. {
  26. this.digest = digest;
  27. this.seed = new byte[digest.GetDigestSize()];
  28. this.seedCounter = 1;
  29. this.state = new byte[digest.GetDigestSize()];
  30. this.stateCounter = 1;
  31. }
  32. public void AddSeedMaterial(byte[] inSeed)
  33. {
  34. lock (this)
  35. {
  36. if (!Arrays.IsNullOrEmpty(inSeed))
  37. {
  38. DigestUpdate(inSeed);
  39. }
  40. DigestUpdate(seed);
  41. DigestDoFinal(seed);
  42. }
  43. }
  44. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  45. public void AddSeedMaterial(ReadOnlySpan<byte> inSeed)
  46. {
  47. lock (this)
  48. {
  49. if (!inSeed.IsEmpty)
  50. {
  51. DigestUpdate(inSeed);
  52. }
  53. DigestUpdate(seed);
  54. DigestDoFinal(seed);
  55. }
  56. }
  57. #endif
  58. public void AddSeedMaterial(long rSeed)
  59. {
  60. lock (this)
  61. {
  62. DigestAddCounter(rSeed);
  63. DigestUpdate(seed);
  64. DigestDoFinal(seed);
  65. }
  66. }
  67. public void NextBytes(byte[] bytes)
  68. {
  69. NextBytes(bytes, 0, bytes.Length);
  70. }
  71. public void NextBytes(byte[] bytes, int start, int len)
  72. {
  73. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  74. NextBytes(bytes.AsSpan(start, len));
  75. #else
  76. lock (this)
  77. {
  78. int stateOff = 0;
  79. GenerateState();
  80. int end = start + len;
  81. for (int i = start; i < end; ++i)
  82. {
  83. if (stateOff == state.Length)
  84. {
  85. GenerateState();
  86. stateOff = 0;
  87. }
  88. bytes[i] = state[stateOff++];
  89. }
  90. }
  91. #endif
  92. }
  93. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  94. public void NextBytes(Span<byte> bytes)
  95. {
  96. lock (this)
  97. {
  98. int stateOff = 0;
  99. GenerateState();
  100. for (int i = 0; i < bytes.Length; ++i)
  101. {
  102. if (stateOff == state.Length)
  103. {
  104. GenerateState();
  105. stateOff = 0;
  106. }
  107. bytes[i] = state[stateOff++];
  108. }
  109. }
  110. }
  111. #endif
  112. private void CycleSeed()
  113. {
  114. DigestUpdate(seed);
  115. DigestAddCounter(seedCounter++);
  116. DigestDoFinal(seed);
  117. }
  118. private void GenerateState()
  119. {
  120. DigestAddCounter(stateCounter++);
  121. DigestUpdate(state);
  122. DigestUpdate(seed);
  123. DigestDoFinal(state);
  124. if ((stateCounter % CYCLE_COUNT) == 0)
  125. {
  126. CycleSeed();
  127. }
  128. }
  129. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  130. private void DigestAddCounter(long seedVal)
  131. {
  132. Span<byte> bytes = stackalloc byte[8];
  133. Pack.UInt64_To_LE((ulong)seedVal, bytes);
  134. digest.BlockUpdate(bytes);
  135. }
  136. private void DigestUpdate(ReadOnlySpan<byte> inSeed)
  137. {
  138. digest.BlockUpdate(inSeed);
  139. }
  140. private void DigestDoFinal(Span<byte> result)
  141. {
  142. digest.DoFinal(result);
  143. }
  144. #else
  145. private void DigestAddCounter(long seedVal)
  146. {
  147. byte[] bytes = new byte[8];
  148. Pack.UInt64_To_LE((ulong)seedVal, bytes);
  149. digest.BlockUpdate(bytes, 0, bytes.Length);
  150. }
  151. private void DigestUpdate(byte[] inSeed)
  152. {
  153. digest.BlockUpdate(inSeed, 0, inSeed.Length);
  154. }
  155. private void DigestDoFinal(byte[] result)
  156. {
  157. digest.DoFinal(result, 0);
  158. }
  159. #endif
  160. }
  161. }
  162. #pragma warning restore
  163. #endif