AlgorithmParameterGenerator.java revision cff1616012dc0d56c2da9af2b9b1183e76c7e044
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.security;
19
20import java.security.spec.AlgorithmParameterSpec;
21import org.apache.harmony.security.fortress.Engine;
22
23/**
24 * {@code AlgorithmParameterGenerator} is an engine class which is capable of
25 * generating parameters for the algorithm it was initialized with.
26 */
27public class AlgorithmParameterGenerator {
28
29    // Store spi service name
30    private static final String SERVICE = "AlgorithmParameterGenerator";
31
32    // Used to access common engine functionality
33    private static final Engine ENGINE = new Engine(SERVICE);
34
35    // Store SecureRandom
36    private static final SecureRandom RANDOM = new SecureRandom();
37
38    // Store used provider
39    private final Provider provider;
40
41    // Store used AlgorithmParameterGeneratorSpi implementation
42    private final AlgorithmParameterGeneratorSpi spiImpl;
43
44    //Store used algorithm
45    private final String algorithm;
46
47    /**
48     * Constructs a new instance of {@code AlgorithmParameterGenerator} with the
49     * given arguments.
50     *
51     * @param paramGenSpi
52     *            a concrete implementation, this engine instance delegates to.
53     * @param provider
54     *            the provider.
55     * @param algorithm
56     *            the name of the algorithm.
57     */
58    protected AlgorithmParameterGenerator(
59            AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,
60            String algorithm) {
61        this.provider = provider;
62        this.algorithm = algorithm;
63        this.spiImpl = paramGenSpi;
64    }
65
66    /**
67     * Returns the name of the algorithm.
68     *
69     * @return the name of the algorithm.
70     */
71    public final String getAlgorithm() {
72        return algorithm;
73    }
74
75    /**
76     * Returns a new instance of {@code AlgorithmParameterGenerator} for the
77     * specified algorithm.
78     *
79     * @param algorithm
80     *            the name of the algorithm to use.
81     * @return a new instance of {@code AlgorithmParameterGenerator} for the
82     *         specified algorithm.
83     * @throws NoSuchAlgorithmException
84     *             if the specified algorithm is not available.
85     * @throws NullPointerException
86     *             if {@code algorithm} is {@code null}.
87     */
88    public static AlgorithmParameterGenerator getInstance(String algorithm)
89            throws NoSuchAlgorithmException {
90        if (algorithm == null) {
91            throw new NullPointerException("algorithm == null");
92        }
93        Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
94        return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) sap.spi,
95                                               sap.provider, algorithm);
96    }
97
98    /**
99     * Returns a new instance of {@code AlgorithmParameterGenerator} from the
100     * specified provider for the specified algorithm.
101     *
102     * @param algorithm
103     *            the name of the algorithm to use.
104     * @param provider
105     *            name of the provider of the {@code
106     *            AlgorithmParameterGenerator}.
107     * @return a new instance of {@code AlgorithmParameterGenerator} for the
108     *         specified algorithm.
109     * @throws NoSuchAlgorithmException
110     *             if the specified algorithm is not available.
111     * @throws NoSuchProviderException
112     *             if the specified provider is not available.
113     * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
114     * @throws NullPointerException
115     *             if {@code algorithm} is {@code null}.
116     */
117    public static AlgorithmParameterGenerator getInstance(String algorithm,
118            String provider) throws NoSuchAlgorithmException,
119            NoSuchProviderException {
120        if (provider == null || provider.isEmpty()) {
121            throw new IllegalArgumentException();
122        }
123        Provider impProvider = Security.getProvider(provider);
124        if (impProvider == null) {
125            throw new NoSuchProviderException(provider);
126        }
127        return getInstance(algorithm, impProvider);
128    }
129
130    /**
131     * Returns a new instance of {@code AlgorithmParameterGenerator} from the
132     * specified provider for the specified algorithm.
133     *
134     * @param algorithm
135     *            the name of the algorithm to use.
136     * @param provider
137     *            the provider of the {@code AlgorithmParameterGenerator}.
138     * @return a new instance of {@code AlgorithmParameterGenerator} for the
139     *         specified algorithm.
140     * @throws NoSuchAlgorithmException
141     *             if the specified algorithm is not available.
142     * @throws NullPointerException
143     *             if {@code algorithm} is {@code null}.
144     * @throws IllegalArgumentException if {@code provider == null}
145     */
146    public static AlgorithmParameterGenerator getInstance(String algorithm,
147            Provider provider) throws NoSuchAlgorithmException {
148        if (provider == null) {
149            throw new IllegalArgumentException("provider == null");
150        }
151        if (algorithm == null) {
152            throw new NullPointerException("algorithm == null");
153        }
154        Object spi = ENGINE.getInstance(algorithm, provider, null);
155        return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) spi, provider,
156                                               algorithm);
157    }
158
159    /**
160     * Returns the provider associated with this {@code
161     * AlgorithmParameterGenerator}.
162     *
163     * @return the provider associated with this {@code
164     *         AlgorithmParameterGenerator}.
165     */
166    public final Provider getProvider() {
167        return provider;
168    }
169
170    /**
171     * Initializes this {@code AlgorithmParameterGenerator} with the given size.
172     * The default parameter set and a default {@code SecureRandom} instance
173     * will be used.
174     *
175     * @param size
176     *            the size (in number of bits).
177     */
178    public final void init(int size) {
179        spiImpl.engineInit(size, RANDOM);
180    }
181
182    /**
183     * Initializes this {@code AlgorithmParameterGenerator} with the given size
184     * and the given {@code SecureRandom}. The default parameter set will be
185     * used.
186     *
187     * @param size
188     *            the size (in number of bits).
189     * @param random
190     *            the source of randomness.
191     */
192    public final void init(int size, SecureRandom random) {
193        spiImpl.engineInit(size, random);
194    }
195
196    /**
197     * Initializes this {@code AlgorithmParameterGenerator} with the given {@code
198     * AlgorithmParameterSpec}. A default {@code SecureRandom} instance will be
199     * used.
200     *
201     * @param genParamSpec
202     *            the parameters to use.
203     * @throws InvalidAlgorithmParameterException
204     *             if the specified parameters are not supported.
205     */
206    public final void init(AlgorithmParameterSpec genParamSpec)
207            throws InvalidAlgorithmParameterException {
208        spiImpl.engineInit(genParamSpec, RANDOM);
209    }
210
211    /**
212     * Initializes this {@code AlgorithmParameterGenerator} with the given
213     * {@code AlgorithmParameterSpec} and the given {@code SecureRandom}.
214     *
215     * @param genParamSpec
216     *            the parameters to use.
217     * @param random
218     *            the source of randomness.
219     * @throws InvalidAlgorithmParameterException
220     *             if the specified parameters are not supported.
221     */
222    public final void init(AlgorithmParameterSpec genParamSpec,
223            SecureRandom random) throws InvalidAlgorithmParameterException {
224        spiImpl.engineInit(genParamSpec, random);
225    }
226
227    /**
228     * Computes and returns {@code AlgorithmParameters} for this generator's
229     * algorithm.
230     *
231     * @return {@code AlgorithmParameters} for this generator's algorithm.
232     */
233    public final AlgorithmParameters generateParameters() {
234        return spiImpl.engineGenerateParameters();
235    }
236}
237