securerandom.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 536 行 · 第 1/2 页

JAVA
536
字号
/* * @(#)SecureRandom.java	1.43 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */ package java.security;import java.util.Enumeration;/** * <p>This class provides a cryptographically strong pseudo-random number * generator (PRNG). A cryptographically strong pseudo-random number * minimally complies with the statistical random number generator tests * specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm"><i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>, section 4.9.1. * Additionally, SecureRandom must produce non-deterministic  * output and therefore it is required that the seed material be unpredictable * and that output of SecureRandom be cryptographically strong sequences as * described in <a href="http://www.ietf.org/rfc/rfc1750.txt"><i>RFC 1750: Randomness Recommendations for Security</i></a>. *  * <p>Like other algorithm-based classes in Java Security, SecureRandom  * provides implementation-independent algorithms, whereby a caller  * (application code) requests a particular PRNG algorithm * and is handed back a SecureRandom object for that algorithm. It is * also possible, if desired, to request a particular algorithm from a * particular provider. See the <code>getInstance</code> methods. * * <p>Thus, there are two ways to request a SecureRandom object: by * specifying either just an algorithm name, or both an algorithm name * and a package provider. * * <ul> * * <li>If just an algorithm name is specified, as in: * <pre> *      SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); * </pre> * the system will determine if there is an implementation of the algorithm * requested available in the environment, and if there is more than one, if * there is a preferred one.<p> *  * <li>If both an algorithm name and a package provider are specified, as in: * <pre> *      SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); * </pre> * the system will determine if there is an implementation of the * algorithm in the package requested, and throw an exception if there * is not. * * </ul> * * <p>The SecureRandom implementation attempts to completely * randomize the internal state of the generator itself unless * the caller follows the call to a <code>getInstance</code> method * with a call to the <code>setSeed</code> method: * <pre> *      SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); *      random.setSeed(seed); * </pre> * * <p>After the caller obtains the SecureRandom object from the * <code>getInstance</code> call, it can call <code>nextBytes</code> * to generate random bytes: * <pre> *      byte bytes[] = new byte[20]; *      random.nextBytes(bytes); * </pre> * * <p>The caller may also invoke the <code>generateSeed</code> method * to generate a given number of seed bytes (to seed other random number * generators, for example): * <pre> *      byte seed[] = random.generateSeed(20); * </pre> * * @see java.security.SecureRandomSpi * @see java.util.Random *  * @version 1.36, 02/02/00 * @author Benjamin Renaud * @author Josh Bloch  */public class SecureRandom extends java.util.Random {    /**     * The provider.     *     * @serial     * @since 1.2     */    private Provider provider = null;     /**     * The provider implementation.     *     * @serial     * @since 1.2     */    private SecureRandomSpi secureRandomSpi = null;    // Seed Generator    private static SecureRandom seedGenerator = null;    /**     * <p>By using this constructor, the caller obtains a SecureRandom object     * containing the implementation from the highest-priority installed     * provider that has a SecureRandom implementation.     *      * <p>Note that this instance of SecureRandom has not been seeded.     * A call to the <code>setSeed</code> method will seed the SecureRandom     * object.  If a call is not made to <code>setSeed</code>, the first call     * to the <code>nextBytes</code> method will force the SecureRandom object     * to seed itself.     *     * <p>This constructor is provided for backwards compatibility.      * The caller is encouraged to use one of the alternative     * <code>getInstance</code> methods to obtain a SecureRandom object.     */    public SecureRandom() {	/*	 * This call to our superclass constructor will result in a call	 * to our own <code>setSeed</code> method, which will return	 * immediately when it is passed zero.	 */	super(0);	String prng = getPrngAlgorithm();	if (prng == null) {	    // bummer, get the SUN implementation	    this.secureRandomSpi = new sun.security.provider.SecureRandom();	    this.provider = new sun.security.provider.Sun();	} else {	    try {		SecureRandom random = SecureRandom.getInstance(prng);		this.secureRandomSpi = random.getSecureRandomSpi();		this.provider = random.getProvider();	    } catch (NoSuchAlgorithmException nsae) {		// never happens, because we made sure the algorithm exists	    }	}    }    /**     * <p>By using this constructor, the caller obtains a SecureRandom object     * containing the implementation from the highest-priority installed     * provider that has a SecureRandom implementation. This constructor      * uses a user-provided seed in     * preference to the self-seeding algorithm referred to in the empty     * constructor description. It may be preferable to the empty constructor     * if the caller has access to high-quality random bytes from some physical     * device (for example, a radiation detector or a noisy diode).     *      * <p>This constructor is provided for backwards compatibility.      * The caller is encouraged to use one of the alternative     * <code>getInstance</code> methods to obtain a SecureRandom object, and     * then to call the <code>setSeed</code> method to seed it.     *      * @param seed the seed.     */    public SecureRandom(byte seed[]) {	super(0);	String prng = getPrngAlgorithm();	if (prng == null) {	    // bummer, get the SUN implementation	    this.secureRandomSpi = new sun.security.provider.SecureRandom();	    this.provider = new sun.security.provider.Sun();	    this.secureRandomSpi.engineSetSeed(seed);	} else {	    try {		SecureRandom random = getInstance(prng);		this.secureRandomSpi = random.getSecureRandomSpi();		this.provider = random.getProvider();		this.secureRandomSpi.engineSetSeed(seed);	    } catch (NoSuchAlgorithmException nsae) {		// never happens, because we made sure the algorithm exists	    }	}    }    /**     * Creates a SecureRandom object.     *     * @param secureRandomSpi the SecureRandom implementation.     * @param provider the provider.     */    protected SecureRandom(SecureRandomSpi secureRandomSpi,			   Provider provider) {	super(0);	this.secureRandomSpi = secureRandomSpi;	this.provider = provider;    }    /**     * Generates a SecureRandom object that implements the specified     * Pseudo Random Number Generator (PRNG) algorithm. If the default     * provider package provides an implementation of the requested PRNG,     * an instance of SecureRandom containing that implementation is returned.     * If the PRNG is not available in the default      * package, other packages are searched.     *     * <p>Note that the returned instance of SecureRandom has not been seeded.     * A call to the <code>setSeed</code> method will seed the SecureRandom     * object.  If a call is not made to <code>setSeed</code>, the first call     * to the <code>nextBytes</code> method will force the SecureRandom object     * to seed itself.     *     * @param algorithm the name of the PRNG algorithm.     * See Appendix A in the <a href=     * "../../../guide/security/CryptoSpec.html#AppA">     * Java Cryptography Architecture API Specification &amp; Reference </a>      * for information about standard PRNG algorithm names.     *     * @return the new SecureRandom object.     *     * @exception NoSuchAlgorithmException if the PRNG algorithm is     * not available in the caller's environment.     *     * @since 1.2     */    public static SecureRandom getInstance(String algorithm)	throws NoSuchAlgorithmException {	    try {		Object[] objs = Security.getImpl(algorithm,						 "SecureRandom",						 (String)null);		return new SecureRandom((SecureRandomSpi)objs[0],					(Provider)objs[1]);	    } catch (NoSuchProviderException e) {		throw new NoSuchAlgorithmException(algorithm + " not found");	    }    }    /**     * Generates a SecureRandom object for the specified PRNG     * algorithm, as supplied from the specified provider, if such a     * PRNG implementation is available from the provider.     *     * <p>Note that the returned instance of SecureRandom has not been seeded.     * A call to the <code>setSeed</code> method will seed the SecureRandom     * object.  If a call is not made to <code>setSeed</code>, the first call     * to the <code>nextBytes</code> method will force the SecureRandom object     * to seed itself.     *     * @param algorithm the name of the PRNG algorithm.     * See Appendix A in the <a href=     * "../../../guide/security/CryptoSpec.html#AppA">     * Java Cryptography Architecture API Specification &amp; Reference </a> 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?