private static byte[] CreateSftpNameResponse(uint responseId, Encoding encoding, params string[] names)
        {
            var namesAndAttributes = new List <byte>();

            foreach (var name in names)
            {
                var nameBytes       = encoding.GetBytes(name);
                var attributesBytes = SftpFileAttributes.Empty.GetBytes();

                namesAndAttributes.AddRange((((uint)nameBytes.Length).GetBytes())); // filename length
                namesAndAttributes.AddRange(nameBytes);                             // filename
                namesAndAttributes.AddRange(((uint)0).GetBytes());                  // longname length
                namesAndAttributes.AddRange(attributesBytes);                       // attributes
            }

            var namesAndAttributesBytes = namesAndAttributes.ToArray();

            var sshDataStream = new SshDataStream(4 + 1 + 4 + 4 + namesAndAttributesBytes.Length);

            sshDataStream.Write((uint)sshDataStream.Capacity - 4);
            sshDataStream.WriteByte((byte)SftpMessageTypes.Name);
            sshDataStream.Write(responseId);
            sshDataStream.Write((uint)names.Length);
            sshDataStream.Write(namesAndAttributesBytes, 0, namesAndAttributesBytes.Length);
            return(sshDataStream.ToArray());
        }
        public void Load_ShouldThrowNotSupportedExceptionWhenChannelTypeIsNotSupported()
        {
            var localChannelNumber = (uint)_random.Next(0, int.MaxValue);
            var initialWindowSize  = (uint)_random.Next(0, int.MaxValue);
            var maximumPacketSize  = (uint)_random.Next(0, int.MaxValue);
            var channelName        = "dunno_" + _random.Next().ToString(CultureInfo.InvariantCulture);
            var channelType        = _ascii.GetBytes(channelName);

            var sshDataStream = new SshDataStream(1 + 4 + channelType.Length + 4 + 4 + 4);

            sshDataStream.WriteByte(ChannelOpenMessage.MessageNumber);
            sshDataStream.Write((uint)channelType.Length);
            sshDataStream.Write(channelType, 0, channelType.Length);
            sshDataStream.Write(localChannelNumber);
            sshDataStream.Write(initialWindowSize);
            sshDataStream.Write(maximumPacketSize);
            var bytes  = sshDataStream.ToArray();
            var target = new ChannelOpenMessage();

            try
            {
                target.Load(bytes);
                Assert.Fail();
            }
            catch (NotSupportedException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual(string.Format("Channel type '{0}' is not supported.", channelName), ex.Message);
            }
        }
Example #3
0
        public void Load()
        {
            var target          = new SftpAttrsResponse(_protocolVersion);
            var attributes      = CreateSftpFileAttributes();
            var attributesBytes = attributes.GetBytes();

            var sshDataStream = new SshDataStream(4 + 1 + 4 + attributesBytes.Length);

            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte)SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(attributesBytes, 0, attributesBytes.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Attributes);
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Attrs, target.SftpMessageType);

            // check attributes in detail
            Assert.AreEqual(attributes.GroupId, target.Attributes.GroupId);
            Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
            Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
            Assert.AreEqual(attributes.UserId, target.Attributes.UserId);
        }
Example #4
0
        public byte[] Build()
        {
            var sshDataStream = new SshDataStream(0);

            sshDataStream.WriteByte(KeyExchangeDhGroupExchangeGroup.MessageNumber);
            sshDataStream.Write(_safePrime);
            sshDataStream.Write(_subGroup);
            return(sshDataStream.ToArray());
        }
