2002-10-25 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / CryptoStream.cs
index 6dad1ca628d9f25d34dde03c0d897c0d30e0aa3d..0f4df3c6cf3cbb2cf536960da74f82f0ca9ce5be 100755 (executable)
-//\r
-// System.Security.Cryptography CryptoStream.cs\r
-//\r
-// Author:\r
-//   Thomas Neidhart (tome@sbox.tugraz.at)\r
-//\r
-\r
-using System;\r
-using System.IO;\r
-\r
-namespace System.Security.Cryptography\r
-{\r
-\r
-       public class CryptoStream : Stream\r
-       {\r
-               private CryptoStreamMode _mode;\r
-               \r
-               public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) \r
-               {\r
-                       _mode = mode;\r
-               }\r
-               \r
-               public override bool CanRead\r
-               {\r
-                       get {\r
-                               switch (_mode) {\r
-                                       case CryptoStreamMode.Read:\r
-                                               return true;\r
-                                       \r
-                                       case CryptoStreamMode.Write:\r
-                                               return false;\r
-                                       \r
-                                       default:\r
-                                               return false;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               public override bool CanSeek\r
-               {\r
-                       get {\r
-                               return false;\r
-                       }\r
-               }\r
-\r
-               public override bool CanWrite\r
-               {\r
-                       get {\r
-                               switch (_mode) {\r
-                                       case CryptoStreamMode.Read:\r
-                                               return false;\r
-                                       \r
-                                       case CryptoStreamMode.Write:\r
-                                               return true;\r
-                                       \r
-                                       default:\r
-                                               return false;\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               public override long Length\r
-               {\r
-                       get {\r
-                               throw new NotSupportedException("Length property not supported by CryptoStream");\r
-                       }\r
-               }\r
-\r
-               public override long Position\r
-               {\r
-                       get {\r
-                               throw new NotSupportedException("Position property not supported by CryptoStream");\r
-                       }\r
-                       set {\r
-                               throw new NotSupportedException("Position property not supported by CryptoStream");\r
-                       }\r
-               }\r
-\r
-               [MonoTODO]\r
-               public override int Read(byte[] buffer, int offset, int count)\r
-               {\r
-                       // TODO: implement\r
-                       return 0;\r
-               }\r
-\r
-               [MonoTODO]\r
-               public override void Write(byte[] buffer, int offset, int count)\r
-               {\r
-                       // TODO: implement\r
-               }\r
-\r
-               [MonoTODO]\r
-               public override void Flush()\r
-               {\r
-                       // TODO: implement\r
-               }\r
-\r
-               [MonoTODO]\r
-               public void FlushFinalBlock()\r
-               {\r
-                       if (_mode != CryptoStreamMode.Write)\r
-                               throw new NotSupportedException("cannot flush a non-writeable CryptoStream");\r
-                       \r
-                       // TODO: implement\r
-               }\r
-\r
-               public override long Seek(long offset, SeekOrigin origin)\r
-               {\r
-                       throw new NotSupportedException("cannot seek a CryptoStream");\r
-               }\r
-               \r
-               public override void SetLength(long value)\r
-               {\r
-                       // LAMESPEC: should throw NotSupportedException like Seek??\r
-                       return;\r
-               }\r
-               \r
-       } // CryptoStream\r
-       \r
-} // System.Security.Cryptography\r
+//
+// System.Security.Cryptography CryptoStream.cs
+//
+// Author:
+//   Thomas Neidhart (tome@sbox.tugraz.at)
+//   Sebastien Pouliot (spouliot@motus.com)
+//
+// 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;
+       
+       public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) 
+       {
+               _stream = stream;
+               _transform = transform;
+               _mode = mode;
+       }
+
+       ~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);
+       }
+
+       [MonoTODO("Limited support for HMACSHA1")]
+       public override void Close () 
+       {
+               if (_mode != CryptoStreamMode.Write)
+                       throw new NotSupportedException ();
+               // TODO: limited implemention for HMACSHA1
+               byte[] buffer = new byte [0];
+               _transform.TransformFinalBlock (buffer, 0, 0);
+               if (_stream != null)
+                       _stream.Close();
+       }
+
+       [MonoTODO]
+       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 ();
+               // TODO: implement
+               return 0;
+       }
+
+       [MonoTODO("Limited support for HMACSHA1")]
+       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 ();
+               // TODO: limited implemention for HMACSHA1
+               byte[] output = new byte [count];
+               _transform.TransformBlock (buffer, offset, count, output, 0);
+       }
+
+       [MonoTODO]
+       public override void Flush ()
+       {
+               if (_mode != CryptoStreamMode.Write)
+                       throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
+               // TODO: implement
+       }
+
+       [MonoTODO]
+       public void FlushFinalBlock ()
+       {
+               if (_mode != CryptoStreamMode.Write)
+                       throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
+               // TODO: implement
+       }
+
+       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) 
+       {
+       }
+       
+} // CryptoStream
+       
+} // System.Security.Cryptography