Makefile.in: Rebuilt.
* Makefile.in: Rebuilt. * Makefile.am (ordinary_java_source_files): Added new files. * java/security/AlgorithmParameterGenerator.java, java/security/AlgorithmParameters.java, java/security/Engine.java, java/security/Identity.java, java/security/IdentityScope.java, java/security/KeyFactory.java, java/security/KeyPairGenerator.java, java/security/KeyStore.java, java/security/MessageDigest.java, java/security/Policy.java, java/security/ProtectionDomain.java, java/security/SecureRandom.java, java/security/Security.java, java/security/Signature.java, java/security/SignatureSpi.java, java/security/SignedObject.java, java/security/Signer.java, java/security/interfaces/RSAMultiPrimePrivateCrtKey.java, java/security/spec/PSSParameterSpec.java, java/security/spec/RSAMultiPrimePrivateCrtKeySpec.java, java/security/spec/RSAOtherPrimeInfo.java: New versions from Classpath. From-SVN: r65829
This commit is contained in:
parent
9e9e204234
commit
7451c1559e
24 changed files with 3797 additions and 1898 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
// Class methods.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
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
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue