jdk/src/share/classes/java/security/SecureRandom.java
author wetmore
Thu, 11 Apr 2013 21:03:24 -0700
changeset 16915 675d1569af3e
parent 9035 1255eb81cc2f
child 18156 edb590d448c5
permissions -rw-r--r--
6425477: Better support for generation of high entropy random numbers Reviewed-by: xuelei, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.*;
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
    29
import java.util.regex.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class provides a cryptographically strong random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * generator (RNG).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>A cryptographically strong random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * minimally complies with the statistical random number generator tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * specified in <a href="http://csrc.nist.gov/cryptval/140-2.htm">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <i>FIPS 140-2, Security Requirements for Cryptographic Modules</i></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * section 4.9.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Additionally, SecureRandom must produce non-deterministic output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Therefore any seed material passed to a SecureRandom object must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * unpredictable, and all SecureRandom output sequences must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * cryptographically strong, as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <a href="http://www.ietf.org/rfc/rfc1750.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <i>RFC 1750: Randomness Recommendations for Security</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>A caller obtains a SecureRandom instance via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * no-argument constructor or one of the <code>getInstance</code> methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> Many SecureRandom implementations are in the form of a pseudo-random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * number generator (PRNG), which means they use a deterministic algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * to produce a pseudo-random sequence from a true random seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Other implementations may produce true random numbers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * and yet others may use a combination of both techniques.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> Typical callers of SecureRandom invoke the following methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * to retrieve random bytes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *      SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      byte bytes[] = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      random.nextBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p> Callers may also invoke the <code>generateSeed</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * to generate a given number of seed bytes (to seed other random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * generators, for example):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      byte seed[] = random.generateSeed(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Note: Depending on the implementation, the <code>generateSeed</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <code>nextBytes</code> methods may block as entropy is being gathered,
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
    83
 * for example, if they need to read from /dev/random on various Unix-like
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * operating systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see java.security.SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see java.util.Random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
public class SecureRandom extends java.util.Random {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private Provider provider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The provider implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private SecureRandomSpi secureRandomSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The algorithm name of null if unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Seed Generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static volatile SecureRandom seedGenerator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * default random number algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Provider that supports a SecureRandom (RNG) algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   137
     * <p> See the SecureRandom section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   138
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   139
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public SecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * This call to our superclass constructor will result in a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * to our own <code>setSeed</code> method, which will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * immediately when it is passed zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        getDefaultPRNG(false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Constructs a secure random number generator (RNG) implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * default random number algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The SecureRandom instance is seeded with the specified seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p> This constructor traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Provider that supports a SecureRandom (RNG) algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * If none of the Providers support a RNG algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * then an implementation-specific default is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   175
     * <p> See the SecureRandom section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   176
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   177
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public SecureRandom(byte seed[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        getDefaultPRNG(true, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void getDefaultPRNG(boolean setSeed, byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        String prng = getPrngAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (prng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // bummer, get the SUN implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            prng = "SHA1PRNG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.secureRandomSpi = new sun.security.provider.SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            this.provider = Providers.getSunProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                SecureRandom random = SecureRandom.getInstance(prng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                this.secureRandomSpi = random.getSecureRandomSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                this.provider = random.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (setSeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    this.secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // never happens, because we made sure the algorithm exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new RuntimeException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // JDK 1.1 based implementations subclass SecureRandom instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // SecureRandomSpi. They will also go through this code path because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // they must call a SecureRandom constructor as it is their superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // If we are dealing with such an implementation, do not set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // algorithm value as it would be inaccurate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (getClass() == SecureRandom.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this.algorithm = prng;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Creates a SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param secureRandomSpi the SecureRandom implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected SecureRandom(SecureRandomSpi secureRandomSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                           Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this(secureRandomSpi, provider, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        super(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.secureRandomSpi = secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * SecureRandomSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param algorithm the name of the RNG algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   260
     * See the SecureRandom section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   261
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   262
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          SecureRandomSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public static SecureRandom getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            SecureRandomSpi.class, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p> A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * SecureRandomSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param algorithm the name of the RNG algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   303
     * See the SecureRandom section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   304
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   305
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @exception NoSuchAlgorithmException if a SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @exception IllegalArgumentException if the provider name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *          or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public static SecureRandom getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns a SecureRandom object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Random Number Generator (RNG) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p> A new SecureRandom object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * SecureRandomSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <p> The returned SecureRandom object has not been seeded.  To seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * returned object, call the <code>setSeed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * If <code>setSeed</code> is not called, the first call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>nextBytes</code> will force the SecureRandom object to seed itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This self-seeding will not occur if <code>setSeed</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param algorithm the name of the RNG algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   351
     * See the SecureRandom section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   352
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecureRandom">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   353
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * for information about standard RNG algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return the new SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @exception NoSuchAlgorithmException if a SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @exception IllegalArgumentException if the specified provider is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public static SecureRandom getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Instance instance = GetInstance.getInstance("SecureRandom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            SecureRandomSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return new SecureRandom((SecureRandomSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns the SecureRandomSpi of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    SecureRandomSpi getSecureRandomSpi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return secureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns the provider of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return the provider of this SecureRandom object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns the name of the algorithm implemented by this SecureRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the name of the algorithm or <code>unknown</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *          if the algorithm name cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return (algorithm != null) ? algorithm : "unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Reseeds this random object. The given seed supplements, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * replaces, the existing seed. Thus, repeated calls are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    synchronized public void setSeed(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        secureRandomSpi.engineSetSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Reseeds this random object, using the eight bytes contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * in the given <code>long seed</code>. The given seed supplements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * rather than replaces, the existing seed. Thus, repeated calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * are guaranteed never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>This method is defined for compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <code>java.util.Random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @see #getSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   432
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public void setSeed(long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * Ignore call from super constructor (as well as any other calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * unfortunate enough to be passing 0).  It's critical that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * ignore call from superclass constructor, as digest has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * yet been initialized at that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (seed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            secureRandomSpi.engineSetSeed(longToByteArray(seed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p> If a call to <code>setSeed</code> had not occurred previously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * the first call to this method forces this SecureRandom object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * to seed itself.  This self-seeding will not occur if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <code>setSeed</code> was previously called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param bytes the array to be filled in with random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   455
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    synchronized public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        secureRandomSpi.engineNextBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Generates an integer containing the user-specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * pseudo-random bits (right justified, with leading zeros).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * method overrides a <code>java.util.Random</code> method, and serves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * to provide a source of random bits to all of the methods inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * from that class (for example, <code>nextInt</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <code>nextLong</code>, and <code>nextFloat</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param numBits number of pseudo-random bits to be generated, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * 0 <= <code>numBits</code> <= 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return an <code>int</code> containing the user-specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * of pseudo-random bits (right justified, with leading zeros).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   474
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    final protected int next(int numBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int numBytes = (numBits+7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        byte b[] = new byte[numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        nextBytes(b);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   481
        for (int i = 0; i < numBytes; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            next = (next << 8) + (b[i] & 0xFF);
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   483
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return next >>> (numBytes*8 - numBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * <p>This method is only included for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * The caller is encouraged to use one of the alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <code>getInstance</code> methods to obtain a SecureRandom object, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * then call the <code>generateSeed</code> method to obtain seed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * from that object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @see #setSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public static byte[] getSeed(int numBytes) {
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   506
        if (seedGenerator == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            seedGenerator = new SecureRandom();
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   508
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return seedGenerator.generateSeed(numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Returns the given number of seed bytes, computed using the seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * generation algorithm that this class uses to seed itself.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * call may be used to seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public byte[] generateSeed(int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return secureRandomSpi.engineGenerateSeed(numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Helper function to convert a long into a byte array (least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * byte first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    private static byte[] longToByteArray(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        byte[] retVal = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            retVal[i] = (byte) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            l >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Gets a default PRNG algorithm by looking through all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * providers. Returns the first PRNG algorithm of the first provider that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * has registered a SecureRandom implementation, or null if none of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * registered providers supplies a SecureRandom implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    private static String getPrngAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        for (Provider p : Providers.getProviderList().providers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (Service s : p.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return s.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
16915
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   557
    /*
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   558
     * Lazily initialize since Pattern.compile() is heavy.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   559
     * Effective Java (2nd Edition), Item 71.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   560
     */
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   561
    private static final class StrongPatternHolder {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   562
        /*
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   563
         * Entries are alg:prov separated by ,
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   564
         * Allow for prepended/appended whitespace between entries.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   565
         *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   566
         * Capture groups:
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   567
         *     1 - alg
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   568
         *     2 - :prov (optional)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   569
         *     3 - prov (optional)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   570
         *     4 - ,nextEntry (optional)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   571
         *     5 - nextEntry (optional)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   572
         */
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   573
        private static Pattern pattern =
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   574
            Pattern.compile(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   575
                "\\s*([\\S&&[^:,]]*)(\\:([\\S&&[^,]]*))?\\s*(\\,(.*))?");
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   576
    }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   577
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   578
    /**
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   579
     * Returns a {@code SecureRandom} object that was selected by using
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   580
     * the algorithms/providers specified in the {@code
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   581
     * securerandom.strongAlgorithms} Security property.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   582
     * <p>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   583
     * Some situations require strong random values, such as when
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   584
     * creating high-value/long-lived secrets like RSA public/private
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   585
     * keys.  To help guide applications in selecting a suitable strong
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   586
     * {@code SecureRandom} implementation, Java distributions should
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   587
     * include a list of known strong {@code SecureRandom}
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   588
     * implementations in the {@code securerandom.strongAlgorithms}
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   589
     * Security property.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   590
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   591
     * <pre>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   592
     *     SecureRandom sr = SecureRandom.getStrongSecureRandom();
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   593
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   594
     *     if (sr == null) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   595
     *         // Decide if this is a problem, and whether to recover.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   596
     *         sr = new SecureRandom();
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   597
     *         if (!goodEnough(sr)) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   598
     *             return;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   599
     *         }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   600
     *     }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   601
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   602
     *     keyPairGenerator.initialize(2048, sr);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   603
     * </pre>
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   604
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   605
     * @return a strong {@code SecureRandom} implementation as indicated
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   606
     * by the {@code securerandom.strongAlgorithms} Security property, or
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   607
     * null if none are available.
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   608
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   609
     * @see Security#getProperty(String)
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   610
     *
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   611
     * @since 1.8
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   612
     */
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   613
    public static SecureRandom getStrongSecureRandom() {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   614
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   615
        String property = AccessController.doPrivileged(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   616
            new PrivilegedAction<String>() {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   617
                @Override
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   618
                public String run() {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   619
                    return Security.getProperty(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   620
                        "securerandom.strongAlgorithms");
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   621
                }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   622
            });
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   623
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   624
        if ((property == null) || (property.length() == 0)) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   625
            return null;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   626
        }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   627
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   628
        String remainder = property;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   629
        while (remainder != null) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   630
            Matcher m;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   631
            if ((m = StrongPatternHolder.pattern.matcher(
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   632
                    remainder)).matches()) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   633
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   634
                String alg = m.group(1);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   635
                String prov = m.group(3);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   636
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   637
                try {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   638
                    if (prov == null) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   639
                        return SecureRandom.getInstance(alg);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   640
                    } else {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   641
                        return SecureRandom.getInstance(alg, prov);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   642
                    }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   643
                } catch (NoSuchAlgorithmException |
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   644
                        NoSuchProviderException e) {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   645
                }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   646
                remainder = m.group(5);
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   647
            } else {
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   648
                remainder = null;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   649
            }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   650
        }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   651
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   652
        return null;
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   653
    }
675d1569af3e 6425477: Better support for generation of high entropy random numbers
wetmore
parents: 9035
diff changeset
   654
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    // Declare serialVersionUID to be compatible with JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    static final long serialVersionUID = 4940670005562187L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    // Retain unused values serialized from JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private byte[] state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private MessageDigest digest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * We know that the MessageDigest class does not implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * java.io.Serializable.  However, since this field is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * used, it will always be NULL and won't affect the serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * of the SecureRandom class itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    private byte[] randomBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private int randomBytesUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    private long counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}