Exemplo n.º 1
0
        private void SecRandTest()
        {
            using (SecureRandom rnd = new SecureRandom())
            {
                double x1 = 0.0;
                for (int i = 0; i < 1000; i++)
                {
                    x1 = rnd.NextDouble();
                    if (x1 > 1.0)
                        throw new Exception("SecureRandom: NextDouble returned a value outside of the expected range.");
                }

                short x2 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x2 = rnd.NextInt16(1, 6);
                    if (x2 > 6)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                    if (x2 < 1)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                }

                ushort x3 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x3 = rnd.NextUInt16(1, 52);
                    if (x3 > 52)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                    if (x3 < 1)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                }

                int x4 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x4 = rnd.NextInt32(3371, 16777216);
                    if (x4 > 16777216)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                    if (x4 < 3371)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                }

                uint x5 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x5 = rnd.NextUInt32(77721, 777216);
                    if (x5 > 777216)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                    if (x5 < 77721)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                }

                long x6 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x6 = rnd.NextInt64(2814749767, 281474976710653);
                    if (x6 > 281474976710656)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                    if (x6 < 2814749767)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                }

                ulong x7 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x7 = rnd.NextUInt64(5759403792, 72057594037927934);
                    if (x7 > 72057594037927936)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                    if (x7 < 5759403792)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// A random number is generated until a probable prime number is found
        /// </summary>
        internal static BigInteger ConsBigInteger(int BitLength, int Certainty, SecureRandom Rnd)
        {
            // PRE: bitLength >= 2;
            // For small numbers get a random prime from the prime table
            if (BitLength <= 10)
            {
                int[] rp = _offsetPrimes[BitLength];
                return _biPrimes[rp[0] + Rnd.NextInt32(rp[1])];
            }
            int shiftCount = (-BitLength) & 31;
            int last = (BitLength + 31) >> 5;
            BigInteger n = new BigInteger(1, last, new int[last]);

            last--;
            do
            {// To fill the array with random integers
                for (int i = 0; i < n._numberLength; i++)
                    n._digits[i] = Rnd.Next();
                
                // To fix to the correct bitLength
                // n.digits[last] |= 0x80000000;
                n._digits[last] |= Int32.MinValue;
                n._digits[last] = IntUtils.URShift(n._digits[last], shiftCount);
                // To create an odd number
                n._digits[0] |= 1;
            } while (!IsProbablePrime(n, Certainty));

            return n;
        }