Example #1
0
        /// <summary>
        /// Assembles the current outgoing packet from the given payload data, crypto session, auth session and partial header.
        /// </summary>
        /// <param name="payloadData">The raw payload data.</param>
        /// <param name="crypto">The crypto session being used.</param>
        /// <param name="auth">The auth session being used.</param>
        /// <param name="partialHeader">The outer packet header, except for the 2 first bytes (packet size).</param>
        /// <returns>The assembled packet, ready to be sent.</returns>
        private static byte[] AssemblePacket(byte[] payloadData, CryptoSession crypto, AuthSession auth, byte[] partialHeader)
        {
            PayloadReader reader = new PayloadReader(payloadData, 6);

            if (reader.ReadBool())
            {
                payloadData = PayloadCompression.Compress(payloadData);
            }

            FixSize(payloadData);

            byte[] iv            = Generate.IV();
            byte[] encryptedData = crypto.EncryptPacket(payloadData, iv);

            HeaderWriter headerWriter = new HeaderWriter();

            headerWriter.WriteData((short)(16 + encryptedData.Length + 10));
            headerWriter.WriteData(partialHeader);
            headerWriter.WriteData(iv);

            byte[] partialBuffer = Sequence.Concat(headerWriter.GetHeader(), encryptedData);
            byte[] hmac          = auth.GetHmac(partialBuffer);

            return(Sequence.Concat(partialBuffer, hmac));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of InPacket from the given packet buffer and crypto session.
        /// </summary>
        /// <param name="packetBuffer">The packet buffer the way it was received.</param>
        /// <param name="crypto">The current crypto session.</param>
        public InPacket(byte[] packetBuffer, CryptoSession crypto)
        {
            ParseHeader(packetBuffer);
            PayloadData = ProcessData(packetBuffer, crypto);

            PayloadReader reader = new PayloadReader(PayloadData);

            Id = reader.ReadInt16();
        }
Example #3
0
        /// <summary>
        /// Processes the current packet data and returns the raw packet payload.
        /// </summary>
        /// <param name="packetBuffer">The packet buffer the way it was received.</param>
        /// <param name="crypto">The current crypto session.</param>
        /// <returns>The packet payload (decrypted and uncompressed).</returns>
        private static byte[] ProcessData(byte[] packetBuffer, CryptoSession crypto)
        {
            byte[] data = crypto.DecryptPacket(packetBuffer);

            PayloadReader reader = new PayloadReader(data, 6);

            if (reader.ReadBool())
            {
                data = PayloadCompression.Decompress(data);
            }
            return(data);
        }