Example #5
0
        public void GetReply_StatVfsReplyInfo()
        {
            var bsize   = (ulong)_random.Next(0, int.MaxValue);
            var frsize  = (ulong)_random.Next(0, int.MaxValue);
            var blocks  = (ulong)_random.Next(0, int.MaxValue);
            var bfree   = (ulong)_random.Next(0, int.MaxValue);
            var bavail  = (ulong)_random.Next(0, int.MaxValue);
            var files   = (ulong)_random.Next(0, int.MaxValue);
            var ffree   = (ulong)_random.Next(0, int.MaxValue);
            var favail  = (ulong)_random.Next(0, int.MaxValue);
            var sid     = (ulong)_random.Next(0, int.MaxValue);
            var namemax = (ulong)_random.Next(0, int.MaxValue);

            var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);

            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte)SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(bsize);
            sshDataStream.Write(frsize);
            sshDataStream.Write(blocks);
            sshDataStream.Write(bfree);
            sshDataStream.Write(bavail);
            sshDataStream.Write(files);
            sshDataStream.Write(ffree);
            sshDataStream.Write(favail);
            sshDataStream.Write(sid);
            sshDataStream.Write((ulong)0x2);
            sshDataStream.Write(namemax);

            var sshData = sshDataStream.ToArray();

            var target = new SftpExtendedReplyResponse(_protocolVersion);

            target.Load(sshData, 5, sshData.Length - 5);

            var reply = target.GetReply <StatVfsReplyInfo>();

            Assert.IsNotNull(reply);

            var information = reply.Information;

            Assert.IsNotNull(information);
            Assert.AreEqual(bavail, information.AvailableBlocks);
            Assert.AreEqual(favail, information.AvailableNodes);
            Assert.AreEqual(frsize, information.BlockSize);
            Assert.AreEqual(bsize, information.FileSystemBlockSize);
            Assert.AreEqual(bfree, information.FreeBlocks);
            Assert.AreEqual(ffree, information.FreeNodes);
            Assert.IsFalse(information.IsReadOnly);
            Assert.AreEqual(namemax, information.MaxNameLenght);
            Assert.AreEqual(sid, information.Sid);
            Assert.IsFalse(information.SupportsSetUid);
            Assert.AreEqual(blocks, information.TotalBlocks);
            Assert.AreEqual(files, information.TotalNodes);
        }
Example #6
0
        private byte[] BuildExtensions()
        {
            var sshDataStream = new SshDataStream(0);

            foreach (var extensionPair in _extensions)
            {
                sshDataStream.Write(extensionPair.Key, Encoding.ASCII);
                sshDataStream.Write(extensionPair.Value, Encoding.ASCII);
            }
            return(sshDataStream.ToArray());
        }
Example #7
0
        public byte[] Build()
        {
            var extensions = BuildExtensions();

            var sshDataStream = new SshDataStream(4 + 1 + 4 + extensions.Length);

            sshDataStream.Write((uint)sshDataStream.Capacity - 4);
            sshDataStream.WriteByte((byte)SftpMessageTypes.Version);
            sshDataStream.Write(_version);
            sshDataStream.Write(extensions, 0, extensions.Length);
            return(sshDataStream.ToArray());
        }
            public byte[] Build()
            {
                var serviceName = _serviceName.ToArray();

                var sshDataStream = new SshDataStream(4 + 1 + 1 + 4 + serviceName.Length);

                sshDataStream.Write((uint)(sshDataStream.Capacity - 4)); // packet length
                sshDataStream.WriteByte(0);                              // padding length
                sshDataStream.WriteByte(ServiceAcceptMessage.MessageNumber);
                sshDataStream.WriteBinary(serviceName);
                return(sshDataStream.ToArray());
            }
        private static byte[] CreateSftpDataResponse(uint responseId, byte[] data)
        {
            var sshDataStream = new SshDataStream(4 + 1 + 4 + 4 + data.Length + 1);

            sshDataStream.Write((uint)sshDataStream.Capacity - 4);
            sshDataStream.WriteByte((byte)SftpMessageTypes.Data);
            sshDataStream.Write(responseId);
            sshDataStream.Write((uint)data.Length);
            sshDataStream.Write(data, 0, data.Length);
            sshDataStream.WriteByte(1); // EOF
            return(sshDataStream.ToArray());
        }
        public byte[] Build()
        {
            var sshDataStream = new SshDataStream(0);

            sshDataStream.WriteByte(KeyExchangeDhGroupExchangeReply.MessageNumber);
            sshDataStream.Write((uint)(4 + _hostKeyAlgorithm.Length + _hostKeys.Length));
            sshDataStream.Write((uint)_hostKeyAlgorithm.Length);
            sshDataStream.Write(_hostKeyAlgorithm, 0, _hostKeyAlgorithm.Length);
            sshDataStream.Write(_hostKeys, 0, _hostKeys.Length);
            sshDataStream.Write(_f);
            sshDataStream.WriteBinary(_signature);
            return(sshDataStream.ToArray());
        }
Example #11
0
        public void Load()
        {
            var target = new SftpExtendedReplyResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4);

            sshDataStream.Write(_responseId);

            target.Load(sshDataStream.ToArray());

            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.ExtendedReply, target.SftpMessageType);
        }
        public KeyExchangeDhGroupExchangeReplyBuilder WithHostKey(string hostKeyAlgorithm, params BigInteger[] hostKeys)
        {
            _hostKeyAlgorithm = Encoding.UTF8.GetBytes(hostKeyAlgorithm);

            var sshDataStream = new SshDataStream(0);

            foreach (var hostKey in hostKeys)
            {
                sshDataStream.Write(hostKey);
            }
            _hostKeys = sshDataStream.ToArray();

            return(this);
        }
