FileDocCategorySizeDatePackage
SecretKeyFactory.javaAPI DocAndroid 1.5 API8806Wed May 06 22:41:02 BST 2009javax.crypto

SecretKeyFactory

public class SecretKeyFactory extends Object
The public API for {@code SecretKeyFactory} implementations.

Secret key factories provide the following functionality:

  • convert {@link SecretKey} objects to and from {@link KeySpec} objects
  • translate {@link SecretKey} objects from one provider implementation to another
Which key specifications are supported by the {@link #generateSecret} and {@link #getKeySpec} is provider dependent.

since
Android 1.0

Fields Summary
private static final org.apache.harmony.security.fortress.Engine
engine
private final Provider
provider
private final SecretKeyFactorySpi
spiImpl
private final String
algorithm
Constructors Summary
protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi, Provider provider, String algorithm)
Creates a new {@code SecretKeyFactory}

param
keyFacSpi the SPI delegate.
param
provider the provider providing this key factory.
param
algorithm the algorithm name for the secret key.
since
Android 1.0


                                                                         
      
                
        this.provider = provider;
        this.algorithm = algorithm;
        this.spiImpl = keyFacSpi;
    
Methods Summary
public final javax.crypto.SecretKeygenerateSecret(java.security.spec.KeySpec keySpec)
Generate a secret key from the specified key specification.

param
keySpec the key specification.
return
a secret key.
throws
InvalidKeySpecException if the specified key specification cannot be used to generate a secret key.
since
Android 1.0

        return spiImpl.engineGenerateSecret(keySpec);
    
public final java.lang.StringgetAlgorithm()
Returns the name of the secret key algorithm.

return
the name of the secret key algorithm.
since
Android 1.0

        return algorithm;
    
public static final javax.crypto.SecretKeyFactorygetInstance(java.lang.String algorithm)
Creates a new {@code SecretKeyFactory} instance for the specified key algorithm.

param
algorithm the name of the key algorithm.
return
a secret key factory for the specified key algorithm.
throws
NoSuchAlgorithmException if no installed provider can provide the requested algorithm.
throws
NullPointerException if the specified algorithm is {@code null}.
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, null);
            return new SecretKeyFactory((SecretKeyFactorySpi) engine.spi,
                    engine.provider, algorithm);
        }
    
public static final javax.crypto.SecretKeyFactorygetInstance(java.lang.String algorithm, java.lang.String provider)
Creates a new {@code SecretKeyFactory} instance for the specified key algorithm from the specified {@code provider}.

param
algorithm the name of the key algorithm.
param
provider the name of the provider that provides the requested algorithm.
return
a secret key factory for the specified key algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified provider cannot provide the requested algorithm.
throws
NoSuchProviderException if the specified provider does not exist.
throws
IllegalArgumentException if the specified provider name is {@code null} or empty.
since
Android 1.0

        if ((provider == null) || (provider.length() == 0)) {
            throw new IllegalArgumentException(Messages.getString("crypto.03")); //$NON-NLS-1$
        }
        Provider impProvider = Security.getProvider(provider);
        if (impProvider == null) {
            throw new NoSuchProviderException(provider);
        }
        return getInstance(algorithm, impProvider);
    
public static final javax.crypto.SecretKeyFactorygetInstance(java.lang.String algorithm, java.security.Provider provider)
Creates a new {@code SecretKeyFactory} instance for the specified key algorithm from the specified provider.

param
algorithm the name of the key algorithm.
param
provider the provider that provides the requested algorithm.
return
a secret key factory for the specified key algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified provider cannot provider the requested algorithm.
throws
IllegalArgumentException if the specified provider is {@code null}.
throws
NullPointerException is the specified algorithm name is {@code null}.
since
Android 1.0

        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.04")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, provider, null);
            return new SecretKeyFactory((SecretKeyFactorySpi) engine.spi, provider,
                    algorithm);
        }
    
public final java.security.spec.KeySpecgetKeySpec(javax.crypto.SecretKey key, java.lang.Class keySpec)
Returns the key specification of the specified secret key.

param
key the secret key to get the specification from.
param
keySpec the target key specification class.
return
an instance of the specified key specification class.
throws
InvalidKeySpecException if the specified secret key cannot be transformed into the requested key specification.
since
Android 1.0

        return spiImpl.engineGetKeySpec(key, keySpec);
    
public final java.security.ProvidergetProvider()
Returns the provider for this {@code SecretKeyFactory} instance.

return
the provider for this {@code SecretKeyFactory} instance.
since
Android 1.0

        return provider;
    
public final javax.crypto.SecretKeytranslateKey(javax.crypto.SecretKey key)
Translates the specified secret key into an instance of the corresponding key from the provider of this key factory.

param
key the secret key to translate.
return
the corresponding translated key.
throws
InvalidKeyException if the specified key cannot be translated using this key factory.
since
Android 1.0

        return spiImpl.engineTranslateKey(key);