package com.jscape.inet.ssh.protocol.v2.marshaling;

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.inet.ssh.protocol.v2.marshaling.keys.EcPublicKeyCodec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgKexEcdhInit;
import com.jscape.util.io.DataCodec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.PublicKey;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class SshMsgKexEcdhInitCodec
        implements DataCodec<Message> {

    private final String curveIdentifier;
    private final Object provider;

    public SshMsgKexEcdhInitCodec(String curveIdentifier, Object provider) {
        assertNotNull(curveIdentifier);
        this.curveIdentifier = curveIdentifier;

        this.provider = provider;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        PublicKey qc = EcPublicKeyCodec.readValue(this.curveIdentifier, this.provider, in);
        return new SshMsgKexEcdhInit(qc);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgKexEcdhInit message = (SshMsgKexEcdhInit) value;
        EcPublicKeyCodec.writeValue(message.qc, out);
    }
}
