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.InvalidKeyException;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.Security;
028: import java.security.spec.InvalidKeySpecException;
029: import java.security.spec.KeySpec;
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: public class SecretKeyFactory {
039:
040: // Used to access common engine functionality
041: private static final Engine engine = new Engine("SecretKeyFactory"); //$NON-NLS-1$
042:
043: // Store used provider
044: private final Provider provider;
045:
046: // Store used spi implementation
047: private final SecretKeyFactorySpi spiImpl;
048:
049: // Store used algorithm name
050: private final String algorithm;
051:
052: /**
053: * @com.intel.drl.spec_ref
054: *
055: */
056: protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi,
057: Provider provider, String algorithm) {
058: this .provider = provider;
059: this .algorithm = algorithm;
060: this .spiImpl = keyFacSpi;
061: }
062:
063: /**
064: * @com.intel.drl.spec_ref
065: *
066: */
067: public final String getAlgorithm() {
068: return algorithm;
069: }
070:
071: /**
072: * @com.intel.drl.spec_ref
073: *
074: */
075: public final Provider getProvider() {
076: return provider;
077: }
078:
079: /**
080: * @com.intel.drl.spec_ref
081: *
082: */
083: public static final SecretKeyFactory getInstance(String algorithm)
084: throws NoSuchAlgorithmException {
085: if (algorithm == null) {
086: throw new NullPointerException(Messages
087: .getString("crypto.02")); //$NON-NLS-1$
088: }
089: synchronized (engine) {
090: engine.getInstance(algorithm, null);
091: return new SecretKeyFactory(
092: (SecretKeyFactorySpi) engine.spi, engine.provider,
093: algorithm);
094: }
095: }
096:
097: /**
098: * @com.intel.drl.spec_ref
099: *
100: */
101: public static final SecretKeyFactory getInstance(String algorithm,
102: String provider) throws NoSuchAlgorithmException,
103: NoSuchProviderException {
104: if ((provider == null) || (provider.length() == 0)) {
105: throw new IllegalArgumentException(Messages
106: .getString("crypto.03")); //$NON-NLS-1$
107: }
108: Provider impProvider = Security.getProvider(provider);
109: if (impProvider == null) {
110: throw new NoSuchProviderException(provider);
111: }
112: return getInstance(algorithm, impProvider);
113: }
114:
115: /**
116: * @com.intel.drl.spec_ref
117: *
118: */
119: public static final SecretKeyFactory getInstance(String algorithm,
120: Provider provider) throws NoSuchAlgorithmException {
121: if (provider == null) {
122: throw new IllegalArgumentException(Messages
123: .getString("crypto.04")); //$NON-NLS-1$
124: }
125: if (algorithm == null) {
126: throw new NullPointerException(Messages
127: .getString("crypto.02")); //$NON-NLS-1$
128: }
129: synchronized (engine) {
130: engine.getInstance(algorithm, provider, null);
131: return new SecretKeyFactory(
132: (SecretKeyFactorySpi) engine.spi, provider,
133: algorithm);
134: }
135: }
136:
137: /**
138: * @com.intel.drl.spec_ref
139: *
140: */
141: public final SecretKey generateSecret(KeySpec keySpec)
142: throws InvalidKeySpecException {
143: return spiImpl.engineGenerateSecret(keySpec);
144: }
145:
146: /**
147: * @com.intel.drl.spec_ref
148: *
149: */
150: @SuppressWarnings("unchecked")
151: public final KeySpec getKeySpec(SecretKey key, Class keySpec)
152: throws InvalidKeySpecException {
153: return spiImpl.engineGetKeySpec(key, keySpec);
154: }
155:
156: /**
157: * @com.intel.drl.spec_ref
158: *
159: */
160: public final SecretKey translateKey(SecretKey key)
161: throws InvalidKeyException {
162: return spiImpl.engineTranslateKey(key);
163:
164: }
165: }
|