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

Diff of /classpath/java/security/SecureRandom.java

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

revision 1.7 by mkoch, Wed Oct 2 12:58:40 2002 UTC revision 1.8 by mark, Fri Dec 13 12:51:47 2002 UTC
# Line 1  Line 1 
1  /* SecureRandom.java --- Secure Random class implmentation  /* SecureRandom.java --- Secure Random class implmentation
2     Copyright (C) 1999, 2001 Free Software Foundation, Inc.     Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 53  public class SecureRandom extends Random Line 53  public class SecureRandom extends Random
53    
54    //Serialized Field    //Serialized Field
55    long counter = 0;             //Serialized    long counter = 0;             //Serialized
   MessageDigest digest = null;  
56    Provider provider = null;    Provider provider = null;
57    byte[] randomBytes = null;    //Always null    byte[] randomBytes = null;    //Always null
58    int randomBytesUsed = 0;    int randomBytesUsed = 0;
# Line 83  public class SecureRandom extends Random Line 82  public class SecureRandom extends Random
82      Enumeration e;      Enumeration e;
83      for (i = 0; i < p.length; i++)      for (i = 0; i < p.length; i++)
84        {        {
85          e = p[i].propertyNames();          e = p[i].propertyNames();
86          while (e.hasMoreElements())          while (e.hasMoreElements())
87            {            {
88              key = (String) e.nextElement();              key = (String) e.nextElement();
89              if (key.startsWith("SecureRandom."))              if (key.startsWith("SECURERANDOM."))
90                if ((classname = p[i].getProperty(key)) != null)                {
91                  break;                  if ((classname = p[i].getProperty(key)) != null)
92                      {
93                        try
94                          {
95                            secureRandomSpi = (SecureRandomSpi) Class.
96                              forName(classname).newInstance();
97                            provider = p[i];
98                            return;
99                          }
100                        catch (Throwable ignore) { }
101                      }
102                  }
103            }            }
         if (classname != null)  
             break;  
104        }        }
105    
106      //if( classname == null)      // Nothing found. Fall back to SHA1PRNG
107      //  throw new NoSuchAlgorithmException();      secureRandomSpi = new gnu.java.security.provider.SHA1PRNG();
   
     try  
       {  
         this.secureRandomSpi =  
           (SecureRandomSpi) Class.forName(classname).newInstance();  
   
         //s.algorithm = algorithm;  
         this.provider = p[i];  
       }  
     catch (ClassNotFoundException cnfe)  
       {  
         //throw new NoSuchAlgorithmException("Class not found");  
       }  
     catch (InstantiationException ie)  
       {  
         //throw new NoSuchAlgorithmException("Class instantiation failed");  
       }  
     catch (IllegalAccessException iae)  
       {  
         //throw new NoSuchAlgorithmException("Illegal Access");  
       }  
108    }    }
109    
110    /**    /**
# Line 167  public class SecureRandom extends Random Line 154  public class SecureRandom extends Random
154      NoSuchAlgorithmException      NoSuchAlgorithmException
155    {    {
156      Provider p[] = Security.getProviders();      Provider p[] = Security.getProviders();
157        for (int i = 0; i < p.length; i++)
     //Format of Key: SecureRandom.algname  
     StringBuffer key = new StringBuffer("SecureRandom.");  
     key.append(algorithm);  
   
     String classname = null;  
     int i;  
     for (i = 0; i < p.length; i++)  
       {  
         if ((classname = p[i].getProperty(key.toString())) != null)  
           break;  
       }  
   
     if (classname == null)  
         throw new NoSuchAlgorithmException();  
   
     try  
       {  
         return new SecureRandom((SecureRandomSpi) Class.forName(classname).  
                                 newInstance(), p[i]);  
       }  
     catch (ClassNotFoundException cnfe)  
       {  
         throw new NoSuchAlgorithmException("Class not found");  
       }  
     catch (InstantiationException ie)  
       {  
         throw new NoSuchAlgorithmException("Class instantiation failed");  
       }  
     catch (IllegalAccessException iae)  
158        {        {
159          throw new NoSuchAlgorithmException("Illegal Access");          try
160              {
161                return getInstance(algorithm, p[i]);
162              }
163            catch (NoSuchAlgorithmException ignored) { }
164        }        }
165    
166        // None found.
167        throw new NoSuchAlgorithmException(algorithm);
168    }    }
169    
170    /**    /**
# Line 222  public class SecureRandom extends Random Line 186  public class SecureRandom extends Random
186      Provider p = Security.getProvider(provider);      Provider p = Security.getProvider(provider);
187      if (p == null)      if (p == null)
188        throw new NoSuchProviderException();        throw new NoSuchProviderException();
189        
190        return getInstance(algorithm, p);
191      }
192    
193      //Format of Key: SecureRandom.algName    /**
194      StringBuffer key = new StringBuffer("SecureRandom.");       Returns an instance of a SecureRandom. It creates the class for
195      key.append(algorithm);       the specified algorithm from the given provider.
   
     String classname = p.getProperty(key.toString());  
     if (classname == null)  
       throw new NoSuchAlgorithmException();  
196    
197      try       @param algorithm The SecureRandom algorithm to create.
198        {       @param provider  The provider to get the instance from.
199          return new SecureRandom((SecureRandomSpi) Class.forName(classname).  
200                                  newInstance(), p);       @throws NoSuchAlgorithmException If the algorithm cannot be found, or
201        }               if the class cannot be instantiated.
202      catch (ClassNotFoundException cnfe)     */
203        {    public static SecureRandom getInstance(String algorithm,
204          throw new NoSuchAlgorithmException("Class not found");                                           Provider provider) throws
205        }      NoSuchAlgorithmException
206      catch (InstantiationException ie)    {
207        {      return getInstance(algorithm, provider, true);
208          throw new NoSuchAlgorithmException("Class instantiation failed");    }
209        }  
210      catch (IllegalAccessException iae)    /**
211         Creates the instance of SecureRandom, recursing to resolve aliases.
212    
213         @param algorithm The SecureRandom algorithm to create.
214         @param provider  The provider to get the implementation from.
215         @param recurse   Whether or not to recurse to resolve aliases.
216    
217         @throws NoSuchAlgorithmException If the algorithm cannot be found,
218                 if there are too many aliases, or if the class cannot be
219                 instantiated.
220       */
221      private static SecureRandom getInstance(String algorithm,
222                                              Provider provider,
223                                              boolean recurse)
224        throws NoSuchAlgorithmException
225      {
226        String msg = algorithm;
227        for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); )
228        {        {
229          throw new NoSuchAlgorithmException("Illegal Access");          // We could replace the boolean with an integer, incrementing it
230            // every
231            String key = (String) e.nextElement();
232            if (key.startsWith("SECURERANDOM.")
233                && key.substring(13).equalsIgnoreCase(algorithm))
234              {
235                try
236                  {
237                    Class c = Class.forName(provider.getProperty(key));
238                    return new SecureRandom((SecureRandomSpi) c.newInstance(),
239                                            provider);
240                  }
241                catch (Throwable ignored) { }
242              }
243            else if (key.startsWith("ALG.ALIAS.SECURERANDOM.")
244                     && key.substring(23).equalsIgnoreCase(algorithm) && recurse)
245              {
246                try
247                  {
248                    // First see if this alias refers to a class in this
249                    // provider.
250                    return getInstance(provider.getProperty(key), provider, false);
251                  }
252                catch (NoSuchAlgorithmException nsae)
253                  {
254                    Provider[] provs = Security.getProviders();
255                    for (int i = 0; i < provs.length; i++)
256                      {
257                        if (provs[i] == provider)
258                          continue;
259                        // Now try other providers for the implementation
260                        try
261                          {
262                            return getInstance(provider.getProperty(key),
263                                               provs[i], false);
264                          }
265                        catch (NoSuchAlgorithmException nsae2)
266                          {
267                            msg = nsae2.getMessage();
268                          }
269                      }
270                  }
271              }
272        }        }
273        throw new NoSuchAlgorithmException(algorithm);
274    }    }
275    
276    /**    /**

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

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