OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / security / AlgorithmParameterGenerator.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package java.security;
19
20 import java.security.spec.AlgorithmParameterSpec;
21 import org.apache.harmony.security.fortress.Engine;
22
23 /**
24  * {@code AlgorithmParameterGenerator} is an engine class which is capable of
25  * generating parameters for the algorithm it was initialized with.
26  */
27 public class AlgorithmParameterGenerator {
28
29     // Store spi service name
30     private static final String SERVICE = "AlgorithmParameterGenerator";
31
32     // Used to access common engine functionality
33     private static Engine engine = new Engine(SERVICE);
34
35     // Store SecureRandom
36     private static SecureRandom randm = new SecureRandom();
37
38     // Store used provider
39     private final Provider provider;
40
41     // Store used AlgorithmParameterGeneratorSpi implementation
42     private final AlgorithmParameterGeneratorSpi spiImpl;
43
44     //Store used algorithm
45     private final String algorithm;
46
47     /**
48      * Constructs a new instance of {@code AlgorithmParameterGenerator} with the
49      * given arguments.
50      *
51      * @param paramGenSpi
52      *            a concrete implementation, this engine instance delegates to.
53      * @param provider
54      *            the provider.
55      * @param algorithm
56      *            the name of the algorithm.
57      */
58     protected AlgorithmParameterGenerator(
59             AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,
60             String algorithm) {
61         this.provider = provider;
62         this.algorithm = algorithm;
63         this.spiImpl = paramGenSpi;
64     }
65
66     /**
67      * Returns the name of the algorithm.
68      *
69      * @return the name of the algorithm.
70      */
71     public final String getAlgorithm() {
72         return algorithm;
73     }
74
75     /**
76      * Returns a new instance of {@code AlgorithmParameterGenerator} for the
77      * specified algorithm.
78      *
79      * @param algorithm
80      *            the name of the algorithm to use.
81      * @return a new instance of {@code AlgorithmParameterGenerator} for the
82      *         specified algorithm.
83      * @throws NoSuchAlgorithmException
84      *             if the specified algorithm is not available.
85      * @throws NullPointerException
86      *             if {@code algorithm} is {@code null}.
87      */
88     public static AlgorithmParameterGenerator getInstance(String algorithm)
89             throws NoSuchAlgorithmException {
90         if (algorithm == null) {
91             throw new NullPointerException();
92         }
93         synchronized (engine) {
94             engine.getInstance(algorithm, null);
95             return new AlgorithmParameterGenerator(
96                     (AlgorithmParameterGeneratorSpi) engine.spi, engine.provider,
97                     algorithm);
98         }
99     }
100
101     /**
102      * Returns a new instance of {@code AlgorithmParameterGenerator} from the
103      * specified provider for the specified algorithm.
104      *
105      * @param algorithm
106      *            the name of the algorithm to use.
107      * @param provider
108      *            name of the provider of the {@code
109      *            AlgorithmParameterGenerator}.
110      * @return a new instance of {@code AlgorithmParameterGenerator} for the
111      *         specified algorithm.
112      * @throws NoSuchAlgorithmException
113      *             if the specified algorithm is not available.
114      * @throws NoSuchProviderException
115      *             if the specified provider is not available.
116      * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
117      * @throws NullPointerException
118      *             if {@code algorithm} is {@code null}.
119      */
120     public static AlgorithmParameterGenerator getInstance(String algorithm,
121             String provider) throws NoSuchAlgorithmException,
122             NoSuchProviderException {
123         if (provider == null || provider.isEmpty()) {
124             throw new IllegalArgumentException();
125         }
126         Provider impProvider = Security.getProvider(provider);
127         if (impProvider == null) {
128             throw new NoSuchProviderException(provider);
129         }
130         return getInstance(algorithm, impProvider);
131     }
132
133     /**
134      * Returns a new instance of {@code AlgorithmParameterGenerator} from the
135      * specified provider for the specified algorithm.
136      *
137      * @param algorithm
138      *            the name of the algorithm to use.
139      * @param provider
140      *            the provider of the {@code AlgorithmParameterGenerator}.
141      * @return a new instance of {@code AlgorithmParameterGenerator} for the
142      *         specified algorithm.
143      * @throws NoSuchAlgorithmException
144      *             if the specified algorithm is not available.
145      * @throws NullPointerException
146      *             if {@code algorithm} is {@code null}.
147      * @throws IllegalArgumentException if {@code provider == null}
148      */
149     public static AlgorithmParameterGenerator getInstance(String algorithm,
150             Provider provider) throws NoSuchAlgorithmException {
151         if (provider == null) {
152             throw new IllegalArgumentException();
153         }
154         if (algorithm == null) {
155             throw new NullPointerException();
156         }
157         synchronized (engine) {
158             engine.getInstance(algorithm, provider, null);
159             return new AlgorithmParameterGenerator(
160                     (AlgorithmParameterGeneratorSpi) engine.spi, provider,
161                     algorithm);
162         }
163     }
164
165     /**
166      * Returns the provider associated with this {@code
167      * AlgorithmParameterGenerator}.
168      *
169      * @return the provider associated with this {@code
170      *         AlgorithmParameterGenerator}.
171      */
172     public final Provider getProvider() {
173         return provider;
174     }
175
176     /**
177      * Initializes this {@code AlgorithmParameterGenerator} with the given size.
178      * The default parameter set and a default {@code SecureRandom} instance
179      * will be used.
180      *
181      * @param size
182      *            the size (in number of bits).
183      */
184     public final void init(int size) {
185         spiImpl.engineInit(size, randm);
186     }
187
188     /**
189      * Initializes this {@code AlgorithmParameterGenerator} with the given size
190      * and the given {@code SecureRandom}. The default parameter set will be
191      * used.
192      *
193      * @param size
194      *            the size (in number of bits).
195      * @param random
196      *            the source of randomness.
197      */
198     public final void init(int size, SecureRandom random) {
199         spiImpl.engineInit(size, random);
200     }
201
202     /**
203      * Initializes this {@code AlgorithmParameterGenerator} with the given {@code
204      * AlgorithmParameterSpec}. A default {@code SecureRandom} instance will be
205      * used.
206      *
207      * @param genParamSpec
208      *            the parameters to use.
209      * @throws InvalidAlgorithmParameterException
210      *             if the specified parameters are not supported.
211      */
212     public final void init(AlgorithmParameterSpec genParamSpec)
213             throws InvalidAlgorithmParameterException {
214         spiImpl.engineInit(genParamSpec, randm);
215     }
216
217     /**
218      * Initializes this {@code AlgorithmParameterGenerator} with the given
219      * {@code AlgorithmParameterSpec} and the given {@code SecureRandom}.
220      *
221      * @param genParamSpec
222      *            the parameters to use.
223      * @param random
224      *            the source of randomness.
225      * @throws InvalidAlgorithmParameterException
226      *             if the specified parameters are not supported.
227      */
228     public final void init(AlgorithmParameterSpec genParamSpec,
229             SecureRandom random) throws InvalidAlgorithmParameterException {
230         spiImpl.engineInit(genParamSpec, random);
231     }
232
233     /**
234      * Computes and returns {@code AlgorithmParameters} for this generator's
235      * algorithm.
236      *
237      * @return {@code AlgorithmParameters} for this generator's algorithm.
238      */
239     public final AlgorithmParameters generateParameters() {
240         return spiImpl.engineGenerateParameters();
241     }
242 }