Org.BouncyCastle.Math.BigInteger.IsProbablePrime C# (CSharp) Method

IsProbablePrime() public method

public IsProbablePrime ( int certainty ) : bool
certainty int
return bool
		public bool IsProbablePrime(
			int certainty)
		{
			if (certainty <= 0)
				return true;

			BigInteger n = Abs();

			if (!n.TestBit(0))
				return n.Equals(Two);

			if (n.Equals(One))
				return false;

			return n.CheckProbablePrime(certainty, RandomSource);
		}

Usage Example

        /// <summary>Choose a random prime value for use with RSA</summary>
        /// <param name="bitlength">the bit-length of the returned prime</param>
        /// <param name="e">the RSA public exponent</param>
        /// <returns>a prime p, with (p-1) relatively prime to e</returns>
        protected virtual BigInteger ChooseRandomPrime(int bitlength, BigInteger e)
        {
            for (;;)
            {
                BigInteger p = new BigInteger(bitlength, 1, param.Random);

                if (p.Mod(e).Equals(BigInteger.One))
                    continue;

                if (!p.IsProbablePrime(param.Certainty))
                    continue;

                if (!e.Gcd(p.Subtract(BigInteger.One)).Equals(BigInteger.One))
                    continue;

                return p;
            }
        }
All Usage Examples Of Org.BouncyCastle.Math.BigInteger::IsProbablePrime