| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Security.Cryptography;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
- {
- public class CryptoApiEntropySourceProvider
- : IEntropySourceProvider
- {
- private readonly RandomNumberGenerator mRng;
- private readonly bool mPredictionResistant;
- public CryptoApiEntropySourceProvider()
- : this(RandomNumberGenerator.Create(), true)
- {
- }
- public CryptoApiEntropySourceProvider(RandomNumberGenerator rng, bool isPredictionResistant)
- {
- if (rng == null)
- throw new ArgumentNullException("rng");
- mRng = rng;
- mPredictionResistant = isPredictionResistant;
- }
- public IEntropySource Get(int bitsRequired)
- {
- return new CryptoApiEntropySource(mRng, mPredictionResistant, bitsRequired);
- }
- private class CryptoApiEntropySource
- : IEntropySource
- {
- private readonly RandomNumberGenerator mRng;
- private readonly bool mPredictionResistant;
- private readonly int mEntropySize;
- internal CryptoApiEntropySource(RandomNumberGenerator rng, bool predictionResistant, int entropySize)
- {
- this.mRng = rng;
- this.mPredictionResistant = predictionResistant;
- this.mEntropySize = entropySize;
- }
- #region IEntropySource Members
- bool IEntropySource.IsPredictionResistant
- {
- get { return mPredictionResistant; }
- }
- byte[] IEntropySource.GetEntropy()
- {
- byte[] result = new byte[(mEntropySize + 7) / 8];
- mRng.GetBytes(result);
- return result;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- int IEntropySource.GetEntropy(Span<byte> output)
- {
- int length = (mEntropySize + 7) / 8;
- mRng.GetBytes(output[..length]);
- return length;
- }
- #endif
- int IEntropySource.EntropySize
- {
- get { return mEntropySize; }
- }
- #endregion
- }
- }
- }
- #pragma warning restore
- #endif
|