/*
 * Class: RSAFormat
 *
 * 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.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

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

    private static final String ALGORITHM = "RSA";

    /**
     * @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()));

        BigInteger publicExponent = reader.readMpint();
        BigInteger privateExponent = reader.readMpint();
        BigInteger modulus = reader.readMpint();

        return new KeyPairSpec(ALGORITHM, new RSAPublicKeySpec(modulus, publicExponent),
                               new RSAPrivateKeySpec(modulus, privateExponent));

    }

}
