| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- //
- // System.Security.Cryptography CryptoStream.cs
- //
- // Authors:
- // Thomas Neidhart ([email protected])
- // Sebastien Pouliot ([email protected])
- //
- // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
- //
- using System;
- using System.IO;
- namespace System.Security.Cryptography {
- public class CryptoStream : Stream {
- private Stream _stream;
- private ICryptoTransform _transform;
- private CryptoStreamMode _mode;
- private byte[] work;
- private int workPos;
- private bool disposed;
-
- public CryptoStream (Stream stream, ICryptoTransform transform, CryptoStreamMode mode)
- {
- _stream = stream;
- _transform = transform;
- _mode = mode;
- disposed = false;
- if (mode == CryptoStreamMode.Read)
- work = new byte [transform.InputBlockSize];
- else if (mode == CryptoStreamMode.Write)
- work = new byte [transform.OutputBlockSize];
- workPos = 0;
- }
- ~CryptoStream ()
- {
- Dispose (false);
- }
-
- public override bool CanRead {
- get { return (_mode == CryptoStreamMode.Read); }
- }
- public override bool CanSeek {
- get { return false; }
- }
- public override bool CanWrite {
- get { return (_mode == CryptoStreamMode.Write); }
- }
-
- public override long Length {
- get {
- throw new NotSupportedException ("Length property not supported by CryptoStream");
- }
- }
- public override long Position {
- get {
- throw new NotSupportedException ("Position property not supported by CryptoStream");
- }
- set {
- throw new NotSupportedException ("Position property not supported by CryptoStream");
- }
- }
- public void Clear ()
- {
- Dispose (true);
- }
- public override void Close ()
- {
- if (_mode != CryptoStreamMode.Write)
- throw new NotSupportedException ();
- byte[] finalBuffer = _transform.TransformFinalBlock (work, 0, workPos);
- if (_stream != null) {
- _stream.Write (finalBuffer, 0, finalBuffer.Length);
- _stream.Close ();
- }
- }
- public override int Read (byte[] buffer, int offset, int count)
- {
- if (_mode != CryptoStreamMode.Read)
- throw new NotSupportedException ();
- if ((offset < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if (offset + count > buffer.Length)
- throw new ArgumentException ();
- // reached end of stream ?
- if (_stream.Position == _stream.Length)
- return 0;
- int result = 0;
- int bufferPos = offset;
- while (count > 0) {
- int len = Math.Min (work.Length - workPos, count);
- _stream.Read (work, workPos, len);
- workPos += len;
- count -= len;
- if (_stream.Position == _stream.Length) {
- byte[] input = _transform.TransformFinalBlock (work, 0, work.Length);
- Array.Copy (input, 0, buffer, bufferPos, input.Length);
- result += input.Length;
- break;
- } else if (workPos == work.Length) {
- workPos = 0;
- result += _transform.TransformBlock (work, 0, work.Length, buffer, bufferPos);
- }
- bufferPos += len;
- }
- return result;
- }
- public override void Write (byte[] buffer, int offset, int count)
- {
- if (_mode != CryptoStreamMode.Write)
- throw new NotSupportedException ();
- if ((offset < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if (offset + count > buffer.Length)
- throw new ArgumentException ();
- int bufferPos = offset;
- while (count > 0) {
- int len = Math.Min (work.Length - workPos, count);
- Array.Copy (buffer, bufferPos, work, workPos, len);
- bufferPos += len;
- workPos += len;
- count -= len;
- if (workPos == work.Length) {
- workPos = 0;
- byte[] output = new byte[_transform.OutputBlockSize];
- _transform.TransformBlock (work, 0, work.Length, output, 0);
- _stream.Write (output, 0, output.Length);
- }
- }
- }
- public override void Flush ()
- {
- if (_mode != CryptoStreamMode.Write)
- throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
- if (_stream != null)
- _stream.Flush ();
- }
- public void FlushFinalBlock ()
- {
- if (_mode != CryptoStreamMode.Write)
- throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
- if (_stream != null) {
- _stream.Flush ();
- Close ();
- }
- }
- public override long Seek (long offset, SeekOrigin origin)
- {
- throw new NotSupportedException ("cannot Seek a CryptoStream");
- }
-
- // LAMESPEC: Exception NotSupportedException not documented
- public override void SetLength (long value)
- {
- throw new NotSupportedException ("cannot SetLength a CryptoStream");
- }
- protected virtual void Dispose (bool disposing)
- {
- if (!disposed) {
- if (_stream != null)
- _stream.Close ();
- }
- }
-
- } // CryptoStream
-
- } // System.Security.Cryptography
|