* config/m32r/m32r.c (move_src_operand): Fix 32-bit int test.
[official-gcc.git] / libjava / java / security / KeyFactory.java
blob7bbc355391b12d29ad6516c724bd5e41a4f9afb9
1 /* KeyFactory.java --- Key Factory Class
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
27 package java.security;
28 import java.security.spec.KeySpec;
29 import java.security.spec.InvalidKeySpecException;
31 /**
32 Key factories are used to convert keys (opaque cryptographic
33 keys of type Key) into key specifications (transparent
34 representations of the underlying key material).
36 Key factories are bi-directional. They allow a key class
37 to be converted into a key specification (key material) and
38 back again.
40 For example DSA public keys can be specified as
41 DSAPublicKeySpec or X509EncodedKeySpec. The key factory
42 translate these key specifications.
44 @since JDK 1.2
45 @author Mark Benvenuto
47 public class KeyFactory
49 private KeyFactorySpi keyFacSpi;
50 private Provider provider;
51 private String algorithm;
53 /**
54 Constructs a new keyFactory with the specified parameters.
56 @param keyFacSpi Key Factory SPI to use
57 @param provider the provider of the Key Factory SPI
58 @param algorithm the name of the key algorithm for this key factory
60 protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
61 String algorithm)
63 this.keyFacSpi = keyFacSpi;
64 this.provider = provider;
65 this.algorithm = algorithm;
68 /**
69 Gets an instance of the KeyFactory class representing
70 the specified key factory. If the algorithm is not
71 found then, it throws NoSuchAlgorithmException.
73 @param algorithm the name of algorithm to choose
74 @return a KeyFactory repesenting the desired algorithm
76 @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
78 public static KeyFactory getInstance(String algorithm)
79 throws NoSuchAlgorithmException
81 Provider[] p = Security.getProviders();
83 for (int i = 0; i < p.length; i++)
85 String classname = p[i].getProperty("KeyFactory." + algorithm);
86 if (classname != null)
87 return getInstance(classname, algorithm, p[i]);
90 throw new NoSuchAlgorithmException(algorithm);
93 /**
94 Gets an instance of the KeyFactory class representing
95 the specified key factory from the specified provider.
96 If the algorithm is not found then, it throws
97 NoSuchAlgorithmException. If the provider is not found, then
98 it throws NoSuchProviderException.
100 @param algorithm the name of algorithm to choose
101 @param provider the name of the provider to find the algorithm in
102 @return a KeyFactory repesenting the desired algorithm
104 @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
105 @throws NoSuchProviderException if the provider is not found
107 public static KeyFactory getInstance(String algorithm, String provider)
108 throws NoSuchAlgorithmException, NoSuchProviderException
110 Provider p = Security.getProvider(provider);
111 if (p == null)
112 throw new NoSuchProviderException();
114 return getInstance(p.getProperty("KeyFactory." + algorithm),
115 algorithm, p);
118 private static KeyFactory getInstance(String classname,
119 String algorithm,
120 Provider provider)
121 throws NoSuchAlgorithmException
126 return new KeyFactory((KeyFactorySpi) Class.forName(classname).
127 newInstance(), provider, algorithm);
129 catch (ClassNotFoundException cnfe)
131 throw new NoSuchAlgorithmException("Class not found");
133 catch (InstantiationException ie)
135 throw new NoSuchAlgorithmException("Class instantiation failed");
137 catch (IllegalAccessException iae)
139 throw new NoSuchAlgorithmException("Illegal Access");
144 Gets the provider that the class is from.
146 @return the provider of this class
148 public final Provider getProvider()
150 return provider;
154 Returns the name of the algorithm used
156 @return A string with the name of the algorithm
158 public final String getAlgorithm()
160 return algorithm;
164 Generates a public key from the provided key specification.
166 @param keySpec key specification
168 @return the public key
170 @throws InvalidKeySpecException invalid key specification for
171 this key factory to produce a public key
173 public final PublicKey generatePublic(KeySpec keySpec) throws
174 InvalidKeySpecException
176 return keyFacSpi.engineGeneratePublic(keySpec);
180 Generates a private key from the provided key specification.
182 @param keySpec key specification
184 @return the private key
186 @throws InvalidKeySpecException invalid key specification for
187 this key factory to produce a private key
189 public final PrivateKey generatePrivate(KeySpec keySpec) throws
190 InvalidKeySpecException
192 return keyFacSpi.engineGeneratePrivate(keySpec);
196 Returns a key specification for the given key. keySpec
197 identifies the specification class to return the key
198 material in.
200 @param key the key
201 @param keySpec the specification class to return the
202 key material in.
204 @return the key specification in an instance of the requested
205 specification class
207 @throws InvalidKeySpecException the requested key specification
208 is inappropriate for this key or the key is
209 unrecognized.
211 public final KeySpec getKeySpec(Key key, Class keySpec)
212 throws InvalidKeySpecException
214 return keyFacSpi.engineGetKeySpec(key, keySpec);
218 Translates the key from an unknown or untrusted provider
219 into a key for this key factory.
221 @param the key from an unknown or untrusted provider
223 @return the translated key
225 @throws InvalidKeySpecException if the key cannot be
226 processed by this key factory
228 public final Key translateKey(Key key) throws InvalidKeyException
230 return keyFacSpi.engineTranslateKey(key);