jdk/src/share/classes/javax/crypto/KeyGenerator.java
author juh
Wed, 03 Jul 2013 12:51:45 -0700
changeset 18771 9dadb0719cea
parent 10336 0bb1999251f8
child 21955 abc02575919c
permissions -rw-r--r--
8019772: Fix doclint issues in javax.crypto and javax.security subpackages Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.GetInstance.Instance;
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 the functionality of a secret (symmetric) key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>Key generators are constructed using one of the <code>getInstance</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * class methods of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>KeyGenerator objects are reusable, i.e., after a key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * generated, the same KeyGenerator object can be re-used to generate further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>There are two ways to generate a key: in an algorithm-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * manner, and in an algorithm-specific manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The only difference between the two is the initialization of the object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li><b>Algorithm-Independent Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>All key generators share the concepts of a <i>keysize</i> and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <i>source of randomness</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * There is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link #init(int, java.security.SecureRandom) init}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method in this KeyGenerator class that takes these two universally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * shared types of arguments. There is also one that takes just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>keysize</code> argument, and uses the SecureRandom implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * providers supply a SecureRandom implementation), and one that takes just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>Since no other parameters are specified when you call the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * algorithm-independent <code>init</code> methods, it is up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * provider what to do about the algorithm-specific parameters (if any) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * associated with each of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li><b>Algorithm-Specific Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>For situations where a set of algorithm-specific parameters already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exists, there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link #init(java.security.spec.AlgorithmParameterSpec) init}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * methods that have an <code>AlgorithmParameterSpec</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * argument. One also has a <code>SecureRandom</code> argument, while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * other uses the SecureRandom implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * providers supply a SecureRandom implementation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>In case the client does not explicitly initialize the KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * (via a call to an <code>init</code> method), each provider must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * supply (and document) a default initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    87
 * <p> Every implementation of the Java platform is required to support the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    88
 * following standard <code>KeyGenerator</code> algorithms with the keysizes in
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    89
 * parentheses:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    90
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    91
 * <li><tt>AES</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    92
 * <li><tt>DES</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    93
 * <li><tt>DESede</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    94
 * <li><tt>HmacSHA1</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    95
 * <li><tt>HmacSHA256</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    96
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    97
 * These algorithms are described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    98
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    99
 * KeyGenerator section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   100
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   101
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   102
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   103
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
public class KeyGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // see java.security.KeyPairGenerator for failover notes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final static int I_NONE   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private final static int I_RANDOM = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private final static int I_PARAMS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private final static int I_SIZE   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private volatile KeyGeneratorSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   130
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private int initType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private int initKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private AlgorithmParameterSpec initParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private SecureRandom initRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Creates a KeyGenerator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param keyGenSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                           String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.spi = keyGenSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private KeyGenerator(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.algorithm = algorithm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   153
        List<Service> list =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   154
                GetInstance.getServices("KeyGenerator", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        initType = I_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (nextSpi(null, false) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                (algorithm + " KeyGenerator not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * KeyGeneratorSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   191
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   192
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   193
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          KeyGeneratorSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static final KeyGenerator getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return new KeyGenerator(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * KeyGeneratorSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   224
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   225
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   226
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * KeyGeneratorSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   266
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   267
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   268
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the provider of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return the provider of this <code>KeyGenerator</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            boolean reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            while (serviceIterator.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   324
                Service s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    Object inst = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    // ignore non-spis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    if (inst instanceof KeyGeneratorSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    KeyGeneratorSpi spi = (KeyGeneratorSpi)inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    if (reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        if (initType == I_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            spi.engineInit(initKeySize, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        } else if (initType == I_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            spi.engineInit(initParams, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        } else if (initType == I_RANDOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            spi.engineInit(initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        } else if (initType != I_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                ("KeyGenerator initType: " + initType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        initType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        initRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Initializes this key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param random the source of randomness for this generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public final void init(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            spi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                mySpi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                initType = I_RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Initializes this key generator with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   401
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * are inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public final void init(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        init(params, JceSecurity.RANDOM);
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
     * Initializes this key generator with the specified parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * set and a user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @exception InvalidAlgorithmParameterException if <code>params</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public final void init(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            spi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                mySpi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                initType = I_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                initParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (failure instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw (InvalidAlgorithmParameterException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        throw new InvalidAlgorithmParameterException("init() failed", failure);
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
     * Initializes this key generator for a certain keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   466
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public final void init(int keysize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        init(keysize, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Initializes this key generator for a certain keysize, using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public final void init(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            spi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                mySpi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                initType = I_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                initKeySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Generates a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return the new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public final SecretKey generateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return spi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                return mySpi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                mySpi = nextSpi(mySpi, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
}