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