Example #13
0
        public void Load()
        {
            var target = new SftpExtendedReplyResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + 1 + 4);

            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte)SftpMessageTypes.ExtendedReply);
            sshDataStream.Write(_responseId);

            target.Load(sshDataStream.ToArray());

            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.ExtendedReply, target.SftpMessageType);
        }
Example #14
0
        private static byte[] CreatePacketForUnsupportedMessageType()
        {
            byte messageType      = 255;
            byte messageLength    = 1;
            byte paddingLength    = 10;
            var  packetDataLength = (uint)messageLength + paddingLength + 1;

            var sshDataStream = new SshDataStream(4 + 1 + messageLength + paddingLength);

            sshDataStream.Write(packetDataLength);
            sshDataStream.WriteByte(paddingLength);
            sshDataStream.WriteByte(messageType);
            sshDataStream.Write(new byte[paddingLength]);

            return(sshDataStream.ToArray());
        }
Example #15
0
        public void Load_ShouldIgnoreDataWhenItsLengthIsGreatherThanItsActualBytes()
        {
            var ssh = new SshDataStream(1);

            ssh.WriteByte(2);       // Type
            ssh.Write(5u);          // Data length
            ssh.Write(new byte[3]); // Data

            var ignoreMessageBytes = ssh.ToArray();

            var ignoreMessage = new IgnoreMessage();

            ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1);
            Assert.IsNotNull(ignoreMessage.Data);
            Assert.AreEqual(0, ignoreMessage.Data.Length);
        }
Example #16
0
        public void Load()
        {
            var target = new SftpHandleResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + _handle.Length);

            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint)_handle.Length);
            sshDataStream.Write(_handle, 0, _handle.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Handle);
            Assert.IsTrue(target.Handle.SequenceEqual(_handle));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Handle, target.SftpMessageType);
        }
Example #17
0
        public void Load_Data()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(8);

            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();

            request.Load(sshData);

            Assert.AreEqual(one, request.ValueOne);
            Assert.AreEqual(two, request.ValueTwo);
        }
Example #18
0
        public void Load()
        {
            var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);

            sshDataStream.Write(_responseId);
            sshDataStream.Write(_bsize);
            sshDataStream.Write(_frsize);
            sshDataStream.Write(_blocks);
            sshDataStream.Write(_bfree);
            sshDataStream.Write(_bavail);
            sshDataStream.Write(_files);
            sshDataStream.Write(_ffree);
            sshDataStream.Write(_favail);
            sshDataStream.Write(_sid);
            sshDataStream.Write((ulong)0x1);
            sshDataStream.Write(_namemax);

            var extendedReplyResponse = new SftpExtendedReplyResponse(SftpSession.MaximumSupportedVersion);

            extendedReplyResponse.Load(sshDataStream.ToArray());

            Assert.AreEqual(_responseId, extendedReplyResponse.ResponseId);

            var target = extendedReplyResponse.GetReply <StatVfsReplyInfo>();

            Assert.IsNotNull(target.Information);

            var information = target.Information;

            Assert.AreEqual(_bavail, information.AvailableBlocks);
            Assert.AreEqual(_favail, information.AvailableNodes);
            Assert.AreEqual(_frsize, information.BlockSize);
            Assert.AreEqual(_bsize, information.FileSystemBlockSize);
            Assert.AreEqual(_bfree, information.FreeBlocks);
            Assert.AreEqual(_ffree, information.FreeNodes);
            Assert.IsTrue(information.IsReadOnly);
            Assert.AreEqual(_namemax, information.MaxNameLenght);
            Assert.AreEqual(_sid, information.Sid);
            Assert.IsTrue(information.SupportsSetUid);
            Assert.AreEqual(_blocks, information.TotalBlocks);
            Assert.AreEqual(_files, information.TotalNodes);
        }
Example #19
0
        public void Load()
        {
            var target = new StatVfsReplyInfo();

#if TUNING
            var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);
            sshDataStream.Write((uint)(sshDataStream.Capacity - 4));
#else
            var sshDataStream = new SshDataStream(1 + 4 + 88);
