Ejemplo n.º 1
0
        public static object Decode(Type type, ReadOnlySpan <byte> bytes, string SecurityKey, string SessionKey, out PacketHeader header)
        {
            var(headerLength, payloadLength) = DecodeLengths(bytes);

            header = PacketHeader.Parser.ParseFrom(bytes.Slice(HeaderOffset, headerLength).ToArray());

            ReadOnlySpan <byte> payload = bytes.Slice(HeaderOffset + headerLength, payloadLength);

            if (header.EncryptionMode != PacketHeader.Types.EncryptionMode.KEncryptionNone)
            {
                var key = header.EncryptionMode == PacketHeader.Types.EncryptionMode.KEncryptionServiceToken ? SecurityKey : SessionKey;

                payload = Decrypt(payload, key);
            }


            if (Convert.ToUInt32(payload.Length) != header.DecodedPayloadLen)
            {
                Log.Error("Payload length does not match");
                Log.Debug("Payload Data: {Data}", Convert.ToBase64String(payload));
                return(null);
            }

            var obj = Parse(type, payload);

            return(obj);
        }