home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 9.5 KB | 294 lines |
- /*
- * @(#)AlgorithmParameters.java 1.9 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.security;
-
- import java.io.*;
- import java.security.spec.AlgorithmParameterSpec;
- import java.security.spec.InvalidParameterSpecException;
-
- /**
- * This class is used as an opaque representation of cryptographic parameters.
- *
- * <p>An <code>AlgorithmParameters</code> object for managing the parameters
- * for a particular algorithm can be obtained by
- * calling one of the <code>getInstance</code> factory methods
- * (static methods that return instances of a given class).
- *
- * <p>There are two ways to request such an implementation: by
- * specifying either just an algorithm name, or both an algorithm name
- * and a package provider.
- *
- * <ul>
- *
- * <li>If just an algorithm name is specified, the system will
- * determine if there is an AlgorithmParameters
- * implementation for the algorithm requested
- * available in the environment, and if there is more than one, if
- * there is a preferred one.
- *
- * <li>If both an algorithm name and a package provider are specified,
- * the system will determine if there is an implementation
- * in the package requested, and throw an exception if there
- * is not.
- *
- * </ul>
- *
- * <p>Once an <code>AlgorithmParameters</code> object is returned, it must be
- * initialized via a call to <code>init</code>, using an appropriate parameter
- * specification or parameter encoding.
- *
- * <p>A transparent parameter specification is obtained from an
- * <code>AlgorithmParameters</code> object via a call to
- * <code>getParameterSpec</code>, and a byte encoding of the parameters is
- * obtained via a call to <code>getEncoded</code>.
- *
- * @author Jan Luehe
- *
- * @version 1.9, 98/03/18
- *
- * @see java.security.spec.AlgorithmParameterSpec
- * @see java.security.spec.DSAParameterSpec
- * @see KeyPairGenerator
- *
- * @since JDK1.2
- */
-
- public class AlgorithmParameters {
-
- // The provider
- private Provider provider;
-
- // The provider implementation (delegate)
- private AlgorithmParametersSpi paramSpi;
-
- // The algorithm
- private String algorithm;
-
- // Has this object been initialized?
- private boolean initialized = false;
-
- /**
- * Creates an AlgorithmParameters object.
- *
- * @param keyFacSpi the delegate
- * @param provider the provider
- * @param algorithm the algorithm
- */
- protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
- Provider provider, String algorithm) {
- this.paramSpi = paramSpi;
- this.provider = provider;
- this.algorithm = algorithm;
- }
-
- /**
- * Returns the name of the algorithm associated with the parameter
- * set.
- *
- * @return the algorithm name.
- */
- public final String getAlgorithm() {
- return algorithm;
- }
-
- /**
- * Generates an AlgorithmParameters object that implements the specified digest
- * algorithm. If the default provider package
- * provides an implementation of the requested digest algorithm,
- * an instance of AlgorithmParameters containing that implementation is returned.
- * If the algorithm is not available in the default
- * package, other packages are searched.
- *
- * @param algorithm the name of the algorithm requested.
- *
- * @return the new AlgorithmParameters object.
- *
- * @exception NoSuchAlgorithmException if the algorithm is
- * not available in the environment.
- */
- public static final AlgorithmParameters getInstance(String algorithm)
- throws NoSuchAlgorithmException {
- try {
- Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
- null);
- return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
- (Provider)objs[1],
- algorithm);
- } catch(NoSuchProviderException e) {
- throw new NoSuchAlgorithmException(algorithm + " not found");
- }
- }
-
- /**
- * Generates an AlgorithmParameters object for the specified
- * algorithm, as supplied from the specified provider, if such an
- * algorithm is available from the provider.
- *
- * @param algorithm the name of the algorithm requested.
- *
- * @param provider the name of the provider.
- *
- * @return the new AlgorithmParameters object.
- *
- * @exception NoSuchAlgorithmException if the algorithm is
- * not available in the package supplied by the requested
- * provider.
- *
- * @exception NoSuchProviderException if the provider is not
- * available in the environment.
- *
- * @see Provider
- */
- public static final AlgorithmParameters getInstance(String algorithm,
- String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException {
-
- Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
- provider);
- return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
- (Provider)objs[1],
- algorithm);
- }
-
- /**
- * Returns the provider of this algorithm parameter object.
- *
- * @return the provider of this algorithm parameter object
- */
- public final Provider getProvider() {
- return this.provider;
- }
-
- /**
- * Initializes this parameters object using the parameters
- * specified in <code>paramSpec</code>.
- *
- * @param paramSpec the parameter specification.
- *
- * @exception InvalidParameterSpecException if the given parameter
- * specification is inappropriate for the initialization of this parameter
- * object.
- */
- public final void init(AlgorithmParameterSpec paramSpec)
- throws InvalidParameterSpecException {
- paramSpi.engineInit(paramSpec);
- this.initialized = true;
- }
-
- /**
- * Imports the specified parameters and decodes them according to the
- * primary decoding format for parameters. The primary decoding
- * format for parameters is ASN.1, if an ASN.1 specification for this type
- * of parameters exists.
- *
- * @param params the encoded parameters.
- *
- * @exception IOException on decoding errors
- */
- public final void init(byte[] params) throws IOException {
- paramSpi.engineInit(params);
- this.initialized = true;
- }
-
- /**
- * Imports the parameters from <code>params</code> and decodes them
- * according to the specified decoding format.
- * If <code>format</code> is null, the
- * primary decoding format for parameters is used. The primary decoding
- * format is ASN.1, if an ASN.1 specification for these parameters
- * exists.
- *
- * @param params the encoded parameters.
- *
- * @param format the name of the decoding format
- *
- * @exception IOException on decoding errors
- */
- public final void init(byte[] params, String format) throws IOException {
- paramSpi.engineInit(params, format);
- this.initialized = true;
- }
-
- /**
- * Returns a (transparent) specification of this parameters object.
- * <code>paramSpec</code> identifies the specification class in which
- * the parameters should be returned. It could, for example, be
- * <code>DSAParameterSpec.class</code>, to indicate that the
- * parameters should be returned in an instance of the
- * <code>DSAParameterSpec</code> class.
- *
- * @param paramSpec the specification class in which
- * the parameters should be returned.
- *
- * @return the parameter specification.
- *
- * @exception InvalidParameterSpecException if the requested parameter
- * specification is inappropriate for this parameter object.
- */
- public final AlgorithmParameterSpec getParameterSpec(Class paramSpec)
- throws InvalidParameterSpecException {
- return paramSpi.engineGetParameterSpec(paramSpec);
- }
-
- /**
- * Returns the parameters in their primary encoding format.
- * The primary encoding format for parameters is ASN.1, if an ASN.1
- * specification for this type of parameters exists.
- *
- * @return the parameters encoded using the specified encoding scheme.
- *
- * @exception IOException on encoding errors.
- */
- public final byte[] getEncoded() throws IOException {
- if (this.initialized == false) {
- throw new IOException("Uninitialized parameters");
- }
- return paramSpi.engineGetEncoded();
- }
-
- /**
- * Returns the parameters encoded in the specified format.
- * If <code>format</code> is null, the
- * primary encoding format for parameters is used. The primary encoding
- * format is ASN.1, if an ASN.1 specification for these parameters
- * exists.
- *
- * @param format the name of the encoding format.
- *
- * @return the parameters encoded using the specified encoding scheme.
- *
- * @exception IOException on encoding errors.
- */
- public final byte[] getEncoded(String format) throws IOException {
- if (this.initialized == false) {
- throw new IOException("Uninitialized parameters");
- }
- return paramSpi.engineGetEncoded(format);
- }
-
- /**
- * Returns a formatted string describing the parameters.
- *
- * @return a formatted string describing the parameters, or null if this
- * object has not been initialized.
- */
- public final String toString() {
- if (this.initialized == false) {
- return null;
- }
- return paramSpi.engineToString();
- }
- }
-