01: package org.bouncycastle.crypto.generators;
02:
03: import org.bouncycastle.crypto.params.DHParameters;
04:
05: import java.math.BigInteger;
06: import java.security.SecureRandom;
07:
08: public class DHParametersGenerator {
09: private int size;
10: private int certainty;
11: private SecureRandom random;
12:
13: private static final BigInteger TWO = BigInteger.valueOf(2);
14:
15: /**
16: * Initialise the parameters generator.
17: *
18: * @param size bit length for the prime p
19: * @param certainty level of certainty for the prime number tests
20: * @param random a source of randomness
21: */
22: public void init(int size, int certainty, SecureRandom random) {
23: this .size = size;
24: this .certainty = certainty;
25: this .random = random;
26: }
27:
28: /**
29: * which generates the p and g values from the given parameters,
30: * returning the DHParameters object.
31: * <p>
32: * Note: can take a while...
33: */
34: public DHParameters generateParameters() {
35: //
36: // find a safe prime p where p = 2*q + 1, where p and q are prime.
37: //
38: BigInteger[] safePrimes = DHParametersHelper
39: .generateSafePrimes(size, certainty, random);
40:
41: BigInteger p = safePrimes[0];
42: BigInteger q = safePrimes[1];
43: BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
44:
45: return new DHParameters(p, g, q, TWO, null);
46: }
47: }
|