package common.packet;

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

import common.network.ILoginHandler;
import common.network.IPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.EncryptUtil;

public class LPacketPubkey implements Packet<ILoginHandler> {
    private String user;
    private String access;
	private PublicKey key;

	public LPacketPubkey() {
	}

	public LPacketPubkey(String user, String access, PublicKey key) {
        this.user = user;
        this.access = access;
		this.key = key;
	}

	public final void readPacketData(PacketBuffer buf) throws IOException {
        this.user = buf.readString(IPlayer.MAX_USER_LENGTH);
        this.access = buf.readString(IPlayer.MAX_PASS_LENGTH);
		this.key = EncryptUtil.decodePublicKey(buf.readByteArray());
	}

	public final void writePacketData(PacketBuffer buf) throws IOException {
        buf.writeString(this.user);
        buf.writeString(this.access);
		buf.writeByteArray(this.key.getEncoded());
	}

	public void processPacket(ILoginHandler handler) {
		handler.processPubkey(this);
	}

	public String getUser() {
		return this.user;
	}

	public String getAccess() {
		return this.access;
	}

	public PublicKey getKey() {
		return this.key;
	}
}