#endif
            sshDataStream.WriteByte((byte)SftpMessageTypes.ExtendedReply);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(_bsize);
            sshDataStream.Write(_frsize);
            sshDataStream.Write(_blocks);
            sshDataStream.Write(_bfree);
            sshDataStream.Write(_bavail);
            sshDataStream.Write(_files);
            sshDataStream.Write(_ffree);
            sshDataStream.Write(_favail);
            sshDataStream.Write(_sid);
            sshDataStream.Write((ulong)0x1);
            sshDataStream.Write(_namemax);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Information);

            var information = target.Information;
            Assert.AreEqual(_bavail, information.AvailableBlocks);
            Assert.AreEqual(_favail, information.AvailableNodes);
            Assert.AreEqual(_frsize, information.BlockSize);
            Assert.AreEqual(_bsize, information.FileSystemBlockSize);
            Assert.AreEqual(_bfree, information.FreeBlocks);
            Assert.AreEqual(_ffree, information.FreeNodes);
            Assert.IsTrue(information.IsReadOnly);
            Assert.AreEqual(_namemax, information.MaxNameLenght);
            Assert.AreEqual(_sid, information.Sid);
            Assert.IsTrue(information.SupportsSetUid);
            Assert.AreEqual(_blocks, information.TotalBlocks);
            Assert.AreEqual(_files, information.TotalNodes);
        }
            public byte[] Build()
            {
                var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);

                sshDataStream.Write((uint)sshDataStream.Capacity - 4);
                sshDataStream.WriteByte((byte)SftpMessageTypes.ExtendedReply);
                sshDataStream.Write(_responseId);
                sshDataStream.Write(_bsize);
                sshDataStream.Write(_frsize);
                sshDataStream.Write(_blocks);
                sshDataStream.Write(_bfree);
                sshDataStream.Write(_bavail);
                sshDataStream.Write(_files);
                sshDataStream.Write(_ffree);
                sshDataStream.Write(_favail);
                sshDataStream.Write(_sid);
                sshDataStream.Write(_flag);
                sshDataStream.Write(_namemax);
                return(sshDataStream.ToArray());
            }
        public void Load()
        {
            var target = new SftpDataResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + 1 + 4 + _data.Length);

            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte)SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint)_data.Length);
            sshDataStream.Write(_data, 0, _data.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Data);
            Assert.IsTrue(target.Data.SequenceEqual(_data));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Data, target.SftpMessageType);
        }
        public void Load()
        {
            var target = new SftpDataResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + _data.Length);

            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint)_data.Length);
            sshDataStream.Write(_data, 0, _data.Length);

            var sshData = sshDataStream.ToArray();

            target.Load(sshData);

            Assert.IsNotNull(target.Data);
            Assert.IsTrue(target.Data.SequenceEqual(_data));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Data, target.SftpMessageType);
        }
Example #23
0
        public void OfType()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(8);

            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();

            request.Load(sshData);

            var reply = request.OfType <ReplySshData>();

            Assert.IsNotNull(reply);
            Assert.AreEqual(one, reply.ValueOne);
        }
Example #24
0
        public void Load_ShouldThrowNotSupportedExceptionWhenDataLengthIsGreaterThanInt32MaxValue()
        {
            var ssh = new SshDataStream(1);

            ssh.WriteByte(2);         // Type
            ssh.Write(uint.MaxValue); // Data length
            ssh.Write(new byte[3]);

            var ignoreMessageBytes = ssh.ToArray();
            var ignoreMessage      = new IgnoreMessage();

            try
            {
                ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1);
                Assert.Fail();
            }
            catch (NotSupportedException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue), ex.Message);
            }
        }
Example #25
0
        public void Load_DataAndOffsetAndCount()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(11);

            sshDataStream.WriteByte(0x05);
            sshDataStream.WriteByte(0x07);
            sshDataStream.WriteByte(0x0f);
            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();

            request.Load(sshData, 3, sshData.Length - 3);

            Assert.AreEqual(one, request.ValueOne);
            Assert.AreEqual(two, request.ValueTwo);
        }
Example #26
0
        public void OfType_LoadWithOffset()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(11);

            sshDataStream.WriteByte(0x05);
            sshDataStream.WriteByte(0x07);
            sshDataStream.WriteByte(0x0f);
            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();

            request.Load(sshData, 3, sshData.Length - 3);
            var reply = request.OfType <ReplySshData>();

            Assert.IsNotNull(reply);
            Assert.AreEqual(one, reply.ValueOne);
        }
