KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > AlgorithmParameterGenerator


1 /*
2  * @(#)AlgorithmParameterGenerator.java 1.40 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.security;
9
10 import java.security.spec.AlgorithmParameterSpec JavaDoc;
11
12 /**
13  * The <code>AlgorithmParameterGenerator</code> class is used to generate a
14  * set of
15  * parameters to be used with a certain algorithm. Parameter generators
16  * are constructed using the <code>getInstance</code> factory methods
17  * (static methods that return instances of a given class).
18  *
19  * <P>The object that will generate the parameters can be initialized
20  * in two different ways: in an algorithm-independent manner, or in an
21  * algorithm-specific manner:
22  *
23  * <ul>
24  * <li>The algorithm-independent approach uses the fact that all parameter
25  * generators share the concept of a "size" and a
26  * source of randomness. The measure of size is universally shared
27  * by all algorithm parameters, though it is interpreted differently
28  * for different algorithms. For example, in the case of parameters for
29  * the <i>DSA</i> algorithm, "size" corresponds to the size
30  * of the prime modulus (in bits).
31  * When using this approach, algorithm-specific parameter generation
32  * values - if any - default to some standard values, unless they can be
33  * derived from the specified size.<P>
34  *
35  * <li>The other approach initializes a parameter generator object
36  * using algorithm-specific semantics, which are represented by a set of
37  * algorithm-specific parameter generation values. To generate
38  * Diffie-Hellman system parameters, for example, the parameter generation
39  * values usually
40  * consist of the size of the prime modulus and the size of the
41  * random exponent, both specified in number of bits.
42  * </ul>
43  *
44  * <P>In case the client does not explicitly initialize the
45  * AlgorithmParameterGenerator
46  * (via a call to an <code>init</code> method), each provider must supply (and
47  * document) a default initialization. For example, the Sun provider uses a
48  * default modulus prime size of 1024 bits for the generation of DSA
49  * parameters.
50  *
51  * @author Jan Luehe
52  *
53  * @version 1.40, 12/19/03
54  *
55  * @see AlgorithmParameters
56  * @see java.security.spec.AlgorithmParameterSpec
57  *
58  * @since 1.2
59  */

