package meerkat.crypto.utils;

import java.math.BigInteger;
import java.util.Random;

/**
 * Created by Tzlil on 3/28/2016.
 */
public class GenerateRandomPrime {
    private final static int Certainty = 10000;
    public final static BigInteger SafePrime100Bits = new BigInteger("146407324427772525685319783363");

    public static BigInteger generateRandomPrime(int bits, Random random) {
        BigInteger p;
        do {
            p = new BigInteger(bits, random);
        } while (!p.isProbablePrime(Certainty));
        return p;
    }

    public static BigInteger generateRandomSafePrime(int bits, Random random) {
        BigInteger p;
        BigInteger q;
        do {
            p = generateRandomPrime(bits, random);
            q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
        } while (!q.isProbablePrime(Certainty));
        return q;
    }
}
