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 Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.security.spec.InvalidKeySpecException;
024: import java.security.spec.KeySpec;
025:
026: import org.apache.harmony.security.fortress.Engine;
027: import org.apache.harmony.security.internal.nls.Messages;
028:
029: /**
030: * @com.intel.drl.spec_ref
031: *
032: */
033:
034: public class KeyFactory {
035: // The service name.
036: private static final String SERVICE = "KeyFactory"; //$NON-NLS-1$
037:
038: // The provider
039: private Provider provider;
040:
041: // Used to access common engine functionality
042: static private Engine engine = new Engine(SERVICE);
043:
044: // The SPI implementation.
045: private KeyFactorySpi spiImpl;
046:
047: // The algorithm.
048: private String algorithm;
049:
050: /**
051: * @com.intel.drl.spec_ref
052: *
053: */
054: protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
055: String algorithm) {
056: this .provider = provider;
057: this .algorithm = algorithm;
058: this .spiImpl = keyFacSpi;
059: }
060:
061: /**
062: * @com.intel.drl.spec_ref
063: *
064: */
065: public static KeyFactory getInstance(String algorithm)
066: throws NoSuchAlgorithmException {
067: if (algorithm == null) {
068: throw new NullPointerException(Messages
069: .getString("security.01")); //$NON-NLS-1$
070: }
071: synchronized (engine) {
072: engine.getInstance(algorithm, null);
073: return new KeyFactory((KeyFactorySpi) engine.spi,
074: engine.provider, algorithm);
075: }
076: }
077:
078: /**
079: * @com.intel.drl.spec_ref
080: *
081: */
082: public static KeyFactory getInstance(String algorithm,
083: String provider) throws NoSuchAlgorithmException,
084: NoSuchProviderException {
085: if ((provider == null) || (provider.length() == 0)) {
086: throw new IllegalArgumentException(Messages
087: .getString("security.02")); //$NON-NLS-1$
088: }
089: Provider p = Security.getProvider(provider);
090: if (p == null) {
091: throw new NoSuchProviderException(Messages.getString(
092: "security.03", provider)); //$NON-NLS-1$ //$NON-NLS-2$
093: }
094: return getInstance(algorithm, p);
095: }
096:
097: /**
098: * @com.intel.drl.spec_ref
099: *
100: */
101: public static KeyFactory getInstance(String algorithm,
102: Provider provider) throws NoSuchAlgorithmException {
103: if (provider == null) {
104: throw new IllegalArgumentException(Messages
105: .getString("security.04")); //$NON-NLS-1$
106: }
107: if (algorithm == null) {
108: throw new NullPointerException(Messages
109: .getString("security.01")); //$NON-NLS-1$
110: }
111: synchronized (engine) {
112: engine.getInstance(algorithm, provider, null);
113: return new KeyFactory((KeyFactorySpi) engine.spi, provider,
114: algorithm);
115: }
116: }
117:
118: /**
119: * @com.intel.drl.spec_ref
120: *
121: */
122: public final Provider getProvider() {
123: return provider;
124: }
125:
126: /**
127: * @com.intel.drl.spec_ref
128: *
129: */
130: public final String getAlgorithm() {
131: return algorithm;
132: }
133:
134: /**
135: * @com.intel.drl.spec_ref
136: *
137: */
138: public final PublicKey generatePublic(KeySpec keySpec)
139: throws InvalidKeySpecException {
140: return spiImpl.engineGeneratePublic(keySpec);
141: }
142:
143: /**
144: * @com.intel.drl.spec_ref
145: *
146: */
147: public final PrivateKey generatePrivate(KeySpec keySpec)
148: throws InvalidKeySpecException {
149: return spiImpl.engineGeneratePrivate(keySpec);
150: }
151:
152: /**
153: * @com.intel.drl.spec_ref
154: *
155: */
156: public final <T extends KeySpec> T getKeySpec(Key key,
157: Class<T> keySpec) throws InvalidKeySpecException {
158: return spiImpl.engineGetKeySpec(key, keySpec);
159: }
160:
161: /**
162: * @com.intel.drl.spec_ref
163: *
164: */
165: public final Key translateKey(Key key) throws InvalidKeyException {
166: return spiImpl.engineTranslateKey(key);
167: }
168: }
|