FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / security / AlgorithmParameters.java
blobafc457e4a3ce8e0e3277e21eb23184fa88b4eccc
1 /* AlgorithmParameters.java --- Algorithm Parameters Implementation 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 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 java.security;
40 import java.security.spec.InvalidParameterSpecException;
41 import java.security.spec.AlgorithmParameterSpec;
42 import java.io.IOException;
44 /**
45 AlgorithmParameters is the Algorithm Parameters class which
46 provides an interface through which to modify parameters for
47 classes. This class is used to manage the algorithm parameters.
49 @since JDK 1.2
50 @author Mark Benvenuto
52 public class AlgorithmParameters
54 private AlgorithmParametersSpi paramSpi;
55 private Provider provider;
56 private String algorithm;
58 /**
59 Creates an instance of AlgorithmParameters
61 @param paramSpi A parameters engine to use
62 @param provider A provider to use
63 @param algorithm The algorithm
65 protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
66 Provider provider, String algorithm)
68 this.paramSpi = paramSpi;
69 this.provider = provider;
70 this.algorithm = algorithm;
73 /**
74 Returns the name of the algorithm used
76 @return A string with the name of the algorithm
78 public final String getAlgorithm()
80 return algorithm;
83 /**
84 Gets an instance of the AlgorithmParameters class representing
85 the specified algorithm parameters. If the algorithm is not
86 found then, it throws NoSuchAlgorithmException.
88 The returned AlgorithmParameters must still be intialized with
89 init().
91 @param algorithm the name of algorithm to choose
92 @return a AlgorithmParameters repesenting the desired algorithm
94 @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
96 public static AlgorithmParameters getInstance(String algorithm) throws
97 NoSuchAlgorithmException
99 Provider[] p = Security.getProviders();
101 for (int i = 0; i < p.length; i++)
103 String classname =
104 p[i].getProperty("AlgorithmParameters." + algorithm);
105 if (classname != null)
106 return getInstance(classname, algorithm, p[i]);
109 throw new NoSuchAlgorithmException(algorithm);
112 /**
113 Gets an instance of the AlgorithmParameters class representing
114 the specified algorithm parameters from the specified provider.
115 If the algorithm is not found then, it throws
116 NoSuchAlgorithmException. If the provider is not found, then
117 it throws NoSuchProviderException.
119 The returned AlgorithmParameters must still be intialized with
120 init().
122 @param algorithm the name of algorithm to choose
123 @param provider the name of the provider to find the algorithm in
124 @return a AlgorithmParameters repesenting the desired algorithm
126 @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
127 @throws NoSuchProviderException if the provider is not found
129 public static AlgorithmParameters getInstance(String algorithm,
130 String provider) throws
131 NoSuchAlgorithmException, NoSuchProviderException
133 Provider p = Security.getProvider(provider);
134 if (p == null)
135 throw new NoSuchProviderException();
137 return getInstance(p.getProperty("AlgorithmParameters." + algorithm),
138 algorithm, p);
141 private static AlgorithmParameters getInstance(String classname,
142 String algorithm,
143 Provider provider)
144 throws NoSuchAlgorithmException
149 return new AlgorithmParameters((AlgorithmParametersSpi) Class.
150 forName(classname).newInstance(),
151 provider, algorithm);
153 catch (ClassNotFoundException cnfe)
155 throw new NoSuchAlgorithmException("Class not found");
157 catch (InstantiationException ie)
159 throw new NoSuchAlgorithmException("Class instantiation failed");
161 catch (IllegalAccessException iae)
163 throw new NoSuchAlgorithmException("Illegal Access");
168 Gets the provider that the class is from.
170 @return the provider of this class
172 public final Provider getProvider()
174 return provider;
178 Initializes the engine with the specified
179 AlgorithmParameterSpec class.
181 @param paramSpec A AlgorithmParameterSpec to initialize with
183 @throws InvalidParameterSpecException For an inapporiate ParameterSpec class
185 public final void init(AlgorithmParameterSpec paramSpec) throws
186 InvalidParameterSpecException
188 paramSpi.engineInit(paramSpec);
192 Initializes the engine with the specified
193 parameters stored in the byte array and decodes them
194 according to the ASN.1 specification. If the ASN.1
195 specification exists then it succeeds or else it throws
196 IOException.
198 @param params Parameters to initialize with
200 @throws IOException Decoding Error
202 public final void init(byte[]params) throws IOException
204 paramSpi.engineInit(params);
208 Initializes the engine with the specified
209 parameters stored in the byte array and decodes them
210 according to the specified decoding specification.
211 If format is null, then it is decoded using the ASN.1
212 specification if it exists or else it throws
213 IOException.
215 @param params Parameters to initialize with
216 @param format Name of decoding format to use
218 @throws IOException Decoding Error
220 public final void init(byte[]params, String format) throws IOException
222 paramSpi.engineInit(params, format);
226 Returns a specification of this AlgorithmParameters object.
227 paramSpec identifies the class to return the AlgortihmParameters
228 in.
230 @param paramSpec Class to return AlgorithmParameters in
232 @return the parameter specification
234 @throws InvalidParameterSpecException if the paramSpec is an invalid parameter class
236 public final AlgorithmParameterSpec getParameterSpec(Class paramSpec) throws
237 InvalidParameterSpecException
239 return paramSpi.engineGetParameterSpec(paramSpec);
243 Returns the parameters in the default encoding format.
244 The primary encoding format is ASN.1 format if it exists
245 for the specified type.
247 @return byte array representing the parameters
249 public final byte[] getEncoded() throws IOException
251 return paramSpi.engineGetEncoded();
255 Returns the parameters in the specified encoding format.
256 If <code>format</code> is <code>null</code> then the
257 primary encoding format is used, the ASN.1 format,
258 if it exists for the specified type.
260 @return byte array representing the parameters
262 public final byte[] getEncoded(String format) throws IOException
264 return paramSpi.engineGetEncoded(format);
268 Returns a string representation of the encoding format
270 @return a string containing the string representation
272 public final String toString()
274 return paramSpi.engineToString();