jdk/src/share/classes/java/security/SecureRandom.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class provides a cryptographically strong random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * generator (RNG).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>A cryptographically strong random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * minimally complies with the statistical random number generator tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * section 4.9.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Additionally, SecureRandom must produce non-deterministic output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Therefore any seed material passed to a SecureRandom object must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * unpredictable, and all SecureRandom output sequences must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * cryptographically strong, as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <a href="http://www.ietf.org/rfc/rfc1750.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <i>RFC 1750: Randomness Recommendations for Security</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>A caller obtains a SecureRandom instance via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * no-argument constructor or one of the <code>getInstance</code> methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> Many SecureRandom implementations are in the form of a pseudo-random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * number generator (PRNG), which means they use a deterministic algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to produce a pseudo-random sequence from a true random seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Other implementations may produce true random numbers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and yet others may use a combination of both techniques.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p> Typical callers of SecureRandom invoke the following methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * to retrieve random bytes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *      SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *      byte bytes[] = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      random.nextBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p> Callers may also invoke the <code>generateSeed</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * to generate a given number of seed bytes (to seed other random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * generators, for example):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      byte seed[] = random.generateSeed(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Note: Depending on the implementation, the <code>generateSeed</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>nextBytes</code> methods may block as entropy is being gathered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * for example, if they need to read from /dev/random on various unix-like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * operating systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see java.security.SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see java.util.Random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class SecureRandom extends java.util.Random {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private Provider provider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * The provider implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private SecureRandomSpi secureRandomSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The algorithm name of null if unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // Seed Generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static volatile SecureRandom seedGenerator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * default random number algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Provider that supports a SecureRandom (RNG) algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p> See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public SecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * This call to our superclass constructor will result in a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * to our own <code>setSeed</code> method, which will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * immediately when it is passed zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        getDefaultPRNG(false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * default random number algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * The SecureRandom instance is seeded with the specified seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Provider that supports a SecureRandom (RNG) algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p> See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public SecureRandom(byte seed[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        getDefaultPRNG(true, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private void getDefaultPRNG(boolean setSeed, byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        String prng = getPrngAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (prng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // bummer, get the SUN implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            prng = "SHA1PRNG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            this.secureRandomSpi = new sun.security.provider.SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.provider = Providers.getSunProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                SecureRandom random = SecureRandom.getInstance(prng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                this.secureRandomSpi = random.getSecureRandomSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                this.provider = random.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // never happens, because we made sure the algorithm exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new RuntimeException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // JDK 1.1 based implementations subclass SecureRandom instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // SecureRandomSpi. They will also go through this code path because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // they must call a SecureRandom constructor as it is their superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // If we are dealing with such an implementation, do not set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // algorithm value as it would be inaccurate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (getClass() == SecureRandom.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            this.algorithm = prng;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Creates a SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param secureRandomSpi the SecureRandom implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    protected SecureRandom(SecureRandomSpi secureRandomSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                           Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this(secureRandomSpi, provider, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this.secureRandomSpi = secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param algorithm the name of the RNG algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          SecureRandomSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public static SecureRandom getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            SecureRandomSpi.class, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <p> A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * SecureRandomSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param algorithm the name of the RNG algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception NoSuchAlgorithmException if a SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception IllegalArgumentException if the provider name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *          or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public static SecureRandom getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p> A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * SecureRandomSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param algorithm the name of the RNG algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception NoSuchAlgorithmException if a SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception IllegalArgumentException if the specified provider is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public static SecureRandom getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns the SecureRandomSpi of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    SecureRandomSpi getSecureRandomSpi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Returns the provider of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return the provider of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Returns the name of the algorithm implemented by this SecureRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return the name of the algorithm or <code>unknown</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *          if the algorithm name cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return (algorithm != null) ? algorithm : "unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Reseeds this random object. The given seed supplements, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * replaces, the existing seed. Thus, repeated calls are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    synchronized public void setSeed(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Reseeds this random object, using the eight bytes contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * in the given <code>long seed</code>. The given seed supplements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * rather than replaces, the existing seed. Thus, repeated calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * are guaranteed never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>This method is defined for compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <code>java.util.Random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public void setSeed(long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         * Ignore call from super constructor (as well as any other calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * unfortunate enough to be passing 0).  It's critical that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * ignore call from superclass constructor, as digest has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * yet been initialized at that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (seed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            secureRandomSpi.engineSetSeed(longToByteArray(seed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p> If a call to <code>setSeed</code> had not occurred previously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * the first call to this method forces this SecureRandom object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * to seed itself.  This self-seeding will not occur if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * <code>setSeed</code> was previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param bytes the array to be filled in with random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    synchronized public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        secureRandomSpi.engineNextBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Generates an integer containing the user-specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * pseudo-random bits (right justified, with leading zeros).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * method overrides a <code>java.util.Random</code> method, and serves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * to provide a source of random bits to all of the methods inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * from that class (for example, <code>nextInt</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <code>nextLong</code>, and <code>nextFloat</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param numBits number of pseudo-random bits to be generated, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * 0 <= <code>numBits</code> <= 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return an <code>int</code> containing the user-specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * of pseudo-random bits (right justified, with leading zeros).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    final protected int next(int numBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int numBytes = (numBits+7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        byte b[] = new byte[numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        int next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        nextBytes(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        for (int i = 0; i < numBytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            next = (next << 8) + (b[i] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return next >>> (numBytes*8 - numBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>This method is only included for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * The caller is encouraged to use one of the alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <code>getInstance</code> methods to obtain a SecureRandom object, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * then call the <code>generateSeed</code> method to obtain seed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * from that object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @see #setSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public static byte[] getSeed(int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (seedGenerator == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            seedGenerator = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return seedGenerator.generateSeed(numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public byte[] generateSeed(int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return secureRandomSpi.engineGenerateSeed(numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Helper function to convert a long into a byte array (least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * byte first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    private static byte[] longToByteArray(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        byte[] retVal = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            retVal[i] = (byte) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            l >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Gets a default PRNG algorithm by looking through all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * providers. Returns the first PRNG algorithm of the first provider that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * has registered a SecureRandom implementation, or null if none of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * registered providers supplies a SecureRandom implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private static String getPrngAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        for (Provider p : Providers.getProviderList().providers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            for (Service s : p.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    return s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    // Declare serialVersionUID to be compatible with JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    static final long serialVersionUID = 4940670005562187L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    // Retain unused values serialized from JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private byte[] state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    private MessageDigest digest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * We know that the MessageDigest class does not implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * java.io.Serializable.  However, since this field is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * used, it will always be NULL and won't affect the serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * of the SecureRandom class itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    private byte[] randomBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private int randomBytesUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private long counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
}