//Helper method to compress an array of bytes
            static ArraySegment <byte> CompressBuffer(ArraySegment <byte> buffer, BufferManager bufferManager, int messageOffset, CompressionAlgorithm compressionAlgorithm)
            {
                buffer = Cryptographer.EncryptBuffer(buffer, bufferManager, messageOffset);

                var memoryStream = new MemoryStream();

                using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip ?
                                                 (Stream) new GZipStream(memoryStream, CompressionMode.Compress, true) :
                                                 (Stream) new DeflateStream(memoryStream, CompressionMode.Compress, true))
                {
                    compressedStream.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                byte[] compressedBytes = memoryStream.ToArray();
                int    totalLength     = messageOffset + compressedBytes.Length;

                byte[] bufferedBytes = bufferManager.TakeBuffer(totalLength);

                Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset, compressedBytes.Length);

                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(bufferedBytes, messageOffset, compressedBytes.Length);

                return(buffer);
            }
Ejemplo n.º 2
0
        public static ArraySegment <byte> EncryptBuffer(ArraySegment <byte> buffer, BufferManager bufferManager, int messageOffset, string elementToEncrypt = "s:Envelope")
        {
            var xmlDoc = new XmlDocument();

            using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
            {
                xmlDoc.Load(memoryStream);
            }

            Cryptographer.Encrypt(xmlDoc, elementToEncrypt);
            byte[] encryptedBytes = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
            byte[] bufferedBytes  = bufferManager.TakeBuffer(encryptedBytes.Length);
            Array.Copy(encryptedBytes, 0, bufferedBytes, 0, encryptedBytes.Length);
            bufferManager.ReturnBuffer(buffer.Array);

            var byteArray = new ArraySegment <byte>(bufferedBytes, messageOffset, encryptedBytes.Length);

            return(byteArray);
        }
Ejemplo n.º 3
0
        public static ArraySegment <byte> DecryptBuffer(ArraySegment <byte> buffer, BufferManager bufferManager)
        {
            var xmlDoc = new XmlDocument();

            using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
            {
                xmlDoc.Load(memoryStream);
            }

            Cryptographer.Decrypt(xmlDoc);
            byte[] decryptedBytes = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);

            byte[] bufferedBytes = bufferManager.TakeBuffer(decryptedBytes.Length);
            Array.Copy(decryptedBytes, 0, bufferedBytes, 0, decryptedBytes.Length);
            bufferManager.ReturnBuffer(buffer.Array);
            var byteArray = new ArraySegment <byte>(bufferedBytes, 0, decryptedBytes.Length);

            string s = Encoding.UTF8.GetString(byteArray.Array, byteArray.Offset, byteArray.Count);

            return(byteArray);
        }
            //Helper method to decompress an array of bytes
            static ArraySegment <byte> DecompressBuffer(ArraySegment <byte> buffer, BufferManager bufferManager, CompressionAlgorithm compressionAlgorithm)
            {
                var       memoryStream       = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
                var       decompressedStream = new MemoryStream();
                int       totalRead          = 0;
                const int blockSize          = 1024;

                byte[] tempBuffer = bufferManager.TakeBuffer(blockSize);
                using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip ?
                                                 (Stream) new GZipStream(memoryStream, CompressionMode.Decompress) :
                                                 (Stream) new DeflateStream(memoryStream, CompressionMode.Decompress))
                {
                    while (true)
                    {
                        int bytesRead = compressedStream.Read(tempBuffer, 0, blockSize);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        decompressedStream.Write(tempBuffer, 0, bytesRead);
                        totalRead += bytesRead;
                    }
                }
                bufferManager.ReturnBuffer(tempBuffer);

                byte[] decompressedBytes   = decompressedStream.ToArray();
                byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset);
                Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset);
                Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);

                buffer = new ArraySegment <byte>(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);
                buffer = Cryptographer.DecryptBuffer(buffer, bufferManager);
                bufferManager.ReturnBuffer(buffer.Array);

                return(buffer);
            }