CryptoStream.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Security.Cryptography CryptoStream.cs
  3. //
  4. // Author:
  5. // Thomas Neidhart ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. using System;
  11. using System.IO;
  12. namespace System.Security.Cryptography {
  13. public class CryptoStream : Stream {
  14. private Stream _stream;
  15. private ICryptoTransform _transform;
  16. private CryptoStreamMode _mode;
  17. public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode)
  18. {
  19. _stream = stream;
  20. _transform = transform;
  21. _mode = mode;
  22. }
  23. ~CryptoStream ()
  24. {
  25. Dispose (false);
  26. }
  27. public override bool CanRead {
  28. get { return (_mode == CryptoStreamMode.Read); }
  29. }
  30. public override bool CanSeek {
  31. get { return false; }
  32. }
  33. public override bool CanWrite {
  34. get { return (_mode == CryptoStreamMode.Write); }
  35. }
  36. public override long Length {
  37. get {
  38. throw new NotSupportedException("Length property not supported by CryptoStream");
  39. }
  40. }
  41. public override long Position {
  42. get {
  43. throw new NotSupportedException("Position property not supported by CryptoStream");
  44. }
  45. set {
  46. throw new NotSupportedException("Position property not supported by CryptoStream");
  47. }
  48. }
  49. public void Clear ()
  50. {
  51. Dispose (true);
  52. }
  53. [MonoTODO("Limited support for HMACSHA1")]
  54. public override void Close ()
  55. {
  56. if (_mode != CryptoStreamMode.Write)
  57. throw new NotSupportedException ();
  58. // TODO: limited implemention for HMACSHA1
  59. byte[] buffer = new byte [0];
  60. _transform.TransformFinalBlock (buffer, 0, 0);
  61. if (_stream != null)
  62. _stream.Close();
  63. }
  64. [MonoTODO]
  65. public override int Read (byte[] buffer, int offset, int count)
  66. {
  67. if (_mode != CryptoStreamMode.Read)
  68. throw new NotSupportedException ();
  69. if ((offset < 0) || (count < 0))
  70. throw new ArgumentOutOfRangeException ();
  71. if (offset + count > buffer.Length)
  72. throw new ArgumentException ();
  73. // TODO: implement
  74. return 0;
  75. }
  76. [MonoTODO("Limited support for HMACSHA1")]
  77. public override void Write (byte[] buffer, int offset, int count)
  78. {
  79. if (_mode != CryptoStreamMode.Write)
  80. throw new NotSupportedException ();
  81. if ((offset < 0) || (count < 0))
  82. throw new ArgumentOutOfRangeException ();
  83. if (offset + count > buffer.Length)
  84. throw new ArgumentException ();
  85. // TODO: limited implemention for HMACSHA1
  86. byte[] output = new byte [count];
  87. _transform.TransformBlock (buffer, offset, count, output, 0);
  88. }
  89. [MonoTODO]
  90. public override void Flush ()
  91. {
  92. if (_mode != CryptoStreamMode.Write)
  93. throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
  94. // TODO: implement
  95. }
  96. [MonoTODO]
  97. public void FlushFinalBlock ()
  98. {
  99. if (_mode != CryptoStreamMode.Write)
  100. throw new NotSupportedException ("cannot flush a non-writeable CryptoStream");
  101. // TODO: implement
  102. }
  103. public override long Seek (long offset, SeekOrigin origin)
  104. {
  105. throw new NotSupportedException ("cannot Seek a CryptoStream");
  106. }
  107. // LAMESPEC: Exception NotSupportedException not documented
  108. public override void SetLength (long value)
  109. {
  110. throw new NotSupportedException ("cannot SetLength a CryptoStream");
  111. }
  112. protected virtual void Dispose (bool disposing)
  113. {
  114. }
  115. } // CryptoStream
  116. } // System.Security.Cryptography