1 7 8 package java.security; 9 10 import java.util.*; 11 12 import java.security.Provider.Service; 13 14 import sun.security.jca.*; 15 import sun.security.jca.GetInstance.Instance; 16 17 98 99 public class SecureRandom extends java.util.Random { 100 101 107 private Provider provider = null; 108 109 115 private SecureRandomSpi secureRandomSpi = null; 116 117 123 private String algorithm; 124 125 private static SecureRandom seedGenerator = null; 127 128 143 public SecureRandom() { 144 149 super(0); 150 getDefaultPRNG(false, null); 151 } 152 153 170 public SecureRandom(byte seed[]) { 171 super(0); 172 getDefaultPRNG(true, seed); 173 } 174 175 private void getDefaultPRNG(boolean setSeed, byte[] seed) { 176 String prng = getPrngAlgorithm(); 177 if (prng == null) { 178 prng = "SHA1PRNG"; 180 this.secureRandomSpi = new sun.security.provider.SecureRandom(); 181 this.provider = new sun.security.provider.Sun(); 182 if (setSeed) { 183 this.secureRandomSpi.engineSetSeed(seed); 184 } 185 } else { 186 try { 187 SecureRandom random = SecureRandom.getInstance(prng); 188 this.secureRandomSpi = random.getSecureRandomSpi(); 189 this.provider = random.getProvider(); 190 if (setSeed) { 191 this.secureRandomSpi.engineSetSeed(seed); 192 } 193 } catch (NoSuchAlgorithmException nsae) { 194 } 196 } 197 if (getClass() == SecureRandom .class) { 199 this.algorithm = prng; 200 } 201 } 202 203 209 protected SecureRandom(SecureRandomSpi secureRandomSpi, 210 Provider provider) { 211 this(secureRandomSpi, provider, null); 212 } 213 214 private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider, 215 String algorithm) { 216 super(0); 217 this.secureRandomSpi = secureRandomSpi; 218 this.provider = provider; 219 this.algorithm = algorithm; 220 } 221 222 249 public static SecureRandom getInstance(String algorithm) 250 throws NoSuchAlgorithmException { 251 Instance instance = GetInstance.getInstance("SecureRandom", 252 SecureRandomSpi .class, algorithm); 253 return new SecureRandom ((SecureRandomSpi )instance.impl, 254 instance.provider, algorithm); 255 } 256 257 291 public static SecureRandom getInstance(String algorithm, String provider) 292 throws NoSuchAlgorithmException , NoSuchProviderException { 293 Instance instance = GetInstance.getInstance("SecureRandom", 294 SecureRandomSpi .class, algorithm, provider); 295 return new SecureRandom ((SecureRandomSpi )instance.impl, 296 instance.provider, algorithm); 297 } 298 299 331 public static SecureRandom getInstance(String algorithm, 332 Provider provider) throws NoSuchAlgorithmException { 333 Instance instance = GetInstance.getInstance("SecureRandom", 334 SecureRandomSpi .class, algorithm, provider); 335 return new SecureRandom ((SecureRandomSpi )instance.impl, 336 instance.provider, algorithm); 337 } 338 339 342 SecureRandomSpi getSecureRandomSpi() { 343 return secureRandomSpi; 344 } 345 346 351 public final Provider getProvider() { 352 return provider; 353 } 354 355 362 public String getAlgorithm() { 363 return (algorithm != null) ? algorithm : "unknown"; 364 } 365 366 375 synchronized public void setSeed(byte[] seed) { 376 secureRandomSpi.engineSetSeed(seed); 377 } 378 379 392 public void setSeed(long seed) { 393 399 if (seed != 0) { 400 secureRandomSpi.engineSetSeed(longToByteArray(seed)); 401 } 402 } 403 404 411 412 synchronized public void nextBytes(byte[] bytes) { 413 secureRandomSpi.engineNextBytes(bytes); 414 } 415 416 430 final protected int next(int numBits) { 431 int numBytes = (numBits+7)/8; 432 byte b[] = new byte[numBytes]; 433 int next = 0; 434 435 nextBytes(b); 436 for (int i = 0; i < numBytes; i++) 437 next = (next << 8) + (b[i] & 0xFF); 438 439 return next >>> (numBytes*8 - numBits); 440 } 441 442 459 public static byte[] getSeed(int numBytes) { 460 if (seedGenerator == null) 461 seedGenerator = new SecureRandom (); 462 return seedGenerator.generateSeed(numBytes); 463 } 464 465 474 public byte[] generateSeed(int numBytes) { 475 return secureRandomSpi.engineGenerateSeed(numBytes); 476 } 477 478 482 private static byte[] longToByteArray(long l) { 483 byte[] retVal = new byte[8]; 484 485 for (int i = 0; i < 8; i++) { 486 retVal[i] = (byte) l; 487 l >>= 8; 488 } 489 490 return retVal; 491 } 492 493 499 private static String getPrngAlgorithm() { 500 List provs = Providers.getProviderList().providers(); 501 for (Iterator t = provs.iterator(); t.hasNext();) { 502 Provider p = (Provider )t.next(); 503 for (Iterator u = p.getServices().iterator(); u.hasNext();) { 504 Service s = (Service)u.next(); 505 if (s.getType().equals("SecureRandom")) { 506 return s.getAlgorithm(); 507 } 508 } 509 } 510 return null; 511 } 512 513 static final long serialVersionUID = 4940670005562187L; 515 516 520 private byte[] state; 521 524 private MessageDigest digest = null; 525 533 private byte[] randomBytes; 534 537 private int randomBytesUsed; 538 541 private long counter; 542 } 543 | Popular Tags |