001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.security.InvalidAlgorithmParameterException;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.SecureRandom;
028: import java.security.Security;
029: import java.security.spec.AlgorithmParameterSpec;
030:
031: import org.apache.harmony.crypto.internal.nls.Messages;
032: import org.apache.harmony.security.fortress.Engine;
033:
034: /**
035: * @com.intel.drl.spec_ref
036: *
037: */
038:
039: public class KeyGenerator {
040:
041: // Used to access common engine functionality
042: private static final Engine engine = new Engine("KeyGenerator"); //$NON-NLS-1$
043:
044: // Store SecureRandom
045: private static final SecureRandom rndm = new SecureRandom();
046:
047: // Store used provider
048: private final Provider provider;
049:
050: // Store used spi implementation
051: private final KeyGeneratorSpi spiImpl;
052:
053: // Store used algorithm name
054: private final String algorithm;
055:
056: /**
057: * @com.intel.drl.spec_ref
058: *
059: */
060: protected KeyGenerator(KeyGeneratorSpi keyGenSpi,
061: Provider provider, String algorithm) {
062: this .provider = provider;
063: this .algorithm = algorithm;
064: this .spiImpl = keyGenSpi;
065: }
066:
067: /**
068: * @com.intel.drl.spec_ref
069: *
070: */
071: public final String getAlgorithm() {
072: return algorithm;
073: }
074:
075: /**
076: * @com.intel.drl.spec_ref
077: *
078: */
079: public final Provider getProvider() {
080: return provider;
081: }
082:
083: /**
084: * @com.intel.drl.spec_ref
085: *
086: */
087: public static final KeyGenerator getInstance(String algorithm)
088: throws NoSuchAlgorithmException {
089: if (algorithm == null) {
090: throw new NullPointerException(Messages
091: .getString("crypto.02")); //$NON-NLS-1$
092: }
093: synchronized (engine) {
094: engine.getInstance(algorithm, null);
095: return new KeyGenerator((KeyGeneratorSpi) engine.spi,
096: engine.provider, algorithm);
097: }
098: }
099:
100: /**
101: * @com.intel.drl.spec_ref
102: *
103: */
104: public static final KeyGenerator getInstance(String algorithm,
105: String provider) throws NoSuchAlgorithmException,
106: NoSuchProviderException {
107: if ((provider == null) || (provider.length() == 0)) {
108: throw new IllegalArgumentException(Messages
109: .getString("crypto.03")); //$NON-NLS-1$
110: }
111: Provider impProvider = Security.getProvider(provider);
112: if (impProvider == null) {
113: throw new NoSuchProviderException(provider);
114: }
115: return getInstance(algorithm, impProvider);
116: }
117:
118: /**
119: * @com.intel.drl.spec_ref
120: *
121: */
122: public static final KeyGenerator getInstance(String algorithm,
123: Provider provider) throws NoSuchAlgorithmException {
124: if (provider == null) {
125: throw new IllegalArgumentException(Messages
126: .getString("crypto.04")); //$NON-NLS-1$
127: }
128: if (algorithm == null) {
129: throw new NullPointerException(Messages
130: .getString("crypto.02")); //$NON-NLS-1$
131: }
132: synchronized (engine) {
133: engine.getInstance(algorithm, provider, null);
134: return new KeyGenerator((KeyGeneratorSpi) engine.spi,
135: provider, algorithm);
136: }
137: }
138:
139: /**
140: * @com.intel.drl.spec_ref
141: *
142: */
143: public final SecretKey generateKey() {
144: return spiImpl.engineGenerateKey();
145: }
146:
147: /**
148: * @com.intel.drl.spec_ref
149: *
150: */
151: public final void init(AlgorithmParameterSpec params)
152: throws InvalidAlgorithmParameterException {
153: spiImpl.engineInit(params, rndm);//new SecureRandom());
154: }
155:
156: /**
157: * @com.intel.drl.spec_ref
158: *
159: */
160: public final void init(AlgorithmParameterSpec params,
161: SecureRandom random)
162: throws InvalidAlgorithmParameterException {
163: spiImpl.engineInit(params, random);
164: }
165:
166: /**
167: * @com.intel.drl.spec_ref
168: *
169: */
170: public final void init(int keysize) {
171: spiImpl.engineInit(keysize, rndm);//new SecureRandom());
172: }
173:
174: /**
175: * @com.intel.drl.spec_ref
176: *
177: */
178: public final void init(int keysize, SecureRandom random) {
179: spiImpl.engineInit(keysize, random);
180: }
181:
182: /**
183: * @com.intel.drl.spec_ref
184: *
185: */
186: public final void init(SecureRandom random) {
187: spiImpl.engineInit(random);
188: }
189: }
|