DigestRandomGenerator.cs 2.6 KB

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