Authentication Stream : Stream « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » StreamScreenshots 
Authentication Stream
  
// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SlimCode.Utils
{
  internal class AuthenticationStream : Stream
  {
    public AuthenticationStreamStream innerStream, byte[] authenticationKey, AuthenticationType authenticationType, bool writing )
    {
      ifinnerStream == null )
        throw new ArgumentNullException"innerStream" );

      ifwriting && !innerStream.CanWrite )
        throw new ArgumentException"Cannot write to inner stream.""innerStream" );

      if!writing && !innerStream.CanRead )
        throw new ArgumentException"Cannot read from inner stream.""innerStream" );

      ifauthenticationKey == null )
        throw new ArgumentNullException"authenticationKey" );

      ifauthenticationKey.Length < 16 )
        throw new ArgumentException"The authentication key cannot be less than 16 bytes." );

      switchauthenticationType )
      {
        case AuthenticationType.MD5:
          m_hmac = new HMACMD5authenticationKey );
          break;

        case AuthenticationType.SHA1:
          m_hmac = new HMACSHA1authenticationKey, true );
          break;

        case AuthenticationType.SHA256:
          m_hmac = new HMACSHA256authenticationKey );
          break;

        case AuthenticationType.SHA384:
          m_hmac = new HMACSHA384authenticationKey );
          break;

        case AuthenticationType.SHA512:
          m_hmac = new HMACSHA512authenticationKey );
          break;

        default:
          throw new ArgumentException"Unknown authentication type.""authenticationType" );
      }

      m_innerStream = innerStream;
      m_writing = writing;
    }

    public override bool CanRead
    {
      get return !m_writing; }
    }

    public override bool CanSeek
    {
      get return false}
    }

    public override bool CanWrite
    {
      get return m_writing; }
    }

    public override void Flush()
    {
      m_innerStream.Flush();
    }

    public override long Length
    {
      get return m_innerStream.Length; }
    }

    public override long Position
    {
      get return m_innerStream.Position; }
      set throw new NotSupportedException"This stream does not support seeking." )}
    }

    public override int Readbyte[] buffer, int offset, int count )
    {
      ifm_writing )
        throw new IOException"Cannot read from this stream. It is open for writing." );

      ifbuffer == null )
        throw new ArgumentNullException"buffer" );

      if( ( offset < || offset >= buffer.Length ) )
        throw new ArgumentOutOfRangeException"offset" );

      ifcount < )
        throw new ArgumentOutOfRangeException"count" );

      ifoffset + count > buffer.Length )
        throw new ArgumentOutOfRangeException"count" );

      ifcount == )
        return 0;

      int read = 0;
      int hashSize = m_hmac.HashSize / 8;

      ifm_bufferAvailable > hashSize )
      {
        read = count < m_bufferAvailable - hashSize ) ) count m_bufferAvailable - hashSize );

        Array.Copym_buffer, 0, buffer, offset, read );

        m_hmac.TransformBlockbuffer, offset, read, buffer, offset );

        offset += read;
        count -= read;
        
        m_bufferAvailable -= read;

        Array.Copym_buffer, read, m_buffer, 0, m_bufferAvailable );
      }

      ifcount > )
      {
        int innerRead = m_innerStream.Readm_buffer, m_bufferAvailable, m_buffer.Length - m_bufferAvailable );

        ifinnerRead > )
        {
          m_bufferAvailable += innerRead;

          read += this.Readbuffer, offset, count );
        }
      }

      return read;
    }

    public override long Seeklong offset, SeekOrigin origin )
    {
      throw new NotSupportedException"This stream does not support seeking." );
    }

    public override void SetLengthlong value )
    {
      throw new NotSupportedException"This stream does not support seeking." );
    }

    public override void Writebyte[] buffer, int offset, int count )
    {
      if!m_writing )
        throw new IOException"Cannot write to this stream. It is open for reading." );

      ifbuffer == null )
        throw new ArgumentNullException"buffer" );

      if( ( offset < || offset >= buffer.Length ) )
        throw new ArgumentOutOfRangeException"offset" );

      ifcount < )
        throw new ArgumentOutOfRangeException"count" );

      ifoffset + count > buffer.Length )
        throw new ArgumentOutOfRangeException"count" );

      ifcount > )
      {
        m_hmac.TransformBlockbuffer, offset, count, buffer, offset );

        m_innerStream.Writebuffer, offset, count );
      }
    }

    protected override void Disposebool disposing )
    {
      try
      {
        ifdisposing )
        {
          m_hmac.TransformFinalBlocknew byte]0);

          byte[] hash = m_hmac.Hash;

          System.Diagnostics.Trace.Asserthash.Length == m_hmac.HashSize / ) );

          ifm_writing )
          {
            System.Diagnostics.Trace.Asserthash.Length == m_hmac.HashSize / ) );

            m_innerStream.Writehash, 0, hash.Length );
          }
          else
          {
            ifhash.Length != m_bufferAvailable )
              throw new IOException"Invalid authentication hash length." );

            forint i = 0; i < hash.Length; i++ )
            {
              ifhash!= m_buffer] )
                throw new IOException"Invalid authentication hash signature." );
            }
          }

          m_innerStream.Close();
          ( ( IDisposable )m_hmac ).Dispose();
        }

        m_innerStream = null;
        m_hmac = null;
      }
      finally
      {
        base.Disposedisposing );
      }
    }

    private byte[] m_buffer = new byte32768 ];
    private int m_bufferAvailable; // = 0

    private Stream m_innerStream; // = null
    private bool m_writing; // = false
    private HMAC m_hmac; // = null
  }
}

   
    
  
Related examples in the same category
1.Stream.CanRead indicates whether the current stream supports reading.
2.Stream.CanWrite Property indicates whether the current stream supports writing.
3.Stream.CopyTo reads all the bytes from the current stream and writes them to the destination stream.
4.Stream.Read reads a sequence of bytes and advances the position
5.Create StreamWriter class for the specified stream, using the specified encoding and the default buffer size.
6.Create StreamWriter from FileStream
7.Create StreamWriter with encoding
8.Create StreamWriter from file name
9.UTF8 encoding StreamWriter
10.Create UTF8 encoding StreamWriter from File name
11.Set StreamWriter buffer size
12.Writes a subarray of characters to the stream.
13.Create a new instance of the StreamReader class for the specified stream.
14.Gets the current character encoding that the current StreamReader object is using.
15.Returns the next available character but does not consume it.
16.Reads a maximum of count characters from the current stream into buffer, beginning at index.
17.Reads the next character from the input stream and advances the character position by one character.
18.Read a single character
19.Reads a line of characters from the current stream and returns the data as a string.
20.Reads the stream from the current position to the end of the stream.
21.Copy Stream
22.Copy one Stream to another Stream
23.Outputs data from a read stream to a newly created file
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.