internal static byte[] Encode(PacketHeader header, ReadOnlySpan <byte> encrypted) { var bHeader = header.ToByteArray(); var len = HeaderOffset + bHeader.Length + encrypted.Length; Span <byte> data = stackalloc byte[len]; data[0] = 0xAB; data[1] = 0xCD; data[2] = 0x00; data[3] = 0x01; var(packetLength, bodyLength) = EncodeLengths(bHeader.Length, encrypted.Length); Copy(packetLength, 0, data, 4, packetLength.Length); Copy(bodyLength, 0, data, 8, bodyLength.Length); Copy(bHeader, 0, data, HeaderOffset, bHeader.Length); Copy(encrypted, 0, data, HeaderOffset + bHeader.Length, encrypted.Length); //Dump(data); //Decode<UpstreamPayload>(data, key, key, out _); return(data.ToArray()); }
public static byte[] Encode(PacketHeader header, ByteString body, string key) { var bHeader = header.ToByteArray(); var encrypt = Encrypt(key, body); var len = HeaderOffset + bHeader.Length + encrypt.Length; Span <byte> data = stackalloc byte[len]; data[0] = 0xAB; data[1] = 0xCD; data[2] = 0x00; data[3] = 0x01; var(packetLength, bodyLength) = EncodeLengths(bHeader.Length, encrypt.Length); Copy(packetLength, 0, data, 4, packetLength.Length); Copy(bodyLength, 0, data, 8, bodyLength.Length); Copy(bHeader, 0, data, HeaderOffset, bHeader.Length); Copy(encrypt, 0, data, HeaderOffset + bHeader.Length, encrypt.Length); return(data.ToArray()); }