60
61 public class AlgorithmParameterGenerator {
62
63     // The provider
64
private Provider JavaDoc provider;
65
66     // The provider implementation (delegate)
67
private AlgorithmParameterGeneratorSpi JavaDoc paramGenSpi;
68
69     // The algorithm
70
private String JavaDoc algorithm;
71
72     /**
73      * Creates an AlgorithmParameterGenerator object.
74      *
75      * @param paramGenSpi the delegate
76      * @param provider the provider
77      * @param algorithm the algorithm
78      */

79     protected AlgorithmParameterGenerator
80     (AlgorithmParameterGeneratorSpi JavaDoc paramGenSpi, Provider JavaDoc provider,
81      String JavaDoc algorithm) {
82     this.paramGenSpi = paramGenSpi;
83     this.provider = provider;
84     this.algorithm = algorithm;
85     }
86
87     /**
88      * Returns the standard name of the algorithm this parameter
89      * generator is associated with.
90      *
91      * @return the string name of the algorithm.
92      */

93     public final String JavaDoc getAlgorithm() {
94     return this.algorithm;
95     }
96
97     /**
98      * Generates an AlgorithmParameterGenerator object that implements the
99      * specified digest algorithm. If the default provider package
100      * provides an implementation of the requested digest algorithm,
101      * an instance of AlgorithmParameterGenerator containing that
102      * implementation
103      * is returned. If the algorithm is not available in the default
104      * package, other packages are searched.
105      *
106      * @param algorithm the string name of the algorithm this
107      * parameter generator is associated with.
108      *
109      * @return the new AlgorithmParameterGenerator object.
110      *
111      * @exception NoSuchAlgorithmException if the algorithm is
112      * not available in the environment.
113      */

114     public static AlgorithmParameterGenerator JavaDoc getInstance(String JavaDoc algorithm)
115     throws NoSuchAlgorithmException JavaDoc {
116         try {
117         Object JavaDoc[] objs = Security.getImpl(algorithm,
118                          "AlgorithmParameterGenerator",
119                          (String JavaDoc)null);
120         return new AlgorithmParameterGenerator JavaDoc
121             ((AlgorithmParameterGeneratorSpi JavaDoc)objs[0],
122              (Provider JavaDoc)objs[1],
123              algorithm);
124         } catch(NoSuchProviderException JavaDoc e) {
125         throw new NoSuchAlgorithmException JavaDoc(algorithm + " not found");
126         }
127     }
128
129     /**
130      * Generates an AlgorithmParameterGenerator object for the requested
131      * algorithm, as supplied from the specified provider,
132      * if such a parameter generator is available from the provider.
133      *
134      * @param algorithm the string name of the algorithm.
135      *
136      * @param provider the string name of the provider.
137      *
138      * @return the new AlgorithmParameterGenerator object.
139      *
140      * @exception NoSuchAlgorithmException if the algorithm is
141      * not available from the provider.
142      *
143      * @exception NoSuchProviderException if the provider is not
144      * available in the environment.
145      *
146      * @exception IllegalArgumentException if the provider name is null
147      * or empty.
148      *
149      * @see Provider
150      */

151     public static AlgorithmParameterGenerator JavaDoc getInstance(String JavaDoc algorithm,
152                               String JavaDoc provider)
153     throws NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc
154     {
155     if (provider == null || provider.length() == 0)
156         throw new IllegalArgumentException JavaDoc("missing provider");
157     Object JavaDoc[] objs = Security.getImpl(algorithm,
158                      "AlgorithmParameterGenerator",
159                      provider);
160     return new AlgorithmParameterGenerator JavaDoc
161         ((AlgorithmParameterGeneratorSpi JavaDoc)objs[0], (Provider JavaDoc)objs[1],
162          algorithm);
163     }
164
165     /**
166      * Generates an AlgorithmParameterGenerator object for the requested
167      * algorithm, as supplied from the specified provider,
168      * if such a parameter generator is available from the provider.
169      * Note: the <code>provider</code> doesn't have to be registered.
170      *
171      * @param algorithm the string name of the algorithm.
172      *
173      * @param provider the provider.
174      *
175      * @return the new AlgorithmParameterGenerator object.
176      *
177      * @exception NoSuchAlgorithmException if the algorithm is
178      * not available from the provider.
179      *
180      * @exception IllegalArgumentException if the <code>provider</code> is
181      * null.
182      *
183      * @see Provider
184      *
185      * @since 1.4
186      */

187     public static AlgorithmParameterGenerator JavaDoc getInstance(String JavaDoc algorithm,
188                               Provider JavaDoc provider)
189     throws NoSuchAlgorithmException JavaDoc
190     {
191     if (provider == null)
192         throw new IllegalArgumentException JavaDoc("missing provider");
193     Object JavaDoc[] objs = Security.getImpl(algorithm,
194                      "AlgorithmParameterGenerator",
195                      provider);
196     return new AlgorithmParameterGenerator JavaDoc
197         ((AlgorithmParameterGeneratorSpi JavaDoc)objs[0], (Provider JavaDoc)objs[1],
198          algorithm);
199     }
200
201     /**
202      * Returns the provider of this algorithm parameter generator object.
203      *
204      * @return the provider of this algorithm parameter generator object
205      */

206     public final Provider JavaDoc getProvider() {
207     return this.provider;
208     }
209
210     /**
211      * Initializes this parameter generator for a certain size.
212      * To create the parameters, the <code>SecureRandom</code>
213      * implementation of the highest-priority installed provider is used as
214      * the source of randomness.
215      * (If none of the installed providers supply an implementation of
216      * <code>SecureRandom</code>, a system-provided source of randomness is
217      * used.)
218      *
219      * @param size the size (number of bits).
220      */

221     public final void init(int size) {
222     paramGenSpi.engineInit(size, new SecureRandom JavaDoc());
223     }
224
225     /**
226      * Initializes this parameter generator for a certain size and source
227      * of randomness.
228      *
229      * @param size the size (number of bits).
230      * @param random the source of randomness.
231      */

232     public final void init(int size, SecureRandom JavaDoc random) {
233     paramGenSpi.engineInit(size, random);
234     }
235
236     /**
237      * Initializes this parameter generator with a set of algorithm-specific
238      * parameter generation values.
239      * To generate the parameters, the <code>SecureRandom</code>
240      * implementation of the highest-priority installed provider is used as
241      * the source of randomness.
242      * (If none of the installed providers supply an implementation of
243      * <code>SecureRandom</code>, a system-provided source of randomness is
244      * used.)
245      *
246      * @param genParamSpec the set of algorithm-specific parameter generation values.
247      *
248      * @exception InvalidAlgorithmParameterException if the given parameter
249      * generation values are inappropriate for this parameter generator.
250      */

251     public final void init(AlgorithmParameterSpec JavaDoc genParamSpec)
252     throws InvalidAlgorithmParameterException JavaDoc {
253         paramGenSpi.engineInit(genParamSpec, new SecureRandom JavaDoc());
254     }
255
256     /**
257      * Initializes this parameter generator with a set of algorithm-specific
258      * parameter generation values.
259      *
260      * @param genParamSpec the set of algorithm-specific parameter generation values.
261      * @param random the source of randomness.
262      *
263      * @exception InvalidAlgorithmParameterException if the given parameter
264      * generation values are inappropriate for this parameter generator.
265      */

266     public final void init(AlgorithmParameterSpec JavaDoc genParamSpec,
267                SecureRandom JavaDoc random)
268     throws InvalidAlgorithmParameterException JavaDoc {
269         paramGenSpi.engineInit(genParamSpec, random);
270     }
271
272     /**
273      * Generates the parameters.
274      *
275      * @return the new AlgorithmParameters object.
276      */

277     public final AlgorithmParameters JavaDoc generateParameters() {
278     return paramGenSpi.engineGenerateParameters();
279     }
280 }
281
Popular Tags