package common.packet;

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

import common.network.IClientLoginHandler;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.EncryptUtil;

public class RPacketResponse implements Packet<IClientLoginHandler> {
	private byte[] token = new byte[0];

	public RPacketResponse() {
	}

	public RPacketResponse(PrivateKey key, byte[] token) {
		this.token = EncryptUtil.createSignature(key, token);
	}

	public void readPacketData(PacketBuffer buf) throws IOException {
		this.token = buf.readByteArray();
	}

	public void writePacketData(PacketBuffer buf) throws IOException {
		buf.writeByteArray(this.token);
	}

	public void processPacket(IClientLoginHandler handler) {
		handler.handleResponse(this);
	}

	public boolean verifyToken(PublicKey key, byte[] token) {
		return EncryptUtil.verifySignature(key, token, this.token);
	}
}
