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;
25 * {@code KeyPairGenerator} is an engine class which is capable of generating a
26 * private key and its related public key utilizing the algorithm it was
29 * @see KeyPairGeneratorSpi
31 public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
33 // Store KeyPairGenerator SERVICE name
34 private static final String SERVICE = "KeyPairGenerator";
36 // Used to access common engine functionality
37 private static Engine engine = new Engine(SERVICE);
40 private static SecureRandom random = new SecureRandom();
42 // Store used provider
43 private Provider provider;
45 // Store used algorithm
46 private String algorithm;
49 * Constructs a new instance of {@code KeyPairGenerator} with the name of
50 * the algorithm to use.
53 * the name of algorithm to use
55 protected KeyPairGenerator(String algorithm) {
56 this.algorithm = algorithm;
60 * Returns the name of the algorithm of this {@code KeyPairGenerator}.
62 * @return the name of the algorithm of this {@code KeyPairGenerator}
64 public String getAlgorithm() {
69 * Returns a new instance of {@code KeyPairGenerator} that utilizes the
70 * specified algorithm.
73 * the name of the algorithm to use
74 * @return a new instance of {@code KeyPairGenerator} that utilizes the
76 * @throws NoSuchAlgorithmException if the specified algorithm is not available
77 * @throws NullPointerException
78 * if {@code algorithm} is {@code null}
80 public static KeyPairGenerator getInstance(String algorithm)
81 throws NoSuchAlgorithmException {
82 if (algorithm == null) {
83 throw new NullPointerException();
85 KeyPairGenerator result;
86 synchronized (engine) {
87 engine.getInstance(algorithm, null);
88 if (engine.spi instanceof KeyPairGenerator) {
89 result = (KeyPairGenerator) engine.spi;
90 result.algorithm = algorithm;
91 result.provider = engine.provider;
94 result = new KeyPairGeneratorImpl((KeyPairGeneratorSpi) engine.spi,
95 engine.provider, algorithm);
101 * Returns a new instance of {@code KeyPairGenerator} that utilizes the
102 * specified algorithm from the specified provider.
105 * the name of the algorithm to use
107 * the name of the provider
108 * @return a new instance of {@code KeyPairGenerator} that utilizes the
109 * specified algorithm from the specified provider
110 * @throws NoSuchAlgorithmException if the specified algorithm is not available
111 * @throws NoSuchProviderException if the specified provider is not available
112 * @throws NullPointerException
113 * if {@code algorithm} is {@code null}
114 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
116 public static KeyPairGenerator getInstance(String algorithm, String provider)
117 throws NoSuchAlgorithmException, NoSuchProviderException {
118 if (provider == null || provider.isEmpty()) {
119 throw new IllegalArgumentException();
121 Provider impProvider = Security.getProvider(provider);
122 if (impProvider == null) {
123 throw new NoSuchProviderException(provider);
125 return getInstance(algorithm, impProvider);
129 * Returns a new instance of {@code KeyPairGenerator} that utilizes the
130 * specified algorithm from the specified provider.
133 * the name of the algorithm to use
136 * @return a new instance of {@code KeyPairGenerator} that utilizes the
137 * specified algorithm from the specified provider
138 * @throws NoSuchAlgorithmException if the specified algorithm is not available
139 * @throws NullPointerException
140 * if {@code algorithm} is {@code null}
141 * @throws IllegalArgumentException if {@code provider == null}
143 public static KeyPairGenerator getInstance(String algorithm,
144 Provider provider) throws NoSuchAlgorithmException {
145 if (provider == null) {
146 throw new IllegalArgumentException();
148 if (algorithm == null) {
149 throw new NullPointerException();
151 KeyPairGenerator result;
152 synchronized (engine) {
153 engine.getInstance(algorithm, provider, null);
154 if (engine.spi instanceof KeyPairGenerator) {
155 result = (KeyPairGenerator) engine.spi;
156 result.algorithm = algorithm;
157 result.provider = provider;
160 result = new KeyPairGeneratorImpl((KeyPairGeneratorSpi) engine.spi,
161 provider, algorithm);
167 * Returns the provider associated with this {@code KeyPairGenerator}.
169 * @return the provider associated with this {@code KeyPairGenerator}
171 public final Provider getProvider() {
176 * Initializes this {@code KeyPairGenerator} with the given key size. The
177 * default parameter set and a default {@code SecureRandom} instance will be
181 * the size of the key (number of bits)
183 public void initialize(int keysize) {
184 initialize(keysize, random);
188 * Initializes this {@code KeyPairGenerator} with the given {@code
189 * AlgorithmParameterSpec}. A default {@code SecureRandom} instance will be
193 * the parameters to use
194 * @throws InvalidAlgorithmParameterException
195 * if the specified parameters are not supported
197 public void initialize(AlgorithmParameterSpec param)
198 throws InvalidAlgorithmParameterException {
199 initialize(param, random);
203 * Computes and returns a new unique {@code KeyPair} each time this method
206 * This does exactly the same as {@link #generateKeyPair()}.
208 * @return a new unique {@code KeyPair} each time this method is called
210 public final KeyPair genKeyPair() {
211 return generateKeyPair();
215 * Computes and returns a new unique {@code KeyPair} each time this method
218 * This does exactly the same as {@link #genKeyPair()}.
220 * @return a new unique {@code KeyPair} each time this method is called
223 public KeyPair generateKeyPair() {
228 * Initializes this {@code KeyPairGenerator} with the given key size and the
229 * given {@code SecureRandom}. The default parameter set will be used.
234 * the source of randomness
237 public void initialize(int keysize, SecureRandom random) {
241 * Initializes this {@code KeyPairGenerator} with the given {@code
242 * AlgorithmParameterSpec} and the given {@code SecureRandom}.
245 * the parameters to use
247 * the source of randomness
248 * @throws InvalidAlgorithmParameterException
249 * if the specified parameters are not supported
252 public void initialize(AlgorithmParameterSpec param, SecureRandom random)
253 throws InvalidAlgorithmParameterException {
258 * Internal class: KeyPairGenerator implementation
261 private static class KeyPairGeneratorImpl extends KeyPairGenerator {
262 // Save KeyPairGeneratorSpi
263 private KeyPairGeneratorSpi spiImpl;
265 // Implementation of KeyPaiGenerator constructor
267 // @param KeyPairGeneratorSpi
270 private KeyPairGeneratorImpl(KeyPairGeneratorSpi keyPairGeneratorSpi,
271 Provider provider, String algorithm) {
273 super.provider = provider;
274 spiImpl = keyPairGeneratorSpi;
277 // implementation of initialize(int keysize, SecureRandom random)
278 // using corresponding spi initialize() method
280 public void initialize(int keysize, SecureRandom random) {
281 spiImpl.initialize(keysize, random);
284 // implementation of generateKeyPair()
285 // using corresponding spi generateKeyPair() method
287 public KeyPair generateKeyPair() {
288 return spiImpl.generateKeyPair();
291 // implementation of initialize(int keysize, SecureRandom random)
292 // using corresponding spi initialize() method
294 public void initialize(AlgorithmParameterSpec param, SecureRandom random)
295 throws InvalidAlgorithmParameterException {
296 spiImpl.initialize(param, random);