001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.packets.login;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.server.netty.Codec;
022import net.tridentsdk.server.netty.packet.OutPacket;
023import net.tridentsdk.server.netty.packet.PacketDirection;
024
025/**
026 * @author The TridentSDK Team
027 */
028public class PacketLoginOutEncryptionRequest extends OutPacket {
029    /**
030     * Length of the public key
031     */
032    protected short keyLength;
033    /**
034     * Length of the verification token
035     */
036    protected short tokenLength;
037
038    /**
039     * Public Key used during the LOGIN stage, reference PacketLoginInEncrytionResponse regarding encryption for the
040     * LOGIN stage
041     *
042     * @see PacketLoginInEncryptionResponse
043     */
044    protected byte[] publicKey;
045    /**
046     * Verification token used throughout the LOGIN stage
047     */
048    protected byte[] verifyToken;
049
050    @Override
051    public int id() {
052        return 0x01;
053    }
054
055    @Override
056    public PacketDirection direction() {
057        return PacketDirection.OUT;
058    }
059
060    @Override
061    public void encode(ByteBuf buf) {
062        this.keyLength = (short) this.publicKey.length;
063
064        this.tokenLength = (short) 4;
065
066        Codec.writeString(buf, "");
067
068        Codec.writeVarInt32(buf, (int) this.keyLength);
069        buf.writeBytes(this.publicKey);
070
071        Codec.writeVarInt32(buf, (int) this.tokenLength);
072        buf.writeBytes(this.verifyToken);
073    }
074
075    /**
076     * Gets the length of the encryption key
077     *
078     * @return the encrypted key length
079     */
080    public short keyLength() {
081        return this.keyLength;
082    }
083
084    /**
085     * Gets the length of the client token
086     *
087     * @return the client token length
088     */
089    public short tokenLength() {
090        return this.tokenLength;
091    }
092
093    /**
094     * The public encryption key
095     *
096     * @return the encryption key
097     */
098    public byte[] publicKey() {
099        return this.publicKey;
100    }
101
102    /**
103     * The verification token
104     *
105     * @return the verification token
106     */
107    public byte[] verifyToken() {
108        return this.verifyToken;
109    }
110}