2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libjava / classpath / javax / crypto / SecretKeyFactory.java
blob9749a8285fcaffec9c9185e59d5ac059f226dacc
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 /**
98 * Create a new secret key factory from the first appropriate instance.
100 * @param algorithm The algorithm name.
101 * @return The appropriate key factory, if found.
102 * @throws NoSuchAlgorithmException If no provider implements the specified
103 * algorithm.
104 * @throws IllegalArgumentException if <code>algorithm</code> is
105 * <code>null</code> or is an empty string.
107 public static final SecretKeyFactory getInstance(String algorithm)
108 throws NoSuchAlgorithmException
110 Provider[] p = Security.getProviders();
111 NoSuchAlgorithmException lastException = null;
112 for (int i = 0; i < p.length; i++)
115 return getInstance(algorithm, p[i]);
117 catch (NoSuchAlgorithmException x)
119 lastException = x;
121 if (lastException != null)
122 throw lastException;
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 NoSuchAlgorithmException If the named provider does not implement
133 * the algorithm.
134 * @throws NoSuchProviderException If the named provider does not exist.
135 * @throws IllegalArgumentException if either <code>algorithm</code> or
136 * <code>provider</code> is <code>null</code>, or if
137 * <code>algorithm</code> is an empty string.
139 public static final SecretKeyFactory getInstance(String algorithm,
140 String provider)
141 throws NoSuchAlgorithmException, NoSuchProviderException
143 if (provider == null)
144 throw new IllegalArgumentException("provider MUST NOT be null");
145 Provider p = Security.getProvider(provider);
146 if (p == null)
147 throw new NoSuchProviderException(provider);
148 return getInstance(algorithm, p);
152 * Create a new secret key factory from the specified provider.
154 * @param algorithm The algorithm name.
155 * @param provider The provider.
156 * @return The appropriate key factory, if found.
157 * @throws NoSuchAlgorithmException If the provider does not implement the
158 * algorithm.
159 * @throws IllegalArgumentException if either <code>algorithm</code> or
160 * <code>provider</code> is <code>null</code>, or if
161 * <code>algorithm</code> is an empty string.
163 public static final SecretKeyFactory getInstance(String algorithm,
164 Provider provider)
165 throws NoSuchAlgorithmException
167 StringBuilder sb = new StringBuilder("SecretKeyFactory algorithm [")
168 .append(algorithm).append("] from provider[")
169 .append(provider).append("] could not be created");
170 Throwable cause;
173 Object spi = Engine.getInstance(SERVICE, algorithm, provider);
174 return new SecretKeyFactory((SecretKeyFactorySpi) spi, provider, algorithm);
176 catch (InvocationTargetException x)
178 cause = x.getCause();
179 if (cause instanceof NoSuchAlgorithmException)
180 throw (NoSuchAlgorithmException) cause;
181 if (cause == null)
182 cause = x;
184 catch (ClassCastException x)
186 cause = x;
188 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
189 x.initCause(cause);
190 throw x;
194 * Generate a secret key from a key specification, if possible.
196 * @param keySpec The key specification.
197 * @return The secret key.
198 * @throws java.security.InvalidKeySpecException If the key specification
199 * cannot be transformed into a secret key.
201 public final SecretKey generateSecret(KeySpec keySpec)
202 throws InvalidKeySpecException
204 return skfSpi.engineGenerateSecret(keySpec);
208 * Get the algorithm name.
210 * @return The algorithm name.
212 public final String getAlgorithm()
214 return algorithm;
218 * Get the key specification from a secret key.
220 * @param key The secret key.
221 * @param keySpec The target key specification class.
222 * @return The key specification.
223 * @throws java.security.spec.InvalidKeySpecException If the secret key cannot
224 * be transformed into the specified key specification.
226 public final KeySpec getKeySpec(SecretKey key, Class keySpec)
227 throws InvalidKeySpecException
229 return skfSpi.engineGetKeySpec(key, keySpec);
233 * Get the provider of this implementation.
235 * @return The provider.
237 public final Provider getProvider()
239 return provider;
243 * Translate a secret key into another form.
245 * @param key The key to translate.
246 * @return The translated key.
247 * @throws java.security.InvalidKeyException If the argument cannot be
248 * translated.
250 public final SecretKey translateKey(SecretKey key)
251 throws InvalidKeyException
253 return skfSpi.engineTranslateKey(key);