- Timestamp:
- Apr 27, 2004, 8:39:34 PM (22 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
libjava/java/security/SecureRandom.java (modified) (7 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libjava/java/security/SecureRandom.java
-
Property cvs2svn:cvs-rev
changed from
1.1to1.1.1.2
r1390 r1391 1 1 /* SecureRandom.java --- Secure Random class implmentation 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.2 Copyright (C) 1999, 2001 Free Software Foundation, Inc. 3 3 4 4 This file is part of GNU Classpath. … … 37 37 38 38 package java.security; 39 39 40 import java.io.Serializable; 40 41 import java.util.Random; … … 50 51 public class SecureRandom extends Random 51 52 { 53 54 52 55 //Serialized Field 53 56 long counter = 0; //Serialized 54 MessageDigest digest = null;55 57 Provider provider = null; 56 58 byte[] randomBytes = null; //Always null … … 82 84 for (i = 0; i < p.length; i++) 83 85 { 84 e = p[i].propertyNames(); 85 while (e.hasMoreElements()) 86 { 87 key = (String) e.nextElement(); 88 if (key.startsWith("SecureRandom.")) 89 if ((classname = p[i].getProperty(key)) != null) 90 break; 86 e = p[i].propertyNames(); 87 while (e.hasMoreElements()) 88 { 89 key = (String) e.nextElement(); 90 if (key.startsWith("SECURERANDOM.")) 91 { 92 if ((classname = p[i].getProperty(key)) != null) 93 { 94 try 95 { 96 secureRandomSpi = (SecureRandomSpi) Class. 97 forName(classname).newInstance(); 98 provider = p[i]; 99 return; 100 } 101 catch (Throwable ignore) { } 102 } 103 } 91 104 } 92 if (classname != null)93 break;94 105 } 95 106 96 //if( classname == null) 97 // throw new NoSuchAlgorithmException(); 98 99 try 100 { 101 this.secureRandomSpi = 102 (SecureRandomSpi) Class.forName(classname).newInstance(); 103 104 //s.algorithm = algorithm; 105 this.provider = p[i]; 106 } 107 catch (ClassNotFoundException cnfe) 108 { 109 //throw new NoSuchAlgorithmException("Class not found"); 110 } 111 catch (InstantiationException ie) 112 { 113 //throw new NoSuchAlgorithmException("Class instantiation failed"); 114 } 115 catch (IllegalAccessException iae) 116 { 117 //throw new NoSuchAlgorithmException("Illegal Access"); 118 } 107 // Nothing found. Fall back to SHA1PRNG 108 secureRandomSpi = new gnu.java.security.provider.SHA1PRNG(); 119 109 } 120 110 … … 166 156 { 167 157 Provider p[] = Security.getProviders(); 168 169 //Format of Key: SecureRandom.algname 170 StringBuffer key = new StringBuffer("SecureRandom."); 171 key.append(algorithm); 172 173 String classname = null; 174 int i; 175 for (i = 0; i < p.length; i++) 158 for (int i = 0; i < p.length; i++) 176 159 { 177 if ((classname = p[i].getProperty(key.toString())) != null) 178 break; 160 try 161 { 162 return getInstance(algorithm, p[i]); 163 } 164 catch (NoSuchAlgorithmException ignored) { } 179 165 } 180 166 181 if (classname == null) 182 throw new NoSuchAlgorithmException(); 183 184 try 185 { 186 return new SecureRandom((SecureRandomSpi) Class.forName(classname). 187 newInstance(), p[i]); 188 } 189 catch (ClassNotFoundException cnfe) 190 { 191 throw new NoSuchAlgorithmException("Class not found"); 192 } 193 catch (InstantiationException ie) 194 { 195 throw new NoSuchAlgorithmException("Class instantiation failed"); 196 } 197 catch (IllegalAccessException iae) 198 { 199 throw new NoSuchAlgorithmException("Illegal Access"); 200 } 201 167 // None found. 168 throw new NoSuchAlgorithmException(algorithm); 202 169 } 203 170 … … 221 188 if (p == null) 222 189 throw new NoSuchProviderException(); 223 224 //Format of Key: SecureRandom.algName 225 StringBuffer key = new StringBuffer("SecureRandom."); 226 key.append(algorithm); 227 228 String classname = p.getProperty(key.toString()); 229 if (classname == null) 230 throw new NoSuchAlgorithmException(); 231 232 try 190 191 return getInstance(algorithm, p); 192 } 193 194 /** 195 Returns an instance of a SecureRandom. It creates the class for 196 the specified algorithm from the given provider. 197 198 @param algorithm The SecureRandom algorithm to create. 199 @param provider The provider to get the instance from. 200 201 @throws NoSuchAlgorithmException If the algorithm cannot be found, or 202 if the class cannot be instantiated. 203 */ 204 public static SecureRandom getInstance(String algorithm, 205 Provider provider) throws 206 NoSuchAlgorithmException 207 { 208 return getInstance(algorithm, provider, true); 209 } 210 211 /** 212 Creates the instance of SecureRandom, recursing to resolve aliases. 213 214 @param algorithm The SecureRandom algorithm to create. 215 @param provider The provider to get the implementation from. 216 @param recurse Whether or not to recurse to resolve aliases. 217 218 @throws NoSuchAlgorithmException If the algorithm cannot be found, 219 if there are too many aliases, or if the class cannot be 220 instantiated. 221 */ 222 private static SecureRandom getInstance(String algorithm, 223 Provider provider, 224 boolean recurse) 225 throws NoSuchAlgorithmException 226 { 227 String msg = algorithm; 228 for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); ) 233 229 { 234 return new SecureRandom((SecureRandomSpi) Class.forName(classname). 235 newInstance(), p); 230 // We could replace the boolean with an integer, incrementing it 231 // every 232 String key = (String) e.nextElement(); 233 if (key.startsWith("SECURERANDOM.") 234 && key.substring(13).equalsIgnoreCase(algorithm)) 235 { 236 try 237 { 238 Class c = Class.forName(provider.getProperty(key)); 239 return new SecureRandom((SecureRandomSpi) c.newInstance(), 240 provider); 241 } 242 catch (Throwable ignored) { } 243 } 244 else if (key.startsWith("ALG.ALIAS.SECURERANDOM.") 245 && key.substring(23).equalsIgnoreCase(algorithm) && recurse) 246 { 247 try 248 { 249 // First see if this alias refers to a class in this 250 // provider. 251 return getInstance(provider.getProperty(key), provider, false); 252 } 253 catch (NoSuchAlgorithmException nsae) 254 { 255 Provider[] provs = Security.getProviders(); 256 for (int i = 0; i < provs.length; i++) 257 { 258 if (provs[i] == provider) 259 continue; 260 // Now try other providers for the implementation 261 try 262 { 263 return getInstance(provider.getProperty(key), 264 provs[i], false); 265 } 266 catch (NoSuchAlgorithmException nsae2) 267 { 268 msg = nsae2.getMessage(); 269 } 270 } 271 } 272 } 236 273 } 237 catch (ClassNotFoundException cnfe) 238 { 239 throw new NoSuchAlgorithmException("Class not found"); 240 } 241 catch (InstantiationException ie) 242 { 243 throw new NoSuchAlgorithmException("Class instantiation failed"); 244 } 245 catch (IllegalAccessException iae) 246 { 247 throw new NoSuchAlgorithmException("Illegal Access"); 248 } 249 274 throw new NoSuchAlgorithmException(algorithm); 250 275 } 251 276 … … 335 360 336 361 for (int i = 0; i < tmp.length; i++) 337 ret |= tmp[i] << (8 * i); 338 339 return ret; 362 ret |= (tmp[i] & 0xFF) << (8 * i); 363 364 long mask = (1L << numBits) - 1; 365 return (int) (ret & mask); 340 366 } 341 367 -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.