Example #27
0
        public void Load()
        {
            var target = new SftpHandleResponse(_protocolVersion);

#if TUNING
            var sshDataStream = new SshDataStream(4 + 1 + 4 + _handle.Length);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
#else
            var sshDataStream = new SshDataStream(1 + 4 + _handle.Length);
#endif
            sshDataStream.WriteByte((byte)SftpMessageTypes.Handle);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint)_handle.Length);
            sshDataStream.Write(_handle, 0, _handle.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Handle);
            Assert.IsTrue(target.Handle.SequenceEqual(_handle));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Handle, target.SftpMessageType);
        }
        /// <summary>
        /// Returns a byte array representing the current <see cref="SftpFileAttributes"/>.
        /// </summary>
        /// <returns>
        /// A byte array representing the current <see cref="SftpFileAttributes"/>.
        /// </returns>
        public byte[] GetBytes()
        {
            var stream = new SshDataStream(4);

            uint flag = 0;

            if (IsSizeChanged && IsRegularFile)
            {
                flag |= 0x00000001;
            }

            if (IsUserIdChanged || IsGroupIdChanged)
            {
                flag |= 0x00000002;
            }

            if (IsPermissionsChanged)
            {
                flag |= 0x00000004;
            }

            if (IsLastAccessTimeChanged || IsLastWriteTimeChanged)
            {
                flag |= 0x00000008;
            }

            if (IsExtensionsChanged)
            {
                flag |= 0x80000000;
            }

            stream.Write(flag);

            if (IsSizeChanged && IsRegularFile)
            {
                stream.Write((ulong)Size);
            }

            if (IsUserIdChanged || IsGroupIdChanged)
            {
                stream.Write((uint)UserId);
                stream.Write((uint)GroupId);
            }

            if (IsPermissionsChanged)
            {
                stream.Write(Permissions);
            }

            if (IsLastAccessTimeChanged || IsLastWriteTimeChanged)
            {
                var time = (uint)(LastAccessTime.ToFileTime() / 10000000 - 11644473600);
                stream.Write(time);
                time = (uint)(LastWriteTime.ToFileTime() / 10000000 - 11644473600);
                stream.Write(time);
            }

            if (IsExtensionsChanged)
            {
                foreach (var item in Extensions)
                {
                    // TODO: we write as ASCII but read as UTF8 !!!

                    stream.Write(item.Key, SshData.Ascii);
                    stream.Write(item.Value, SshData.Ascii);
                }
            }

            return(stream.ToArray());
        }
Example #29
0
        internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor)
        {
            const int outboundPacketSequenceSize = 4;

            var messageLength = BufferCapacity;

            SshDataStream sshDataStream;

            if (messageLength == -1 || compressor != null)
            {
                sshDataStream = new SshDataStream(DefaultCapacity);

                // skip:
                // * 4 bytes for the outbound packet sequence
                // * 4 bytes for the packet data length
                // * one byte for the packet padding length
                sshDataStream.Seek(outboundPacketSequenceSize + 4 + 1, SeekOrigin.Begin);

                if (compressor != null)
                {
                    // obtain uncompressed message payload
                    var uncompressedDataStream = new SshDataStream(messageLength != -1 ? messageLength : DefaultCapacity);
                    WriteBytes(uncompressedDataStream);

                    // compress message payload
                    var compressedMessageData = compressor.Compress(uncompressedDataStream.ToArray());

                    // add compressed message payload
                    sshDataStream.Write(compressedMessageData, 0, compressedMessageData.Length);
                }
                else
                {
                    // add message payload
                    WriteBytes(sshDataStream);
                }

                messageLength = (int)sshDataStream.Length - (outboundPacketSequenceSize + 4 + 1);

                var packetLength = messageLength + 4 + 1;

                // determine the padding length
                var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);

                // add padding bytes
                var paddingBytes = new byte[paddingLength];
                HashAlgorithmFactory.GenerateRandom(paddingBytes);
                sshDataStream.Write(paddingBytes, 0, paddingLength);

                var packetDataLength = GetPacketDataLength(messageLength, paddingLength);

                // skip bytes for outbound packet sequence
                sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);

                // add packet data length
                sshDataStream.Write(packetDataLength.GetBytes(), 0, 4);

                //  add packet padding length
                sshDataStream.WriteByte(paddingLength);
            }
            else
            {
                var packetLength = messageLength + 4 + 1;

                // determine the padding length
                var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);

                var packetDataLength = GetPacketDataLength(messageLength, paddingLength);

                // lets construct an SSH data stream of the exact size required
                sshDataStream = new SshDataStream(packetLength + paddingLength + outboundPacketSequenceSize);

                // skip bytes for outbound packet sequenceSize
                sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);

                // add packet data length
                sshDataStream.Write(packetDataLength.GetBytes(), 0, 4);

                //  add packet padding length
                sshDataStream.WriteByte(paddingLength);

                // add message payload
                WriteBytes(sshDataStream);

                // add padding bytes
                var paddingBytes = new byte[paddingLength];
                HashAlgorithmFactory.GenerateRandom(paddingBytes);
                sshDataStream.Write(paddingBytes, 0, paddingLength);
            }

            return(sshDataStream.ToArray());
        }