KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > license > SecurityFactory


1 package org.sapia.util.license;
2
3 import java.security.InvalidKeyException JavaDoc;
4 import java.security.KeyFactory JavaDoc;
5 import java.security.KeyPairGenerator JavaDoc;
6 import java.security.NoSuchAlgorithmException JavaDoc;
7 import java.security.NoSuchProviderException JavaDoc;
8 import java.security.PrivateKey JavaDoc;
9 import java.security.Provider JavaDoc;
10 import java.security.PublicKey JavaDoc;
11 import java.security.Signature JavaDoc;
12 import java.security.spec.EncodedKeySpec JavaDoc;
13 import java.security.spec.InvalidKeySpecException JavaDoc;
14 import java.security.spec.PKCS8EncodedKeySpec JavaDoc;
15 import java.security.spec.X509EncodedKeySpec JavaDoc;
16
17 /**
18  * This class is a convenient factory of <code>Signature</code> and <code>KeyPairGenerator</code> instances.
19  *
20  * @author Yanick Duchesne
21  *
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class SecurityFactory {
29   
30   /**
31    * This constant identifies the key used to lookup the <code>java.security.Provider</code>
32    * class that will be used to create <code>Signature</code> objects.
33    * <p>
34    * This constant's value is: <code>org.sapia.license.security.provider</code>.
35    */

36   public static final String JavaDoc SECURITY_PROVIDER_CLASS = "org.sapia.license.security.provider";
37   
38   private static Provider JavaDoc _defaultProvider;
39   
40   private Provider JavaDoc _provider;
41   
42   static{
43     String JavaDoc providerClass = System.getProperty(SECURITY_PROVIDER_CLASS);
44     if(providerClass != null){
45       try{
46         _defaultProvider = (Provider JavaDoc)Class.forName(providerClass).newInstance();
47       }catch(Throwable JavaDoc t){
48         System.err.println("*** Could not load provider: " + providerClass);
49         t.printStackTrace(System.err);
50       }
51     }
52   }
53   
54   /**
55    * @param provider the security <code>Provider</code>
56    * that this instance should use.
57    */

58   public void setProvider(Provider JavaDoc provider){
59     _provider = provider;
60   }
61   
62   /**
63    * @return the security <code>Provider</code> that this instance
64    * uses.
65    */

66   public Provider JavaDoc getProvider(){
67     return _provider;
68   }
69   
70   /**
71    * This method returns the default security <code>Provider</code> that an instance
72    * should use to create <code>Signature</code>s.
73    *
74    * @return the default security <code>Provider</code>.
75    *
76    * @see #getInstance(String)
77    */

78   public static Provider JavaDoc getDefaultProvider(){
79     return _defaultProvider;
80   }
81   
82   /**
83    * Creates a signature object and returns it. If this instance
84    * has been assigned a security provider, that provider is used
85    * to create the signature; otherwise, this method attempts using the
86    * "default" provider (configured through the
87    * <code>org.sapia.license.security.provider</code>
88    * system property). If again no such provider has been configured,
89    * then one of the providers configured as part of the <code>java.security</code>
90    * file will be used).
91    *
92    * @param algo an algorithm identifier.
93    * @return a <code>Signature</code>.
94    * @throws NoSuchAlgorithmException if no security provider supports
95    * the given algorithm.
96    */

97   public Signature JavaDoc newSignature(String JavaDoc algo) throws NoSuchAlgorithmException JavaDoc{
98     if(_provider != null){
99       return Signature.getInstance(algo, _provider);
100     }
101     else if(_defaultProvider != null){
102       return Signature.getInstance(algo, _provider);
103     }
104     return Signature.getInstance(algo);
105   }
106   
107   /**
108    * Creates a signature object with the security provider whose
109    * identifier is given.
110    *
111    * @param algo an algorithm identifier.
112    * @param provider a provider identifier.
113    * @return a <code>Signature</code>.
114    * @throws NoSuchAlgorithmException if no security provider supports
115    * the given algorithm.
116    * @throws NoSuchProviderException if the desired provider does not exist.
117    */

118   public Signature JavaDoc newSignature(String JavaDoc algo, String JavaDoc provider) throws NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc{
119     return Signature.getInstance(algo, provider);
120   }
121   
122   /**
123    * Creates a key pair generator and returns it. If this instance
124    * has been assigned a security provider, that provider is used
125    * to create the pair; otherwise, this method attempts using the
126    * "default" provider (configured through the
127    * <code>org.sapia.license.security.provider</code>
128    * system property). If again no such provider has been configured,
129    * then one of the providers configured as part of the <code>java.security</code>
130    * file will be used).
131    *
132    * @param algo an algorithm identifier.
133    * @return a <code>KeyPairGenerator</code>.
134    * @throws NoSuchAlgorithmException if no security provider supports
135    * the given algorithm.
136    */

137   public KeyPairGenerator JavaDoc newKeyPairGenerator(String JavaDoc algo) throws NoSuchAlgorithmException JavaDoc{
138     if(_provider != null){
139       return KeyPairGenerator.getInstance(algo, _provider);
140     }
141     else if(_defaultProvider != null){
142       return KeyPairGenerator.getInstance(algo, _provider);
143     }
144     return KeyPairGenerator.getInstance(algo);
145   }
146   
147   /**
148    * Creates a key pair generator with the security provider whose
149    * identifier is given.
150    *
151    * @param algo an algorithm identifier.
152    * @param provider a provider identifier.
153    * @return a <code>KeyPairGenerator</code>.
154    * @throws NoSuchAlgorithmException if no security provider supports
155    * the given algorithm.
156    * @throws NoSuchProviderException if the desired provider does not exist.
157    */

158   public KeyPairGenerator JavaDoc newKeyPairGenerator(String JavaDoc algo, String JavaDoc provider) throws NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc{
159     return KeyPairGenerator.getInstance(algo, provider);
160   }
161   
162   public PublicKey JavaDoc generatePublic(byte[] bytes) throws InvalidKeyException JavaDoc, InvalidKeySpecException JavaDoc, NoSuchAlgorithmException JavaDoc{
163    KeyFactory JavaDoc keyFactory;
164    if(_provider != null)
165      keyFactory = KeyFactory.getInstance("DSA", getProvider());
166    else
167      keyFactory = KeyFactory.getInstance("DSA");
168    
169    EncodedKeySpec JavaDoc spec = new X509EncodedKeySpec JavaDoc(bytes);
170    return keyFactory.generatePublic(spec);
171   }
172   
173   public PrivateKey JavaDoc generatePrivate(byte[] bytes) throws InvalidKeyException JavaDoc, InvalidKeySpecException JavaDoc, NoSuchAlgorithmException JavaDoc{
174     KeyFactory JavaDoc keyFactory;
175     if(_provider != null)
176       keyFactory = KeyFactory.getInstance("DSA", getProvider());
177     else
178       keyFactory = KeyFactory.getInstance("DSA");
179     
180     EncodedKeySpec JavaDoc spec = new PKCS8EncodedKeySpec JavaDoc(bytes);
181     return keyFactory.generatePrivate(spec);
182    }
183
184
185 }
186
Popular Tags