summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/java/security/SecureRandom.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/java/security/SecureRandom.java')
-rw-r--r--libjava/classpath/java/security/SecureRandom.java126
1 files changed, 66 insertions, 60 deletions
diff --git a/libjava/classpath/java/security/SecureRandom.java b/libjava/classpath/java/security/SecureRandom.java
index c66963e8f96..005f4670efc 100644
--- a/libjava/classpath/java/security/SecureRandom.java
+++ b/libjava/classpath/java/security/SecureRandom.java
@@ -45,6 +45,7 @@ import gnu.java.security.jce.prng.Sha160RandomSpi;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
@@ -187,101 +188,106 @@ public class SecureRandom extends Random
this.algorithm = algorithm;
}
- // Class methods.
- // ------------------------------------------------------------------------
-
/**
- * Returns an instance of a SecureRandom. It creates the class from
- * the first provider that implements it.
- *
+ * Returns an instance of a <code>SecureRandom</code> from the first provider
+ * that implements it.
+ *
* @param algorithm The algorithm name.
- * @return A new SecureRandom implementing the given algorithm.
- * @throws NoSuchAlgorithmException If no installed provider implements
- * the given algorithm.
+ * @return A new <code>SecureRandom</code> implementing the given algorithm.
+ * @throws NoSuchAlgorithmException If no installed provider implements the
+ * given algorithm.
+ * @throws IllegalArgumentException if <code>algorithm</code> is
+ * <code>null</code> or is an empty string.
*/
public static SecureRandom getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
-
+ NoSuchAlgorithmException lastException = null;
for (int i = 0; i < p.length; i++)
- {
- try
- {
- return getInstance(algorithm, p[i]);
- }
- catch (NoSuchAlgorithmException e)
- {
- // Ignore.
- }
- }
-
- // None found.
+ try
+ {
+ return getInstance(algorithm, p[i]);
+ }
+ catch (NoSuchAlgorithmException x)
+ {
+ lastException = x;
+ }
+ if (lastException != null)
+ throw lastException;
throw new NoSuchAlgorithmException(algorithm);
}
/**
- * Returns an instance of a SecureRandom. It creates the class
- * for the specified algorithm from the named provider.
- *
+ * Returns an instance of a <code>SecureRandom</code> for the specified
+ * algorithm from the named provider.
+ *
* @param algorithm The algorithm name.
- * @param provider The provider name.
- * @return A new SecureRandom implementing the chosen algorithm.
+ * @param provider The provider name.
+ * @return A new <code>SecureRandom</code> implementing the chosen
+ * algorithm.
* @throws NoSuchAlgorithmException If the named provider does not implement
- * the algorithm, or if the implementation cannot be
- * instantiated.
- * @throws NoSuchProviderException If no provider named
- * <code>provider</code> is currently installed.
- * @throws IllegalArgumentException If <code>provider</code> is null
- * or is empty.
+ * the algorithm, or if the implementation cannot be instantiated.
+ * @throws NoSuchProviderException If no provider named <code>provider</code>
+ * is currently installed.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code> or empty.
*/
public static SecureRandom getInstance(String algorithm, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
- if (provider == null || provider.length() == 0)
- throw new IllegalArgumentException("Illegal provider");
-
+ if (provider == null)
+ throw new IllegalArgumentException("provider MUST NOT be null");
+ provider = provider.trim();
+ if (provider.length() == 0)
+ throw new IllegalArgumentException("provider MUST NOT be empty");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException(provider);
-
return getInstance(algorithm, p);
}
/**
- * Returns an instance of a SecureRandom. It creates the class for
- * the specified algorithm from the given provider.
- *
- * @param algorithm The SecureRandom algorithm to create.
- * @param provider The provider to get the instance from.
- * @throws NoSuchAlgorithmException If the algorithm cannot be found, or
- * if the class cannot be instantiated.
- * @throws IllegalArgumentException If <code>provider</code> is null.
+ * Returns an instance of a <code>SecureRandom</code> for the specified
+ * algorithm from the given provider.
+ *
+ * @param algorithm The <code>SecureRandom</code> algorithm to create.
+ * @param provider The provider to use.
+ * @throws NoSuchAlgorithmException If the algorithm cannot be found, or if
+ * the class cannot be instantiated.
+ * @throws IllegalArgumentException if either <code>algorithm</code> or
+ * <code>provider</code> is <code>null</code>, or if
+ * <code>algorithm</code> is an empty string.
*/
public static SecureRandom getInstance(String algorithm, Provider provider)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
- if (provider == null)
- throw new IllegalArgumentException("Illegal provider");
+ StringBuilder sb = new StringBuilder("SecureRandom for algorithm [")
+ .append(algorithm).append("] from provider[")
+ .append(provider).append("] could not be created");
+ Throwable cause;
try
{
- return new SecureRandom((SecureRandomSpi)
- Engine.getInstance(SECURE_RANDOM, algorithm, provider),
- provider, algorithm);
+ Object spi = Engine.getInstance(SECURE_RANDOM, algorithm, provider);
+ return new SecureRandom((SecureRandomSpi) spi, provider, algorithm);
}
- catch (java.lang.reflect.InvocationTargetException ite)
+ catch (InvocationTargetException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x.getCause();
+ if (cause instanceof NoSuchAlgorithmException)
+ throw (NoSuchAlgorithmException) cause;
+ if (cause == null)
+ cause = x;
}
- catch (ClassCastException cce)
+ catch (ClassCastException x)
{
- throw new NoSuchAlgorithmException(algorithm);
+ cause = x;
}
+ NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
+ x.initCause(cause);
+ throw x;
}
- // Instance methods.
- // ------------------------------------------------------------------------
-
/**
Returns the provider being used by the current SecureRandom class.
OpenPOWER on IntegriCloud