View Javadoc
1   package civitas.crypto.parameters;
2   
3   import org.springframework.beans.factory.annotation.Autowired;
4   import org.springframework.stereotype.Controller;
5   
6   import civitas.crypto.Constants;
7   import civitas.crypto.CryptoBase;
8   import civitas.crypto.messagedigest.CryptoHash;
9   import civitas.util.CivitasBigInteger;
10  
11  @Controller
12  public class GenerateSchnorrPrime implements Constants {
13  	@Autowired
14  	CryptoHash cryptoHash;
15  
16  	@Autowired
17  	CalculateNumberOfPrimeTests calculateNumberOfPrimeTests;
18  
19  	@Autowired
20  	CryptoBase cryptoBase;
21  
22  	public PrimePair apply(final int qLength, final int pLength) {
23  		CivitasBigInteger p;
24  		CivitasBigInteger q;
25  
26  		final int numPTests = calculateNumberOfPrimeTests.apply(pLength);
27  		CivitasBigInteger l = TWO.pow(pLength);
28  		boolean done = false;
29  		do {
30  			q = cryptoBase.obtainProbablePrime(qLength);
31  			int nP = 0;
32  			do {
33  				nP++;
34  				p = cryptoBase.generateRandomElement(l);
35  				p.add(l);
36  				CivitasBigInteger m = p.mod(q.multiply(TWO));
37  				p = p.subtract(m).add(ONE);
38  				if (p.bitLength() == pLength && p.isProbablePrime(CERTAINTY)) {
39  					done = true;
40  				}
41  
42  			} while (!done && nP < numPTests);
43  		} while (!done);
44  		return new PrimePair(p, q);
45  	}
46  }