Exemplo n.º 1
0
 public static RNG GetRNG(this RNGType type)
 {
     return(type switch
     {
         RNGType.LCRNG => RNG.LCRNG,
         RNGType.XDRNG => RNG.XDRNG,
         RNGType.ARNG => RNG.ARNG,
         _ => throw new ArgumentException(nameof(type))
     });
Exemplo n.º 2
0
        public Randor(RNGType r, bool pooling)
        {
            _Pooling = pooling;

            _RNGType = r;
            _Random  = RandomImpl.Acquire(r);

            if (_Pooling)
            {
                _Working = new byte[BUFFER_SIZE];
                _Buffer  = new byte[BUFFER_SIZE];
                _Random.GetBytes(_Working);
                ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
            }
        }
Exemplo n.º 3
0
 public static LCRNG GetRNG(this RNGType type) => type switch
 {
Exemplo n.º 4
0
        public static IRandomImpl Acquire(RNGType r)
        {
            bool autodetect = (r == RNGType.AutoDetect);

            if (autodetect)
            {
                bool _unix    = false;
                int  platform = (int)Environment.OSVersion.Platform;
                if (platform == 4 || platform == 128)                 // MS 4, MONO 128
                {
                    _unix = true;
                }

                if (_unix)
                {
                    r = RNGType.CSPRandom;
                }
                else if (Environment.Is64BitProcess)
                {
                    r = RNGType.RDRand64;
                }
                else
                {
                    r = RNGType.RDRand32;
                }
            }

            if (r == RNGType.RDRand32)
            {
                if (!File.Exists("rdrand32.dll"))
                {
                    if (autodetect)
                    {
                        r = RNGType.CSPRandom;
                    }
                    else
                    {
                        throw new NotSupportedException("The selected hardware random number generator is not supported.");
                    }
                }
            }
            else if (r == RNGType.RDRand64)
            {
                if (!File.Exists("rdrand64.dll"))
                {
                    if (autodetect)
                    {
                        r = RNGType.CSPRandom;
                    }
                    else
                    {
                        throw new NotSupportedException("The selected hardware random number generator is not supported.");
                    }
                }
            }

            if (_Instances.ContainsKey(r))
            {
                return(_Instances[r]);
            }

            IRandomImpl i = null;

            switch (r)
            {
            case RNGType.CSPRandom: _Instances[r] = i = new CSPRandom(); break;

            case RNGType.RDRand32: _Instances[r] = i = new RDRand32(); break;

            case RNGType.RDRand64: _Instances[r] = i = new RDRand64(); break;

            default: throw new NotImplementedException("Unknown RNGType");
            }

            if (i is IHardwareRNG && !((IHardwareRNG)i).IsSupported())
            {
                if (autodetect)
                {
                    _Instances.Remove(r);
                    _Instances[RNGType.CSPRandom] = i = new CSPRandom();
                }
                else
                {
                    throw new NotSupportedException("The selected hardware random number generator is not supported.");
                }
            }

            return(i);
        }