source: trunk/src/gcc/libjava/java/security/KeyPairGenerator.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.6 KB
Line 
1/* KeyPairGenerator.java --- Key Pair Generator Class
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.security;
39import java.security.spec.AlgorithmParameterSpec;
40
41/**
42 KeyPairGenerator is the class used to generate key pairs
43 for a security algorithm.
44
45 The KeyPairGenerator is created with the getInstance()
46 methods. The class is used to generate public and private
47 keys for an algorithm and associate it with
48 algorithm parameters.
49
50 @author Mark Benvenuto
51 */
52public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
53{
54 private Provider provider;
55 private String algorithm;
56
57 /**
58 Constructs a new KeyPairGenerator
59
60 @param algorithm the algorithm to use
61 */
62 protected KeyPairGenerator(String algorithm)
63 {
64 this.algorithm = algorithm;
65 this.provider = null;
66 }
67
68 /**
69 Returns the name of the algorithm used
70
71 @return A string with the name of the algorithm
72 */
73 public String getAlgorithm()
74 {
75 return algorithm;
76 }
77
78 /**
79 Gets an instance of the KeyPairGenerator class
80 which generates key pairs for the specified algorithm.
81 If the algorithm is not found then, it throws NoSuchAlgorithmException.
82
83 @param algorithm the name of algorithm to choose
84 @return a AlgorithmParameterGenerator repesenting the desired algorithm
85
86 @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
87 */
88 public static KeyPairGenerator getInstance(String algorithm) throws
89 NoSuchAlgorithmException
90 {
91 Provider[] p = Security.getProviders();
92
93 String name = "KeyPairGenerator." + algorithm;
94 for (int i = 0; i < p.length; i++)
95 {
96 String classname = p[i].getProperty(name);
97 if (classname != null)
98 return getInstance(classname, algorithm, p[i]);
99 }
100
101 throw new NoSuchAlgorithmException(algorithm);
102 }
103
104 /**
105 Gets an instance of the KeyPairGenerator class
106 which generates key pairs for the specified algorithm.
107 If the algorithm is not found then, it throws NoSuchAlgorithmException.
108
109 @param algorithm the name of algorithm to choose
110 @param provider the name of the provider to find the algorithm in
111 @return a AlgorithmParameterGenerator repesenting the desired algorithm
112
113 @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
114 @throws NoSuchProviderException if the provider is not found
115 */
116 public static KeyPairGenerator getInstance(String algorithm, String provider)
117 throws NoSuchAlgorithmException, NoSuchProviderException
118 {
119 Provider p = Security.getProvider(provider);
120 if (p == null)
121 throw new NoSuchProviderException();
122
123 return getInstance(p.getProperty("KeyPairGenerator." + algorithm),
124 algorithm, p);
125 }
126
127 private static KeyPairGenerator getInstance(String classname,
128 String algorithm,
129 Provider provider)
130 throws NoSuchAlgorithmException
131 {
132 try
133 {
134 Object o = Class.forName(classname).newInstance();
135 KeyPairGenerator kpg;
136 if (o instanceof KeyPairGeneratorSpi)
137 kpg =
138 (KeyPairGenerator) (new
139 DummyKeyPairGenerator((KeyPairGeneratorSpi) o,
140 algorithm));
141 else
142 {
143 kpg = (KeyPairGenerator) o;
144 kpg.algorithm = algorithm;
145 }
146
147 kpg.provider = provider;
148 return kpg;
149 }
150 catch (ClassNotFoundException cnfe)
151 {
152 throw new NoSuchAlgorithmException("Class not found");
153 }
154 catch (InstantiationException ie)
155 {
156 throw new NoSuchAlgorithmException("Class instantiation failed");
157 }
158 catch (IllegalAccessException iae)
159 {
160 throw new NoSuchAlgorithmException("Illegal Access");
161 }
162 }
163
164 /**
165 Gets the provider that the class is from.
166
167 @return the provider of this class
168 */
169 public final Provider getProvider()
170 {
171 return provider;
172 }
173
174 /**
175 Initializes the KeyPairGenerator for the specified key size.
176 (Since no source of randomness is specified, a default one is
177 provided.)
178
179 @param keysize Size of key to generate
180 */
181 public void initialize(int keysize)
182 {
183 initialize(keysize, new SecureRandom());
184 }
185
186 /**
187 Initializes the KeyPairGenerator for the specified key size
188 and specified SecureRandom.
189
190 @param keysize Size of key to generate
191 @param random SecureRandom to use
192
193 @since JDK 1.2
194 */
195 public void initialize(int keysize, SecureRandom random)
196 {
197 initialize(keysize, random);
198 }
199
200 /**
201 Initializes the KeyPairGenerator with the specified
202 AlgorithmParameterSpec class.
203 (Since no source of randomness is specified, a default one is
204 provided.)
205
206 @param params AlgorithmParameterSpec to initialize with
207
208 @since JDK 1.2
209 */
210 public void initialize(AlgorithmParameterSpec params)
211 throws InvalidAlgorithmParameterException
212 {
213 initialize(params, new SecureRandom());
214 }
215
216 /**
217 Initializes the KeyPairGenerator with the specified
218 AlgorithmParameterSpec class and specified SecureRandom.
219
220 @param params AlgorithmParameterSpec to initialize with
221 @param random SecureRandom to use
222
223 @since JDK 1.2
224 */
225 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
226 throws InvalidAlgorithmParameterException
227 {
228 super.initialize(params, random);
229 }
230
231 /**
232 Generates a KeyPair according the rules for the algorithm.
233 Unless intialized, algorithm defaults will be used. It
234 creates a unique key pair each time.
235
236 Same as generateKeyPair();
237
238 @return a key pair
239 */
240 public final KeyPair genKeyPair()
241 {
242 try
243 {
244 return getInstance("DSA", "GNU").generateKeyPair();
245 }
246 catch (Exception e)
247 {
248 System.err.println("genKeyPair failed: " + e);
249 e.printStackTrace();
250 return null;
251 }
252 }
253
254 /**
255 Generates a KeyPair according the rules for the algorithm.
256 Unless intialized, algorithm defaults will be used. It
257 creates a unique key pair each time.
258
259 Same as genKeyPair();
260
261 @return a key pair
262 */
263 public KeyPair generateKeyPair()
264 {
265 return genKeyPair();
266 }
267}
Note: See TracBrowser for help on using the repository browser.