Exemplo n.º 1
0
        public static BlockStream Create(Stream baseStream, Stream wrapper, Action wrapperFlush, CloseOptions closeOption)
        {
            var block = new BlockStream()
            {
                _baseStream      = baseStream,
                _pointerPosition = baseStream.Length,
                _wrapperFlush    = wrapperFlush,
                _closeOption     = closeOption,
                _wrapper         = wrapper
            };

            baseStream.WriteInt64(0);
            return(block);
        }
Exemplo n.º 2
0
        public static BlockStream Read(Stream baseStream, Stream wrapper, Action wrapperFlush, CloseOptions closeOption)
        {
            var block = new BlockStream()
            {
                _readMode         = true,
                _baseStream       = baseStream,
                _pointerPosition  = baseStream.Position,
                _wrapperFlush     = wrapperFlush,
                _closeOption      = closeOption,
                _wrapper          = wrapper,
                _blockEndPosition = baseStream.ReadInt64(),
            };

            block._length = block._blockEndPosition - sizeof(Int64) - block._pointerPosition;
            return(block);
        }
Exemplo n.º 3
0
        public static Stream CreateBlock(this Stream baseStream, BlockOptions options, byte[] key, byte[] iv)
        {
            var compression = options.HasFlag(BlockOptions.Compression);
            var encryption  = options.HasFlag(BlockOptions.Encryption);

            if (compression)
            {
                if (encryption) //compression & encryption
                {
                    var gzipStream = new GZipStream(baseStream, CompressionLevel.Optimal, true);
                    var rijAlg     = Rijndael.Create();
                    rijAlg.Key = key;
                    rijAlg.IV  = iv;
                    var encryptor        = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
                    var encryptionStream = new CryptoStream(gzipStream, encryptor, CryptoStreamMode.Write);
                    return(BlockStream.Create(baseStream, encryptionStream, () =>
                    {
                        encryptionStream.FlushFinalBlock();
                        gzipStream.Close();
                    }, CloseOptions.Flush));
                }
                else //compression
                {
                    var gzipStream = new GZipStream(baseStream, CompressionLevel.Optimal, true);
                    return(BlockStream.Create(baseStream, gzipStream, null, CloseOptions.Flush | CloseOptions.CloseWrapper));
                }
            }
            else
            {
                if (encryption) //encryption
                {
                    var rijAlg = Rijndael.Create();
                    rijAlg.Key = key;
                    rijAlg.IV  = iv;
                    var encryptor        = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
                    var encryptionStream = new CryptoStream(baseStream, encryptor, CryptoStreamMode.Write);
                    return(BlockStream.Create(baseStream, encryptionStream, () =>
                    {
                        encryptionStream.FlushFinalBlock();
                    }, CloseOptions.Flush));
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
        }