/*
 * Class: KeyPairSpec
 *
 * Author: Alex Usun
 *
 * Date: 1/9/2005
 */
package com.jscape.inet.ssh.util.keyreader;

import com.jscape.util.Assert;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

/**
 * A specification of the key pair material that constitutes a cryptographic key pair.
 *
 * @author Alex Usun
 * @version 1.0
 * @see <a href="KeyPairSpec.java.html">Source code</a>
 */
public class KeyPairSpec {

    private String algorithm;
    private KeySpec publicSpec = null;
    private KeySpec privateSpec = null;

    /**
     * Creates a new specification.
     *
     * @param algorithm   algorithm
     * @param publicSpec  public key specification
     * @param privateSpec private key specification
     */
    public KeyPairSpec(String algorithm, KeySpec publicSpec, KeySpec privateSpec) {
        Assert.isValidString(algorithm);
        Assert.notNull(publicSpec);
        Assert.notNull(privateSpec);

        this.algorithm = algorithm;
        this.publicSpec = publicSpec;
        this.privateSpec = privateSpec;
    }

    /**
     * Returns specification algorithm.
     *
     * @return specification algorithm
     */
    public String getAlgorithm() {
        return this.algorithm;
    }

    /**
     * Returns public key specification.
     *
     * @return public key specification
     */
    public KeySpec getPublicSpec() {
        return this.publicSpec;
    }

    /**
     * Returns private key specification.
     *
     * @return private key specification
     */
    public KeySpec getPrivateSpec() {
        return this.privateSpec;
    }

    /**
     * Converts specification to the concrete key pair.
     *
     * @return converted key pair
     *
     * @throws FormatException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    public KeyPair toKeyPair()
            throws FormatException, NoSuchAlgorithmException, InvalidKeySpecException {
        KeyFactory factory = KeyFactory.getInstance(this.algorithm);
        PublicKey pubKey = factory.generatePublic(this.publicSpec);
        PrivateKey prvKey = factory.generatePrivate(this.privateSpec);

        return new KeyPair(pubKey, prvKey);
    }

}
