ThreadedSeedGenerator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Threading;
  5. #if NO_THREADS || NETFX_CORE
  6. using System.Threading.Tasks;
  7. #endif
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
  10. {
  11. /**
  12. * A thread based seed generator - one source of randomness.
  13. * <p>
  14. * Based on an idea from Marcus Lippert.
  15. * </p>
  16. */
  17. public class ThreadedSeedGenerator
  18. {
  19. private class SeedGenerator
  20. {
  21. #if NETCF_1_0
  22. // No volatile keyword, but all fields implicitly volatile anyway
  23. private int counter = 0;
  24. private bool stop = false;
  25. #else
  26. private volatile int counter = 0;
  27. private volatile bool stop = false;
  28. #endif
  29. private void Run(object ignored)
  30. {
  31. while (!this.stop)
  32. {
  33. this.counter++;
  34. }
  35. }
  36. public byte[] GenerateSeed(
  37. int numBytes,
  38. bool fast)
  39. {
  40. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  41. return DoGenerateSeed(numBytes, fast);
  42. #else
  43. ThreadPriority originalPriority = Thread.CurrentThread.Priority;
  44. try
  45. {
  46. Thread.CurrentThread.Priority = ThreadPriority.Normal;
  47. return DoGenerateSeed(numBytes, fast);
  48. }
  49. finally
  50. {
  51. Thread.CurrentThread.Priority = originalPriority;
  52. }
  53. #endif
  54. }
  55. private byte[] DoGenerateSeed(
  56. int numBytes,
  57. bool fast)
  58. {
  59. this.counter = 0;
  60. this.stop = false;
  61. byte[] result = new byte[numBytes];
  62. int last = 0;
  63. int end = fast ? numBytes : numBytes * 8;
  64. #if NO_THREADS || NETFX_CORE
  65. Task.Factory.StartNew(() => Run(null), TaskCreationOptions.None);
  66. #else
  67. ThreadPool.QueueUserWorkItem(new WaitCallback(Run));
  68. #endif
  69. #if PORTABLE || NETFX_CORE
  70. AutoResetEvent autoResetEvent = new AutoResetEvent(false);
  71. #endif
  72. try
  73. {
  74. for (int i = 0; i < end; i++)
  75. {
  76. while (this.counter == last)
  77. {
  78. try
  79. {
  80. #if PORTABLE || NETFX_CORE
  81. autoResetEvent.WaitOne(1);
  82. #else
  83. Thread.Sleep(1);
  84. #endif
  85. }
  86. catch (Exception)
  87. {
  88. // ignore
  89. }
  90. }
  91. last = this.counter;
  92. if (fast)
  93. {
  94. result[i] = (byte)last;
  95. }
  96. else
  97. {
  98. int bytepos = i / 8;
  99. result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
  100. }
  101. }
  102. }
  103. finally
  104. {
  105. #if PORTABLE || NETFX_CORE
  106. autoResetEvent.Dispose();
  107. #endif
  108. }
  109. this.stop = true;
  110. return result;
  111. }
  112. }
  113. /**
  114. * Generate seed bytes. Set fast to false for best quality.
  115. * <p>
  116. * If fast is set to true, the code should be round about 8 times faster when
  117. * generating a long sequence of random bytes. 20 bytes of random values using
  118. * the fast mode take less than half a second on a Nokia e70. If fast is set to false,
  119. * it takes round about 2500 ms.
  120. * </p>
  121. * @param numBytes the number of bytes to generate
  122. * @param fast true if fast mode should be used
  123. */
  124. public byte[] GenerateSeed(
  125. int numBytes,
  126. bool fast)
  127. {
  128. return new SeedGenerator().GenerateSeed(numBytes, fast);
  129. }
  130. }
  131. }
  132. #pragma warning restore
  133. #endif