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.InvalidKeySpecException;
21 import java.security.spec.KeySpec;
22 import org.apache.harmony.security.fortress.Engine;
25 * {@code KeyFactory} is an engine class that can be used to translate between
26 * public and private key objects and convert keys between their external
27 * representation, that can be easily transported and their internal
30 public class KeyFactory {
32 private static final String SERVICE = "KeyFactory";
35 private Provider provider;
38 // Used to access common engine functionality
39 static private Engine engine = new Engine(SERVICE);
41 // The SPI implementation.
42 private KeyFactorySpi spiImpl;
45 private String algorithm;
48 * Constructs a new instance of {@code KeyFactory} with the specified
52 * the concrete key factory service.
56 * the algorithm to use.
58 protected KeyFactory(KeyFactorySpi keyFacSpi,
61 this.provider = provider;
62 this. algorithm = algorithm;
63 this.spiImpl = keyFacSpi;
67 * Returns a new instance of {@code KeyFactory} that utilizes the specified
71 * the name of the algorithm.
72 * @return a new instance of {@code KeyFactory} that utilizes the specified
74 * @throws NoSuchAlgorithmException
75 * if no provider provides the requested algorithm.
77 public static KeyFactory getInstance(String algorithm)
78 throws NoSuchAlgorithmException {
79 if (algorithm == null) {
80 throw new NullPointerException();
82 synchronized (engine) {
83 engine.getInstance(algorithm, null);
84 return new KeyFactory((KeyFactorySpi)engine.spi, engine.provider, algorithm);
89 * Returns a new instance of {@code KeyFactory} that utilizes the specified
90 * algorithm from the specified provider.
93 * the name of the algorithm.
95 * the name of the provider.
96 * @return a new instance of {@code KeyFactory} that utilizes the specified
97 * algorithm from the specified provider.
98 * @throws NoSuchAlgorithmException
99 * if the provider does not provide the requested algorithm.
100 * @throws NoSuchProviderException
101 * if the requested provider is not available.
102 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
104 @SuppressWarnings("nls")
105 public static KeyFactory getInstance(String algorithm, String provider)
106 throws NoSuchAlgorithmException, NoSuchProviderException {
107 if (provider == null || provider.isEmpty()) {
108 throw new IllegalArgumentException();
110 Provider p = Security.getProvider(provider);
112 throw new NoSuchProviderException(provider);
114 return getInstance(algorithm, p);
118 * Returns a new instance of {@code KeyFactory} that utilizes the specified
119 * algorithm from the specified provider.
122 * the name of the algorithm.
124 * the security provider.
125 * @return a new instance of {@code KeyFactory} that utilizes the specified
126 * algorithm from the specified provider.
127 * @throws NoSuchAlgorithmException
128 * if the provider does not provide the requested algorithm.
129 * @throws IllegalArgumentException if {@code provider == null}
131 public static KeyFactory getInstance(String algorithm, Provider provider)
132 throws NoSuchAlgorithmException {
133 if (provider == null) {
134 throw new IllegalArgumentException();
136 if (algorithm == null) {
137 throw new NullPointerException();
139 synchronized (engine) {
140 engine.getInstance(algorithm, provider, null);
141 return new KeyFactory((KeyFactorySpi)engine.spi, provider, algorithm);
146 * Returns the provider associated with this {@code KeyFactory}.
148 * @return the provider associated with this {@code KeyFactory}.
150 public final Provider getProvider() {
155 * Returns the name of the algorithm associated with this {@code
158 * @return the name of the algorithm associated with this {@code
161 public final String getAlgorithm() {
166 * Generates a instance of {@code PublicKey} from the given key
170 * the specification of the public key
171 * @return the public key
172 * @throws InvalidKeySpecException
173 * if the specified {@code keySpec} is invalid
175 public final PublicKey generatePublic(KeySpec keySpec)
176 throws InvalidKeySpecException {
177 return spiImpl.engineGeneratePublic(keySpec);
181 * Generates a instance of {@code PrivateKey} from the given key
185 * the specification of the private key.
186 * @return the private key.
187 * @throws InvalidKeySpecException
188 * if the specified {@code keySpec} is invalid.
190 public final PrivateKey generatePrivate(KeySpec keySpec)
191 throws InvalidKeySpecException {
192 return spiImpl.engineGeneratePrivate(keySpec);
196 * Returns the key specification for the specified key.
199 * the key from which the specification is requested.
201 * the type of the requested {@code KeySpec}.
202 * @return the key specification for the specified key.
203 * @throws InvalidKeySpecException
204 * if the key can not be processed, or the requested requested
205 * {@code KeySpec} is inappropriate for the given key.
207 public final <T extends KeySpec> T getKeySpec(Key key,
209 throws InvalidKeySpecException {
210 return spiImpl.engineGetKeySpec(key, keySpec);
214 * Translates the given key into a key from this key factory.
217 * the key to translate.
218 * @return the translated key.
219 * @throws InvalidKeyException
220 * if the specified key can not be translated by this key
223 public final Key translateKey(Key key)
224 throws InvalidKeyException {
225 return spiImpl.engineTranslateKey(key);