jdk/src/share/classes/javax/crypto/KeyGenerator.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8152 94e5966bdf22
child 10336 0bb1999251f8
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 1997, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private Iterator serviceIterator;
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        List list = GetInstance.getServices("KeyGenerator", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        initType = I_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (nextSpi(null, false) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                (algorithm + " KeyGenerator not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
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
     * Returns the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * KeyGeneratorSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @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
   190
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   191
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   192
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *          KeyGeneratorSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static final KeyGenerator getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return new KeyGenerator(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * KeyGeneratorSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @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
   223
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   224
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   225
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * KeyGeneratorSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @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
   265
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   266
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   267
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns the provider of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return the provider of this <code>KeyGenerator</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
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
     * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            boolean reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            while (serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                Service s = (Service)serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    Object inst = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    // ignore non-spis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    if (inst instanceof KeyGeneratorSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    KeyGeneratorSpi spi = (KeyGeneratorSpi)inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    if (reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (initType == I_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            spi.engineInit(initKeySize, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        } else if (initType == I_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            spi.engineInit(initParams, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        } else if (initType == I_RANDOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                            spi.engineInit(initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        } else if (initType != I_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                ("KeyGenerator initType: " + initType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        initType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        initRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Initializes this key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param random the source of randomness for this generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public final void init(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            spi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                mySpi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                initType = I_RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Initializes this key generator with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * are inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public final void init(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        init(params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Initializes this key generator with the specified parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * set and a user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @exception InvalidAlgorithmParameterException if <code>params</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public final void init(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            spi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                mySpi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                initType = I_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                initParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (failure instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw (InvalidAlgorithmParameterException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        throw new InvalidAlgorithmParameterException("init() failed", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Initializes this key generator for a certain keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public final void init(int keysize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        init(keysize, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Initializes this key generator for a certain keysize, using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public final void init(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            spi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                mySpi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                initType = I_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                initKeySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Generates a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @return the new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public final SecretKey generateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return spi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                return mySpi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                mySpi = nextSpi(mySpi, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
}