/*
 * Class: DSAFormat
 *
 * Author: Alex Usun
 *
 * Date: 31/8/2005
 */
package com.jscape.inet.ssh.util.keyreader.sshcom;

import com.jscape.inet.ssh.util.keyreader.KeyPairSpec;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;

/**
 * SSH.COM DSA key pair format.
 *
 * @author Alex Usun
 * @version 1.0
 * @see <a href="DSAFormat.java.html">Source code</a>
 */
public class DSAFormat
        extends SSHComFormat {

    private static final String ALGORITHM = "DSA";

    /**
     * @see com.jscape.inet.ssh.util.keyreader.sshcom.SSHComFormat#getAlgorithm()
     */
    protected String getAlgorithm() {
        return ALGORITHM;
    }

    /**
     * @see SSHComFormat#restoreKeyPair(Record)
     */
    protected KeyPairSpec restoreKeyPair(Record record)
            throws IOException {
        DataReader dataReader = new DataReader(new ByteArrayInputStream(record.getKeyPairData()));
        DataReader reader = new DataReader(new ByteArrayInputStream(dataReader.readStringAsByteArray()));

        reader.readMpint(); // 0

        BigInteger p = reader.readMpint();
        BigInteger g = reader.readMpint();
        BigInteger q = reader.readMpint();
        BigInteger y = reader.readMpint();
        BigInteger x = reader.readMpint();

        return new KeyPairSpec(ALGORITHM, new DSAPublicKeySpec(y, p, q, g),
                               new DSAPrivateKeySpec(x, p, q, g));
    }

}
