Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / crypto / SecretKeyFactory.java
blobd543c57c7e2c1b6869f9f7774689d631810c8faf
1 /* SecretKeyFactory.java -- Factory for creating secret keys.
2 Copyright (C) 2004 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 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.crypto;
41 import gnu.java.security.Engine;
43 import java.lang.reflect.InvocationTargetException;
44 import java.security.InvalidKeyException;
45 import java.security.NoSuchAlgorithmException;
46 import java.security.NoSuchProviderException;
47 import java.security.Provider;
48 import java.security.Security;
49 import java.security.spec.InvalidKeySpecException;
50 import java.security.spec.KeySpec;
52 /**
53 * A secret key factory translates {@link SecretKey} objects to and from
54 * {@link java.security.spec.KeySpec} objects, and can translate between
55 * different vendors' representations of {@link SecretKey} objects (for
56 * security or semantics; whichever applies).
58 * @author Casey Marshall (csm@gnu.org)
59 * @since 1.4
60 * @see SecretKey
62 public class SecretKeyFactory
65 // Constants and fields.
66 // ------------------------------------------------------------------------
68 private static final String SERVICE = "SecretKeyFactory";
70 /** The underlying factory implementation. */
71 private SecretKeyFactorySpi skfSpi;
73 /** The provider of the implementation. */
74 private Provider provider;
76 /** The name of the algorithm. */
77 private String algorithm;
79 // Constructor.
80 // ------------------------------------------------------------------------
82 /**
83 * Create a new secret key factory.
85 * @param skfSpi The underlying factory implementation.
86 * @param provider The provider.
87 * @param algorithm The algorithm name.
89 protected SecretKeyFactory(SecretKeyFactorySpi skfSpi, Provider provider,
90 String algorithm)
92 this.skfSpi = skfSpi;
93 this.provider = provider;
94 this.algorithm = algorithm;
97 // Class methods.
98 // ------------------------------------------------------------------------
101 * Create a new secret key factory from the first appropriate
102 * instance.
104 * @param algorithm The algorithm name.
105 * @return The appropriate key factory, if found.
106 * @throws java.security.NoSuchAlgorithmException If no provider
107 * implements the specified algorithm.
109 public static final SecretKeyFactory getInstance(String algorithm)
110 throws NoSuchAlgorithmException
112 Provider[] provs = Security.getProviders();
113 for (int i = 0; i < provs.length; i++)
117 return getInstance(algorithm, provs[i]);
119 catch (NoSuchAlgorithmException nsae)
123 throw new NoSuchAlgorithmException(algorithm);
127 * Create a new secret key factory from the named provider.
129 * @param algorithm The algorithm name.
130 * @param provider The provider name.
131 * @return The appropriate key factory, if found.
132 * @throws java.security.NoSuchAlgorithmException If the named
133 * provider does not implement the algorithm.
134 * @throws java.security.NoSuchProviderException If the named provider
135 * does not exist.
137 public static final SecretKeyFactory getInstance(String algorithm,
138 String provider)
139 throws NoSuchAlgorithmException, NoSuchProviderException
141 Provider p = Security.getProvider(provider);
142 if (p == null)
144 throw new NoSuchProviderException(provider);
146 return getInstance(algorithm, p);
150 * Create a new secret key factory from the specified provider.
152 * @param algorithm The algorithm name.
153 * @param provider The provider.
154 * @return The appropriate key factory, if found.
155 * @throws java.security.NoSuchAlgorithmException If the provider
156 * does not implement the algorithm.
158 public static final SecretKeyFactory getInstance(String algorithm,
159 Provider provider)
160 throws NoSuchAlgorithmException
164 return new SecretKeyFactory((SecretKeyFactorySpi)
165 Engine.getInstance(SERVICE, algorithm, provider),
166 provider, algorithm);
168 catch (InvocationTargetException ite)
170 if (ite.getCause() == null)
171 throw new NoSuchAlgorithmException(algorithm);
172 if (ite.getCause() instanceof NoSuchAlgorithmException)
173 throw (NoSuchAlgorithmException) ite.getCause();
174 throw new NoSuchAlgorithmException(algorithm);
176 catch (ClassCastException cce)
178 throw new NoSuchAlgorithmException(algorithm);
182 // Instance methods.
183 // ------------------------------------------------------------------------
186 * Generate a secret key from a key specification, if possible.
188 * @param keySpec The key specification.
189 * @return The secret key.
190 * @throws java.security.InvalidKeySpecException If the key specification
191 * cannot be transformed into a secret key.
193 public final SecretKey generateSecret(KeySpec keySpec)
194 throws InvalidKeySpecException
196 return skfSpi.engineGenerateSecret(keySpec);
200 * Get the algorithm name.
202 * @return The algorithm name.
204 public final String getAlgorithm()
206 return algorithm;
210 * Get the key specification from a secret key.
212 * @param key The secret key.
213 * @param keySpec The target key specification class.
214 * @return The key specification.
215 * @throws java.security.spec.InvalidKeySpecException If the secret key cannot
216 * be transformed into the specified key specification.
218 public final KeySpec getKeySpec(SecretKey key, Class keySpec)
219 throws InvalidKeySpecException
221 return skfSpi.engineGetKeySpec(key, keySpec);
225 * Get the provider of this implementation.
227 * @return The provider.
229 public final Provider getProvider()
231 return provider;
235 * Translate a secret key into another form.
237 * @param key The key to translate.
238 * @return The translated key.
239 * @throws java.security.InvalidKeyException If the argument cannot be
240 * translated.
242 public final SecretKey translateKey(SecretKey key)
243 throws InvalidKeyException
245 return skfSpi.engineTranslateKey(key);