diff options
author | Brian Jones <cbj@gnu.org> | 2003-03-27 03:32:08 +0000 |
---|---|---|
committer | Brian Jones <cbj@gnu.org> | 2003-03-27 03:32:08 +0000 |
commit | 8e60cd79d0176a6e86aea698100de3550503c000 (patch) | |
tree | bce574ecece50b2af477f9d71d737532eaf602de /java/security/SecureRandom.java | |
parent | b2bab933c09836f3f4d71f2b3c1b8ced80083021 (diff) | |
download | classpath-8e60cd79d0176a6e86aea698100de3550503c000.tar.gz |
2003-03-26 C. Brian Jones <cbj@gnu.org>
* configure.in: VERSION changed to 0.05+cvs
* THANKYOU: added Casey Marshall
2003-03-26 Casey Marshall <rsdio@metastatic.org>
* java/security/AlgorithmParameterGenerator.java
(getInstance (String)): add missing return statement
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
(getInstance (String,String,Provider)): removed
* java/security/AlgorithmParameters.java
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
(getInstance (String,String,Provider)): removed
* java/security/Engine.java: new file
* java/security/KeyFactory.java
(getInstance (String)): add missing return statement
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
(getInstance (String,String,Provider)): removed
* java/security/KeyPairGenerator.java
(getInstance (String,Provider)): reuse common Engine code
(getInstance (String,String,Provider)): removed
* java/security/KeyStore.java
(getInstance (String)): use getInstance(String,Provider) instead
(getInstance (String,String)): use getInstance(String,Provider) instead
(getInstance (String,Provider): reuse common Engine code
* java/security/MessageDigest.java
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
* java/security/SecureRandom.java
(getInstance (String): formatting
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
* java/security/Signature.java
(getInstance (String,String)): check for improper provider argument
(getInstance (String,Provider)): reuse common Engine code
(getInstance (String,String,Provider)): removed
Diffstat (limited to 'java/security/SecureRandom.java')
-rw-r--r-- | java/security/SecureRandom.java | 217 |
1 files changed, 93 insertions, 124 deletions
diff --git a/java/security/SecureRandom.java b/java/security/SecureRandom.java index d9ac153da..5cd3cae67 100644 --- a/java/security/SecureRandom.java +++ b/java/security/SecureRandom.java @@ -42,14 +42,24 @@ import java.util.Random; import java.util.Enumeration; /** - SecureRandom is the class interface for using SecureRandom - providers. It provides an interface to the SecureRandomSpi - engine so that programmers can generate pseudo-random numbers. - - @author Mark Benvenuto <ivymccough@worldnet.att.net> + * An interface to a cryptographically secure pseudo-random number + * generator (PRNG). Random (or at least unguessable) numbers are used + * in all areas of security and cryptography, from the generation of + * keys and initialization vectors to the generation of random padding + * bytes. + * + * @author Mark Benvenuto <ivymccough@worldnet.att.net> + * @author Casey Marshall */ public class SecureRandom extends Random { + + // Constants and fields. + // ------------------------------------------------------------------------ + + /** Service name for PRNGs. */ + private static final String SECURE_RANDOM = "SecureRandom"; + static final long serialVersionUID = 4940670005562187L; //Serialized Field @@ -60,6 +70,9 @@ public class SecureRandom extends Random SecureRandomSpi secureRandomSpi = null; byte[] state = null; + // Constructors. + // ------------------------------------------------------------------------ + /** Default constructor for SecureRandom. It constructs a new SecureRandom by instantating the first SecureRandom @@ -69,7 +82,7 @@ public class SecureRandom extends Random on the first call to getnextBytes it will force a seed. It is maintained for backwards compatibility and programs - should use getInstance. + should use {@link #getInstance(java.lang.String)}. */ public SecureRandom() { @@ -88,20 +101,20 @@ public class SecureRandom extends Random { key = (String) e.nextElement(); if (key.startsWith("SECURERANDOM.")) - { - if ((classname = p[i].getProperty(key)) != null) - { - try - { - secureRandomSpi = (SecureRandomSpi) Class. - forName(classname).newInstance(); - provider = p[i]; - return; - } - catch (Throwable ignore) { } - } - } - } + { + if ((classname = p[i].getProperty(key)) != null) + { + try + { + secureRandomSpi = (SecureRandomSpi) Class. + forName(classname).newInstance(); + provider = p[i]; + return; + } + catch (Throwable ignore) { } + } + } + } } // Nothing found. Fall back to SHA1PRNG @@ -141,15 +154,17 @@ public class SecureRandom extends Random this.provider = provider; } - /** - Returns an instance of a SecureRandom. It creates the class - for the specified algorithm if it exists from a provider. - - @param algorithm A SecureRandom algorithm to use + // Class methods. + // ------------------------------------------------------------------------ - @return Returns a new SecureRandom implmenting the chosen algorithm - - @throws NoSuchAlgorithmException if the algorithm cannot be found + /** + * Returns an instance of a SecureRandom. It creates the class from + * the first provider that implements it. + * + * @param algorithm The algorithm name. + * @return A new SecureRandom implmenting the given algorithm. + * @throws NoSuchAlgorithmException If no installed provider implements + * the given algorithm. */ public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException @@ -157,11 +172,13 @@ public class SecureRandom extends Random Provider p[] = Security.getProviders(); for (int i = 0; i < p.length; i++) { - try - { - return getInstance(algorithm, p[i]); - } - catch (NoSuchAlgorithmException ignored) { } + try + { + return getInstance(algorithm, p[i]); + } + catch (NoSuchAlgorithmException ignored) + { + } } // None found. @@ -169,21 +186,26 @@ public class SecureRandom extends Random } /** - Returns an instance of a SecureRandom. It creates the class - for the specified algorithm from the specified provider. - - @param algorithm A SecureRandom algorithm to use - @param provider A security provider to use - - @return Returns a new SecureRandom implmenting the chosen algorithm - - @throws NoSuchAlgorithmException if the algorithm cannot be found - @throws NoSuchProviderException if the provider cannot be found + * Returns an instance of a SecureRandom. It creates the class + * for the specified algorithm from the named provider. + * + * @param algorithm The algorithm name. + * @param provider The provider name. + * @return A new SecureRandom implmenting 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. */ - public static SecureRandom getInstance(String algorithm, - String provider) throws - NoSuchAlgorithmException, NoSuchProviderException + public static SecureRandom getInstance(String algorithm, String provider) + throws NoSuchAlgorithmException, NoSuchProviderException { + if (provider == null || provider.length() == 0) + throw new IllegalArgumentException("Illegal provider"); + Provider p = Security.getProvider(provider); if (p == null) throw new NoSuchProviderException(); @@ -192,88 +214,35 @@ public class SecureRandom extends Random } /** - 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. + * 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. */ - public static SecureRandom getInstance(String algorithm, - Provider provider) throws - NoSuchAlgorithmException + public static SecureRandom getInstance(String algorithm, Provider provider) + throws NoSuchAlgorithmException { - return getInstance(algorithm, provider, true); - } - - /** - Creates the instance of SecureRandom, recursing to resolve aliases. - - @param algorithm The SecureRandom algorithm to create. - @param provider The provider to get the implementation from. - @param recurse Whether or not to recurse to resolve aliases. - - @throws NoSuchAlgorithmException If the algorithm cannot be found, - if there are too many aliases, or if the class cannot be - instantiated. - */ - private static SecureRandom getInstance(String algorithm, - Provider provider, - boolean recurse) - throws NoSuchAlgorithmException - { - String msg = algorithm; - for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); ) + if (provider == null) + throw new IllegalArgumentException("Illegal provider"); + try { - // We could replace the boolean with an integer, incrementing it - // every - String key = (String) e.nextElement(); - if (key.startsWith("SECURERANDOM.") - && key.substring(13).equalsIgnoreCase(algorithm)) - { - try - { - Class c = Class.forName(provider.getProperty(key)); - return new SecureRandom((SecureRandomSpi) c.newInstance(), - provider); - } - catch (Throwable ignored) { } - } - else if (key.startsWith("ALG.ALIAS.SECURERANDOM.") - && key.substring(23).equalsIgnoreCase(algorithm) && recurse) - { - try - { - // First see if this alias refers to a class in this - // provider. - return getInstance(provider.getProperty(key), provider, false); - } - catch (NoSuchAlgorithmException nsae) - { - Provider[] provs = Security.getProviders(); - for (int i = 0; i < provs.length; i++) - { - if (provs[i] == provider) - continue; - // Now try other providers for the implementation - try - { - return getInstance(provider.getProperty(key), - provs[i], false); - } - catch (NoSuchAlgorithmException nsae2) - { - msg = nsae2.getMessage(); - } - } - } - } + return new SecureRandom((SecureRandomSpi) + Engine.getInstance(SECURE_RANDOM, algorithm, provider), + provider); + } + catch (ClassCastException cce) + { + throw new NoSuchAlgorithmException(algorithm); } - throw new NoSuchAlgorithmException(algorithm); } + // Instance methods. + // ------------------------------------------------------------------------ + /** Returns the provider being used by the current SecureRandom class. @@ -318,8 +287,8 @@ public class SecureRandom extends Random (byte) (0xff & (seed >> 16)), (byte) (0xff & (seed >> 8)), (byte) (0xff & seed) - }; - secureRandomSpi.engineSetSeed(tmp); + }; + secureRandomSpi.engineSetSeed(tmp); } } |