/[classpath]/classpath/java/security/KeyFactory.java
ViewVC logotype

Diff of /classpath/java/security/KeyFactory.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by mark, Tue Jan 22 22:27:00 2002 UTC revision 1.5 by raif, Sat Mar 8 14:10:12 2003 UTC
# Line 1  Line 1 
1  /* KeyFactory.java --- Key Factory Class  /* KeyFactory.java --- Key Factory Class
2     Copyright (C) 1999 Free Software Foundation, Inc.     Copyright (C) 1999, 2003, Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 36  obligated to do so.  If you do not wish Line 36  obligated to do so.  If you do not wish
36  exception statement from your version. */  exception statement from your version. */
37    
38  package java.security;  package java.security;
39    
40  import java.security.spec.KeySpec;  import java.security.spec.KeySpec;
41  import java.security.spec.InvalidKeySpecException;  import java.security.spec.InvalidKeySpecException;
42    
43  /**  /**
44     Key factories are used to convert keys (opaque cryptographic   * <p>Key factories are used to convert keys (opaque cryptographic keys of type
45     keys of type Key) into key specifications (transparent   * {@link Key}) into key specifications (transparent representations of the
46     representations of the underlying key material).   * underlying key material), and vice versa.</p>
47     *
48     Key factories are bi-directional. They allow a key class   * <p>Key factories are bi-directional. That is, they allow you to build an
49     to be converted into a key specification (key material) and   * opaque key object from a given key specification (key material), or to
50     back again.   * retrieve the underlying key material of a key object in a suitable format.</p>
51     *
52     For example DSA public keys can be specified as   * <p>Multiple compatible key specifications may exist for the same key. For
53     DSAPublicKeySpec or X509EncodedKeySpec. The key factory   * example, a <i>DSA</i> public key may be specified using {@link
54     translate these key specifications.   * java.security.spec.DSAPublicKeySpec} or {@link
55     * java.security.spec.X509EncodedKeySpec}. A key factory can be used to
56     @since JDK 1.2   * translate between compatible key specifications.</p>
57     *
58     * <p>The following is an example of how to use a key factory in order to
59     * instantiate a <i>DSA</i> public key from its encoding. Assume Alice has
60     * received a digital signature from Bob. Bob also sent her his public key (in
61     * encoded format) to verify his signature. Alice then performs the following
62     * actions:
63     *
64     * <pre>
65     *  X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
66     *  KeyFactory keyFactory = KeyFactory.getInstance("DSA");
67     *  PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
68     *  Signature sig = Signature.getInstance("DSA");
69     *  sig.initVerify(bobPubKey);
70     *  sig.update(data);
71     *  sig.verify(signature);
72     * </pre>
73     *
74     * @since 1.2
75     * @see Key
76     * @see PublicKey
77     * @see PrivateKey
78     * @see KeySpec
79     * @see java.security.spec.DSAPublicKeySpec
80     * @see java.security.spec.X509EncodedKeySpec
81     @author Mark Benvenuto     @author Mark Benvenuto
82   */   */
83  public class KeyFactory  public class KeyFactory
# Line 62  public class KeyFactory Line 87  public class KeyFactory
87    private String algorithm;    private String algorithm;
88    
89    /**    /**
90       Constructs a new keyFactory with the specified parameters.     * Creates a <code>KeyFactory</code> object.
91       *
92       @param keyFacSpi Key Factory SPI to use     * @param keyFacSpi the delegate.
93       @param provider the provider of the Key Factory SPI     * @param provider the provider.
94       @param algorithm the name of the key algorithm for this key factory     * @param algorithm the name of the algorithm to associate with this
95       * <code>KeyFactory</code>.
96     */     */
97    protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,    protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
98                         String algorithm)                         String algorithm)
# Line 76  public class KeyFactory Line 102  public class KeyFactory
102      this.algorithm = algorithm;      this.algorithm = algorithm;
103    }    }
104    
105    /**    /**
106       Gets an instance of the KeyFactory class representing     * Generates a <code>KeyFactory</code> object that implements the specified
107       the specified key factory. If the algorithm is not     * algorithm. If the default provider package provides an implementation of
108       found then, it throws NoSuchAlgorithmException.     * the requested algorithm, an instance of <code>KeyFactory</code> containing
109       * that implementation is returned. If the algorithm is not available in the
110       @param algorithm the name of algorithm to choose     * default package, other packages are searched.
111       @return a KeyFactory repesenting the desired algorithm     *
112       * @param algorithm the name of the requested key algorithm. See Appendix A
113       @throws NoSuchAlgorithmException if the algorithm is not implemented by providers     * in the Java Cryptography Architecture API Specification &amp; Reference
114       * for information about standard algorithm names.
115       * @return a <code>KeyFactory</code> object for the specified algorithm.
116       * @throws NoSuchAlgorithmException if the requested algorithm is not
117       * available in the default provider package or any of the other provider
118       * packages that were searched.
119     */     */
120    public static KeyFactory getInstance(String algorithm)    public static KeyFactory getInstance(String algorithm)
121      throws NoSuchAlgorithmException      throws NoSuchAlgorithmException
122    {    {
123      Provider[] p = Security.getProviders();      Provider[] p = Security.getProviders();
   
124      for (int i = 0; i < p.length; i++)      for (int i = 0; i < p.length; i++)
125        {        {
126          String classname = p[i].getProperty("KeyFactory." + algorithm);          String classname = p[i].getProperty("KeyFactory." + algorithm);
# Line 101  public class KeyFactory Line 131  public class KeyFactory
131      throw new NoSuchAlgorithmException(algorithm);      throw new NoSuchAlgorithmException(algorithm);
132    }    }
133    
134    /**    /**
135       Gets an instance of the KeyFactory class representing     * Generates a <code>KeyFactory</code> object for the specified algorithm
136       the specified key factory from the specified provider.     * from the specified provider.
137       If the algorithm is not found then, it throws     *
138       NoSuchAlgorithmException. If the provider is not found, then     * @param algorithm the name of the requested key algorithm. See Appendix A
139       it throws NoSuchProviderException.     * in the Java Cryptography Architecture API Specification &amp; Reference
140       * for information about standard algorithm names.
141       @param algorithm the name of algorithm to choose     * @param provider the name of the provider.
142       @param provider the name of the provider to find the algorithm in     * @return a <code>KeyFactory</code> object for the specified algorithm.
143       @return a KeyFactory repesenting the desired algorithm     * @throws NoSuchAlgorithmException if the algorithm is not available from
144       * the specified provider.
145       @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider     * @throws NoSuchProviderException if the provider has not been configured.
146       @throws NoSuchProviderException if the provider is not found     * @throws IllegalArgumentException if the provider name is null or empty.
147       * @see Provider
148     */     */
149    public static KeyFactory getInstance(String algorithm, String provider)    public static KeyFactory getInstance(String algorithm, String provider)
150      throws NoSuchAlgorithmException, NoSuchProviderException      throws NoSuchAlgorithmException, NoSuchProviderException
# Line 122  public class KeyFactory Line 153  public class KeyFactory
153      if (p == null)      if (p == null)
154        throw new NoSuchProviderException();        throw new NoSuchProviderException();
155    
156      return getInstance(p.getProperty("KeyFactory." + algorithm),      return getInstance(p.getProperty("KeyFactory." + algorithm), algorithm, p);
                        algorithm, p);  
157    }    }
158    
159    private static KeyFactory getInstance(String classname,    private static KeyFactory getInstance(String classname, String algorithm,
                                         String algorithm,  
160                                          Provider provider)                                          Provider provider)
161      throws NoSuchAlgorithmException      throws NoSuchAlgorithmException
162    {    {
   
163      try      try
164        {        {
165          return new KeyFactory((KeyFactorySpi) Class.forName(classname).          return new KeyFactory(
166                                newInstance(), provider, algorithm);              (KeyFactorySpi) Class.forName(classname).newInstance(),
167                provider,
168                algorithm);
169        }        }
170      catch (ClassNotFoundException cnfe)      catch (ClassNotFoundException cnfe)
171        {        {
172          throw new NoSuchAlgorithmException("Class not found");          throw new NoSuchAlgorithmException("Class not found");
173        }        }
174      catch (InstantiationException ie)      catch (InstantiationException ie)
175        {        {
176          throw new NoSuchAlgorithmException("Class instantiation failed");          throw new NoSuchAlgorithmException("Class instantiation failed");
177        }        }
178      catch (IllegalAccessException iae)      catch (IllegalAccessException iae)
179        {        {
180          throw new NoSuchAlgorithmException("Illegal Access");          throw new NoSuchAlgorithmException("Illegal Access");
181        }        }
182    }    }
183    
184    /**    /**
185       Gets the provider that the class is from.     * Returns the provider of this key factory object.
186       *
187       @return the provider of this class     * @return the provider of this key factory object.
188     */     */
189    public final Provider getProvider()    public final Provider getProvider()
190    {    {
# Line 162  public class KeyFactory Line 192  public class KeyFactory
192    }    }
193    
194    /**    /**
195       Returns the name of the algorithm used     * Gets the name of the algorithm associated with this <code>KeyFactory</code>.
196       *
197       @return A string with the name of the algorithm     * @return the name of the algorithm associated with this
198       * <code>KeyFactory</code>.
199     */     */
200    public final String getAlgorithm()    public final String getAlgorithm()
201    {    {
# Line 172  public class KeyFactory Line 203  public class KeyFactory
203    }    }
204    
205    /**    /**
206       Generates a public key from the provided key specification.     * Generates a public key object from the provided key specification (key
207       * material).
208       @param keySpec key specification     *
209       * @param keySpec the specification (key material) of the public key.
210       @return the public key     * @return the public key.
211       * @throws InvalidKeySpecException if the given key specification is
212       @throws InvalidKeySpecException invalid key specification for     * inappropriate for this key factory to produce a public key.
      this key factory to produce a public key  
213     */     */
214    public final PublicKey generatePublic(KeySpec keySpec) throws    public final PublicKey generatePublic(KeySpec keySpec)
215      InvalidKeySpecException      throws InvalidKeySpecException
216    {    {
217      return keyFacSpi.engineGeneratePublic(keySpec);      return keyFacSpi.engineGeneratePublic(keySpec);
218    }    }
219    
220    /**    /**
221       Generates a private key from the provided key specification.     * Generates a private key object from the provided key specification (key
222       * material).
223       @param keySpec key specification     *
224       * @param keySpec the specification (key material) of the private key.
225       @return the private key     * @return the private key.
226       * @throws InvalidKeySpecException if the given key specification is
227       @throws InvalidKeySpecException invalid key specification for     * inappropriate for this key factory to produce a private key.
      this key factory to produce a private key  
228     */     */
229    public final PrivateKey generatePrivate(KeySpec keySpec) throws    public final PrivateKey generatePrivate(KeySpec keySpec)
230      InvalidKeySpecException      throws InvalidKeySpecException
231    {    {
232      return keyFacSpi.engineGeneratePrivate(keySpec);      return keyFacSpi.engineGeneratePrivate(keySpec);
233    }    }
234    
235    /**    /**
236       Returns a key specification for the given key. keySpec     * Returns a specification (key material) of the given key object.
237       identifies the specification class to return the key     * <code>keySpec</code> identifies the specification class in which the key
238       material in.     * material should be returned. It could, for example, be
239       * <code>DSAPublicKeySpec.class</code>, to indicate that the key material
240       @param key the key     * should be returned in an instance of the {@link
241       @param keySpec the specification class to return the     * java.security.spec.DSAPublicKeySpec} class.
242       key material in.     *
243       * @param key the key.
244       @return the key specification in an instance of the requested     * @param keySpec the specification class in which the key material should be
245       specification class     * returned.
246       * @return the underlying key specification (key material) in an instance of
247       @throws InvalidKeySpecException the requested key specification     * the requested specification class.
248       is inappropriate for this key or the key is     * @throws InvalidKeySpecException if the requested key specification is
249       unrecognized.     * inappropriate for the given key, or the given key cannot be processed
250       * (e.g., the given key has an unrecognized algorithm or format).
251     */     */
252    public final KeySpec getKeySpec(Key key, Class keySpec)    public final KeySpec getKeySpec(Key key, Class keySpec)
253      throws InvalidKeySpecException      throws InvalidKeySpecException
# Line 226  public class KeyFactory Line 256  public class KeyFactory
256    }    }
257    
258    /**    /**
259       Translates the key from an unknown or untrusted provider     * Translates a key object, whose provider may be unknown or potentially
260       into a key for this key factory.     * untrusted, into a corresponding key object of this key factory.
261       *
262       @param the key from an unknown or untrusted provider     * @param key the key whose provider is unknown or untrusted.
263       * @return the translated key.
264       @return the translated key     * @throws InvalidKeyException if the given key cannot be processed by this
265       * key factory.
      @throws InvalidKeySpecException if the key cannot be  
      processed by this key factory  
266     */     */
267    public final Key translateKey(Key key) throws InvalidKeyException    public final Key translateKey(Key key) throws InvalidKeyException
268    {    {

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26