EntropyUtilities.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
  6. {
  7. public abstract class EntropyUtilities
  8. {
  9. /**
  10. * Generate numBytes worth of entropy from the passed in entropy source.
  11. *
  12. * @param entropySource the entropy source to request the data from.
  13. * @param numBytes the number of bytes of entropy requested.
  14. * @return a byte array populated with the random data.
  15. */
  16. public static byte[] GenerateSeed(IEntropySource entropySource, int numBytes)
  17. {
  18. byte[] bytes = new byte[numBytes];
  19. int count = 0;
  20. while (count < numBytes)
  21. {
  22. byte[] entropy = entropySource.GetEntropy();
  23. int toCopy = System.Math.Min(bytes.Length, numBytes - count);
  24. Array.Copy(entropy, 0, bytes, count, toCopy);
  25. count += toCopy;
  26. }
  27. return bytes;
  28. }
  29. }
  30. }
  31. #pragma warning restore
  32. #endif