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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package java.security;
20 import java.security.spec.AlgorithmParameterSpec;
21 import org.apache.harmony.security.fortress.Engine;
24 * {@code AlgorithmParameterGenerator} is an engine class which is capable of
25 * generating parameters for the algorithm it was initialized with.
27 public class AlgorithmParameterGenerator {
29 // Store spi service name
30 private static final String SERVICE = "AlgorithmParameterGenerator";
32 // Used to access common engine functionality
33 private static Engine engine = new Engine(SERVICE);
36 private static SecureRandom randm = new SecureRandom();
38 // Store used provider
39 private final Provider provider;
41 // Store used AlgorithmParameterGeneratorSpi implementation
42 private final AlgorithmParameterGeneratorSpi spiImpl;
44 //Store used algorithm
45 private final String algorithm;
48 * Constructs a new instance of {@code AlgorithmParameterGenerator} with the
52 * a concrete implementation, this engine instance delegates to.
56 * the name of the algorithm.
58 protected AlgorithmParameterGenerator(
59 AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,
61 this.provider = provider;
62 this.algorithm = algorithm;
63 this.spiImpl = paramGenSpi;
67 * Returns the name of the algorithm.
69 * @return the name of the algorithm.
71 public final String getAlgorithm() {
76 * Returns a new instance of {@code AlgorithmParameterGenerator} for the
77 * specified 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}.
88 public static AlgorithmParameterGenerator getInstance(String algorithm)
89 throws NoSuchAlgorithmException {
90 if (algorithm == null) {
91 throw new NullPointerException();
93 synchronized (engine) {
94 engine.getInstance(algorithm, null);
95 return new AlgorithmParameterGenerator(
96 (AlgorithmParameterGeneratorSpi) engine.spi, engine.provider,
102 * Returns a new instance of {@code AlgorithmParameterGenerator} from the
103 * specified provider for the specified algorithm.
106 * the name of the algorithm to use.
108 * name of the provider of the {@code
109 * AlgorithmParameterGenerator}.
110 * @return a new instance of {@code AlgorithmParameterGenerator} for the
111 * specified algorithm.
112 * @throws NoSuchAlgorithmException
113 * if the specified algorithm is not available.
114 * @throws NoSuchProviderException
115 * if the specified provider is not available.
116 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
117 * @throws NullPointerException
118 * if {@code algorithm} is {@code null}.
120 public static AlgorithmParameterGenerator getInstance(String algorithm,
121 String provider) throws NoSuchAlgorithmException,
122 NoSuchProviderException {
123 if (provider == null || provider.isEmpty()) {
124 throw new IllegalArgumentException();
126 Provider impProvider = Security.getProvider(provider);
127 if (impProvider == null) {
128 throw new NoSuchProviderException(provider);
130 return getInstance(algorithm, impProvider);
134 * Returns a new instance of {@code AlgorithmParameterGenerator} from the
135 * specified provider for the specified algorithm.
138 * the name of the algorithm to use.
140 * the provider of the {@code AlgorithmParameterGenerator}.
141 * @return a new instance of {@code AlgorithmParameterGenerator} for the
142 * specified algorithm.
143 * @throws NoSuchAlgorithmException
144 * if the specified algorithm is not available.
145 * @throws NullPointerException
146 * if {@code algorithm} is {@code null}.
147 * @throws IllegalArgumentException if {@code provider == null}
149 public static AlgorithmParameterGenerator getInstance(String algorithm,
150 Provider provider) throws NoSuchAlgorithmException {
151 if (provider == null) {
152 throw new IllegalArgumentException();
154 if (algorithm == null) {
155 throw new NullPointerException();
157 synchronized (engine) {
158 engine.getInstance(algorithm, provider, null);
159 return new AlgorithmParameterGenerator(
160 (AlgorithmParameterGeneratorSpi) engine.spi, provider,
166 * Returns the provider associated with this {@code
167 * AlgorithmParameterGenerator}.
169 * @return the provider associated with this {@code
170 * AlgorithmParameterGenerator}.
172 public final Provider getProvider() {
177 * Initializes this {@code AlgorithmParameterGenerator} with the given size.
178 * The default parameter set and a default {@code SecureRandom} instance
182 * the size (in number of bits).
184 public final void init(int size) {
185 spiImpl.engineInit(size, randm);
189 * Initializes this {@code AlgorithmParameterGenerator} with the given size
190 * and the given {@code SecureRandom}. The default parameter set will be
194 * the size (in number of bits).
196 * the source of randomness.
198 public final void init(int size, SecureRandom random) {
199 spiImpl.engineInit(size, random);
203 * Initializes this {@code AlgorithmParameterGenerator} with the given {@code
204 * AlgorithmParameterSpec}. A default {@code SecureRandom} instance will be
207 * @param genParamSpec
208 * the parameters to use.
209 * @throws InvalidAlgorithmParameterException
210 * if the specified parameters are not supported.
212 public final void init(AlgorithmParameterSpec genParamSpec)
213 throws InvalidAlgorithmParameterException {
214 spiImpl.engineInit(genParamSpec, randm);
218 * Initializes this {@code AlgorithmParameterGenerator} with the given
219 * {@code AlgorithmParameterSpec} and the given {@code SecureRandom}.
221 * @param genParamSpec
222 * the parameters to use.
224 * the source of randomness.
225 * @throws InvalidAlgorithmParameterException
226 * if the specified parameters are not supported.
228 public final void init(AlgorithmParameterSpec genParamSpec,
229 SecureRandom random) throws InvalidAlgorithmParameterException {
230 spiImpl.engineInit(genParamSpec, random);
234 * Computes and returns {@code AlgorithmParameters} for this generator's
237 * @return {@code AlgorithmParameters} for this generator's algorithm.
239 public final AlgorithmParameters generateParameters() {
240 return spiImpl.engineGenerateParameters();