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 java.security;
022:
023: import java.security.spec.AlgorithmParameterSpec;
024:
025: import org.apache.harmony.security.fortress.Engine;
026: import org.apache.harmony.security.internal.nls.Messages;
027:
028: /**
029: * @com.intel.drl.spec_ref
030: *
031: */
032:
033: public class AlgorithmParameterGenerator {
034:
035: // Store spi service name
036: private static final String SERVICE = "AlgorithmParameterGenerator"; //$NON-NLS-1$
037:
038: // Used to access common engine functionality
039: private static Engine engine = new Engine(SERVICE);
040:
041: // Store SecureRandom
042: private static SecureRandom randm = new SecureRandom();
043:
044: // Store used provider
045: private final Provider provider;
046:
047: // Store used AlgorithmParameterGeneratorSpi implementation
048: private final AlgorithmParameterGeneratorSpi spiImpl;
049:
050: //Store used algorithm
051: private final String algorithm;
052:
053: /**
054: * @com.intel.drl.spec_ref
055: *
056: */
057: protected AlgorithmParameterGenerator(
058: AlgorithmParameterGeneratorSpi paramGenSpi,
059: Provider provider, String algorithm) {
060: this .provider = provider;
061: this .algorithm = algorithm;
062: this .spiImpl = paramGenSpi;
063: }
064:
065: /**
066: * @com.intel.drl.spec_ref
067: *
068: */
069: public final String getAlgorithm() {
070: return algorithm;
071: }
072:
073: /**
074: * @com.intel.drl.spec_ref
075: *
076: * throws NullPointerException when algorithm is null
077: */
078: public static AlgorithmParameterGenerator getInstance(
079: String algorithm) throws NoSuchAlgorithmException {
080: if (algorithm == null) {
081: throw new NullPointerException(Messages
082: .getString("security.01")); //$NON-NLS-1$
083: }
084: synchronized (engine) {
085: engine.getInstance(algorithm, null);
086: return new AlgorithmParameterGenerator(
087: (AlgorithmParameterGeneratorSpi) engine.spi,
088: engine.provider, algorithm);
089: }
090: }
091:
092: /**
093: * @com.intel.drl.spec_ref
094: *
095: * throws NullPointerException if algorithm is null (instead of
096: * NoSuchAlgorithmException) as in 1.4 release
097: */
098: public static AlgorithmParameterGenerator getInstance(
099: String algorithm, String provider)
100: throws NoSuchAlgorithmException, NoSuchProviderException {
101: if ((provider == null) || (provider.length() == 0)) {
102: throw new IllegalArgumentException(Messages
103: .getString("security.02")); //$NON-NLS-1$
104: }
105: Provider impProvider = Security.getProvider(provider);
106: if (impProvider == null) {
107: throw new NoSuchProviderException(provider);
108: }
109: return getInstance(algorithm, impProvider);
110: }
111:
112: /**
113: * @com.intel.drl.spec_ref
114: *
115: * throws NullPointerException if algorithm is null (instead of
116: * NoSuchAlgorithmException) as in 1.4 release
117: */
118: public static AlgorithmParameterGenerator getInstance(
119: String algorithm, Provider provider)
120: throws NoSuchAlgorithmException {
121: if (provider == null) {
122: throw new IllegalArgumentException(Messages
123: .getString("security.04")); //$NON-NLS-1$
124: }
125: if (algorithm == null) {
126: throw new NullPointerException(Messages
127: .getString("security.01")); //$NON-NLS-1$
128: }
129: synchronized (engine) {
130: engine.getInstance(algorithm, provider, null);
131: return new AlgorithmParameterGenerator(
132: (AlgorithmParameterGeneratorSpi) engine.spi,
133: provider, algorithm);
134: }
135: }
136:
137: /**
138: * @com.intel.drl.spec_ref
139: *
140: */
141: public final Provider getProvider() {
142: return provider;
143: }
144:
145: /**
146: * @com.intel.drl.spec_ref
147: *
148: */
149: public final void init(int size) {
150: spiImpl.engineInit(size, randm);
151: }
152:
153: /**
154: * @com.intel.drl.spec_ref
155: *
156: */
157: public final void init(int size, SecureRandom random) {
158: spiImpl.engineInit(size, random);
159: }
160:
161: /**
162: * @com.intel.drl.spec_ref
163: *
164: */
165: public final void init(AlgorithmParameterSpec genParamSpec)
166: throws InvalidAlgorithmParameterException {
167: spiImpl.engineInit(genParamSpec, randm);
168: }
169:
170: /**
171: * @com.intel.drl.spec_ref
172: *
173: */
174: public final void init(AlgorithmParameterSpec genParamSpec,
175: SecureRandom random)
176: throws InvalidAlgorithmParameterException {
177: spiImpl.engineInit(genParamSpec, random);
178: }
179:
180: /**
181: * @com.intel.drl.spec_ref
182: *
183: */
184: public final AlgorithmParameters generateParameters() {
185: return spiImpl.engineGenerateParameters();
186: }
187: }
|