DigestRandomGenerator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. namespace BestHTTP.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 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(
  25. IDigest digest)
  26. {
  27. this.digest = digest;
  28. this.seed = new byte[digest.GetDigestSize()];
  29. this.seedCounter = 1;
  30. this.state = new byte[digest.GetDigestSize()];
  31. this.stateCounter = 1;
  32. }
  33. public void AddSeedMaterial(
  34. byte[] inSeed)
  35. {
  36. lock (this)
  37. {
  38. DigestUpdate(inSeed);
  39. DigestUpdate(seed);
  40. DigestDoFinal(seed);
  41. }
  42. }
  43. public void AddSeedMaterial(
  44. long rSeed)
  45. {
  46. lock (this)
  47. {
  48. DigestAddCounter(rSeed);
  49. DigestUpdate(seed);
  50. DigestDoFinal(seed);
  51. }
  52. }
  53. public void NextBytes(
  54. byte[] bytes)
  55. {
  56. NextBytes(bytes, 0, bytes.Length);
  57. }
  58. public void NextBytes(
  59. byte[] bytes,
  60. int start,
  61. int len)
  62. {
  63. lock (this)
  64. {
  65. int stateOff = 0;
  66. GenerateState();
  67. int end = start + len;
  68. for (int i = start; i < end; ++i)
  69. {
  70. if (stateOff == state.Length)
  71. {
  72. GenerateState();
  73. stateOff = 0;
  74. }
  75. bytes[i] = state[stateOff++];
  76. }
  77. }
  78. }
  79. private void CycleSeed()
  80. {
  81. DigestUpdate(seed);
  82. DigestAddCounter(seedCounter++);
  83. DigestDoFinal(seed);
  84. }
  85. private void GenerateState()
  86. {
  87. DigestAddCounter(stateCounter++);
  88. DigestUpdate(state);
  89. DigestUpdate(seed);
  90. DigestDoFinal(state);
  91. if ((stateCounter % CYCLE_COUNT) == 0)
  92. {
  93. CycleSeed();
  94. }
  95. }
  96. private void DigestAddCounter(long seedVal)
  97. {
  98. byte[] bytes = new byte[8];
  99. Pack.UInt64_To_LE((ulong)seedVal, bytes);
  100. digest.BlockUpdate(bytes, 0, bytes.Length);
  101. }
  102. private void DigestUpdate(byte[] inSeed)
  103. {
  104. digest.BlockUpdate(inSeed, 0, inSeed.Length);
  105. }
  106. private void DigestDoFinal(byte[] result)
  107. {
  108. digest.DoFinal(result, 0);
  109. }
  110. }
  111. }
  112. #pragma warning restore
  113. #endif