DigestRandomGenerator.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Digests;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Prng
  6. {
  7. /**
  8. * Random generation based on the digest with counter. Calling AddSeedMaterial will
  9. * always increase the entropy of the hash.
  10. * <p>
  11. * Internal access to the digest is synchronized so a single one of these can be shared.
  12. * </p>
  13. */
  14. public class DigestRandomGenerator
  15. : IRandomGenerator
  16. {
  17. private const long CYCLE_COUNT = 10;
  18. private long stateCounter;
  19. private long seedCounter;
  20. private IDigest digest;
  21. private byte[] state;
  22. private byte[] seed;
  23. public DigestRandomGenerator(
  24. 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(
  33. byte[] inSeed)
  34. {
  35. lock (this)
  36. {
  37. DigestUpdate(inSeed);
  38. DigestUpdate(seed);
  39. DigestDoFinal(seed);
  40. }
  41. }
  42. public void AddSeedMaterial(
  43. long rSeed)
  44. {
  45. lock (this)
  46. {
  47. DigestAddCounter(rSeed);
  48. DigestUpdate(seed);
  49. DigestDoFinal(seed);
  50. }
  51. }
  52. public void NextBytes(
  53. byte[] bytes)
  54. {
  55. NextBytes(bytes, 0, bytes.Length);
  56. }
  57. public void NextBytes(
  58. byte[] bytes,
  59. int start,
  60. int len)
  61. {
  62. lock (this)
  63. {
  64. int stateOff = 0;
  65. GenerateState();
  66. int end = start + len;
  67. for (int i = start; i < end; ++i)
  68. {
  69. if (stateOff == state.Length)
  70. {
  71. GenerateState();
  72. stateOff = 0;
  73. }
  74. bytes[i] = state[stateOff++];
  75. }
  76. }
  77. }
  78. private void CycleSeed()
  79. {
  80. DigestUpdate(seed);
  81. DigestAddCounter(seedCounter++);
  82. DigestDoFinal(seed);
  83. }
  84. private void GenerateState()
  85. {
  86. DigestAddCounter(stateCounter++);
  87. DigestUpdate(state);
  88. DigestUpdate(seed);
  89. DigestDoFinal(state);
  90. if ((stateCounter % CYCLE_COUNT) == 0)
  91. {
  92. CycleSeed();
  93. }
  94. }
  95. private void DigestAddCounter(long seedVal)
  96. {
  97. byte[] bytes = new byte[8];
  98. Pack.UInt64_To_LE((ulong)seedVal, bytes);
  99. digest.BlockUpdate(bytes, 0, bytes.Length);
  100. }
  101. private void DigestUpdate(byte[] inSeed)
  102. {
  103. digest.BlockUpdate(inSeed, 0, inSeed.Length);
  104. }
  105. private void DigestDoFinal(byte[] result)
  106. {
  107. digest.DoFinal(result, 0);
  108. }
  109. }
  110. }
  111. #endif