Exemplo n.º 1
0
        /// <summary>
        /// Initialises a new instance of the FlowbishStream class.
        /// </summary>
        /// <param name="stream">The base stream.</param>
        /// <param name="mode">The mode of operation.</param>
        /// <param name="key">The key used for encryption (usually the filename).</param>
        /// <param name="leaveOpen">Whether to leave the file open after disposal.</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="InvalidDataException"/>
        /// <exception cref="IOException"/>
        /// <exception cref="ObjectDisposedException"/>
        public FlowbishStream(Stream stream, FlowbishStreamMode mode, string key, bool leaveOpen)
        {
            if (stream == null || key == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            _key           = key;
            this.leaveOpen = leaveOpen;
            switch (mode)
            {
            case FlowbishStreamMode.Decipher:
                if (!stream.CanRead)
                {
                    throw new ArgumentException(Strings.NotSupported_UnreadableStream, nameof(stream));
                }
                if (!stream.CanSeek)
                {
                    throw new ArgumentException(Strings.NotSupported_UnseekableStream, nameof(stream));
                }
                _box    = new FlowbishBox(ToUIntArray(key + secret, FlowbishBox.keyUIntLength));
                _stream = stream;
                _mode   = FlowbishStreamMode.Decipher;
                ReadHeader();
                _avail_in      = 0;
                _buffer_offset = 0;
                InitialiseBuffer();
                break;

            case FlowbishStreamMode.Encipher:
                if (!stream.CanWrite)
                {
                    throw new ArgumentException(Strings.NotSupported_UnwritableStream, nameof(stream));
                }
                _box           = new FlowbishBox(ToUIntArray(key + secret, FlowbishBox.keyUIntLength));
                _stream        = stream;
                _mode          = FlowbishStreamMode.Encipher;
                _avail_out     = 0;
                _buffer_offset = 0;
                _position      = 0;
                _length        = 0;
                try
                {
                    WriteHeader();
                }
                catch (EncoderFallbackException)
                {
                    throw new ArgumentException(nameof(key));
                }
                break;

            default:
                throw new ArgumentException(Strings.ArgumentOutOfRangeException_Enum, nameof(mode));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initialises a new instance of the FlowbishStream class.
 /// </summary>
 /// <param name="stream">The base stream.</param>
 /// <param name="mode">The mode of operation.</param>
 /// <param name="key">The key used for encryption (usually the filename).</param>
 /// <exception cref="ArgumentNullException"/>
 /// <exception cref="ArgumentException"/>
 ///
 public FlowbishStream(Stream stream, FlowbishStreamMode mode, string key) : this(stream, mode, key, false)
 {
 }