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