From 97b8365cafc3a344a22d3980b8ed885f5c6d8357 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 9 Jan 2007 19:58:05 +0000 Subject: Merged gcj-eclipse branch to trunk. From-SVN: r120621 --- libjava/classpath/java/security/SecureRandom.java | 126 +++++++++++----------- 1 file changed, 66 insertions(+), 60 deletions(-) (limited to 'libjava/classpath/java/security/SecureRandom.java') 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 SecureRandom 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 SecureRandom implementing the given algorithm. + * @throws NoSuchAlgorithmException If no installed provider implements the + * given algorithm. + * @throws IllegalArgumentException if algorithm is + * null 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 SecureRandom 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 SecureRandom 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 - * provider is currently installed. - * @throws IllegalArgumentException If provider is null - * or is empty. + * the algorithm, or if the implementation cannot be instantiated. + * @throws NoSuchProviderException If no provider named provider + * is currently installed. + * @throws IllegalArgumentException if either algorithm or + * provider is null 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 provider is null. + * Returns an instance of a SecureRandom for the specified + * algorithm from the given provider. + * + * @param algorithm The SecureRandom 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 algorithm or + * provider is null, or if + * algorithm 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. -- cgit v1.2.3