/// <summary> /// Shuffle an array using the SecureRandom class /// </summary> /// /// <typeparam name="T">Type of array</typeparam> /// <param name="Source">The list instance</param> public static void Shuffle <T>(this T[] Source) { using (SecureRandom rnd = new SecureRandom()) { if (ParallelUtils.IsParallel) { Parallel.For(0, Source.Length, i => { int index = rnd.NextInt32(0, Source.Length - 1); T temp = Source[i]; Source[i] = Source[index]; lock (temp) Source[index] = temp; }); } else { for (int i = 0; i < Source.Length; i++) { int index = rnd.NextInt32(0, Source.Length - 1); T temp = Source[i]; Source[i] = Source[index]; Source[index] = temp; } } } }
/// <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 = m_offsetPrimes[BitLength]; return(m_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.m_numberLength; i++) { n.m_digits[i] = Rnd.Next(); } // To fix to the correct bitLength // n.digits[last] |= 0x80000000; n.m_digits[last] |= int.MinValue; n.m_digits[last] = IntUtils.URShift(n.m_digits[last], shiftCount); // To create an odd number n.m_digits[0] |= 1; } while (!IsProbablePrime(n, Certainty)); return(n); }
public void IsProbablePrimeI() { int fails = 0; bi = new BigInteger(20, 20, rand); if (!bi.IsProbablePrime(17)) { fails++; } bi = BigInteger.Parse("4", 10); if (bi.IsProbablePrime(17)) { throw new Exception("IsProbablePrime failed for: " + bi.ToString()); } bi = BigInteger.ValueOf(17L * 13L); if (bi.IsProbablePrime(17)) { throw new Exception("IsProbablePrime failed for: " + bi.ToString()); } for (long a = 2; a < 1000; a++) { if (isPrime(a)) { IsTrue(BigInteger.ValueOf(a).IsProbablePrime(5), "false negative on prime number <1000"); } else if (BigInteger.ValueOf(a).IsProbablePrime(17)) { fails++; } } for (int a = 0; a < 1000; a++) { bi = BigInteger.ValueOf(rand.NextInt32(1000000)).Multiply(BigInteger.ValueOf(rand.NextInt32(1000000))); if (bi.IsProbablePrime(17)) { fails++; } } for (int a = 0; a < 200; a++) { bi = new BigInteger(70, rand).Multiply(new BigInteger(70, rand)); if (bi.IsProbablePrime(17)) { fails++; } } IsTrue(fails <= 1, "Too many false positives - may indicate a problem"); }
/// <summary> /// Shuffle an array using the SecureRandom class /// </summary> /// /// <typeparam name="T">Type of array</typeparam> /// <param name="Source">The list instance</param> public static void Shuffle <T>(this T[] Source) { using (SecureRandom rnd = new SecureRandom()) { for (int i = 0; i < Source.Length - 1; i++) { int index = (int)rnd.NextInt32(i, Source.Length - 1); if (i != index) { T temp = Source[i]; Source[i] = Source[index]; Source[index] = temp; } } } }
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."); } } } }