Calculates a 32 bit Cyclic Redundancy Checksum (CRC) using the same polynomial used by Zip. : CRC « Security « 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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Security » CRCScreenshots 
Calculates a 32 bit Cyclic Redundancy Checksum (CRC) using the same polynomial used by Zip.
 
  
// Crc32.cs
//
// Implements the CRC algorithm, which is used in zip files.  The zip format calls for
// the zipfile to contain a CRC for the unencrypted byte stream of each file.
//
// It is based on example source code published at
//    http://www.vbaccelerator.com/home/net/code/libraries/CRC32/Crc32_zip_CRC32_CRC32_cs.asp
//
// This implementation adds a tweak of that code for use within zip creation.  While
// computing the CRC we also compress the byte stream, in the same read loop. This
// avoids the need to read through the uncompressed stream twice - once to computer CRC
// and another time to compress.
//
//
// Thu, 30 Mar 2006  13:58
// 

using System;

namespace ionic.utils.zip
{
  /// <summary>
  /// Calculates a 32bit Cyclic Redundancy Checksum (CRC) using the
  /// same polynomial used by Zip.
  /// </summary>
  public class CRC32
  {
    private UInt32[] crc32Table;
    private const int BUFFER_SIZE = 8192;

    private Int32 _TotalBytesRead = 0;
    public Int32 TotalBytesRead
    {
      get
      {
        return _TotalBytesRead;
      }
    }

    /// <summary>
    /// Returns the CRC32 for the specified stream.
    /// </summary>
    /// <param name="input">The stream over which to calculate the CRC32</param>
    /// <returns>the CRC32 calculation</returns>
    public UInt32 GetCrc32(System.IO.Stream input)
    {
      return GetCrc32AndCopy(input, null);
    }

    /// <summary>
    /// Returns the CRC32 for the specified stream, and writes the input into the output stream.
    /// </summary>
    /// <param name="input">The stream over which to calculate the CRC32</param>
    /// <param name="output">The stream into which to deflate the input</param>
    /// <returns>the CRC32 calculation</returns>
    public UInt32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
    {
      unchecked
      {
        UInt32 crc32Result;
        crc32Result = 0xFFFFFFFF;
        byte[] buffer = new byte[BUFFER_SIZE];
        int readSize = BUFFER_SIZE;

        _TotalBytesRead = 0;
        int count = input.Read(buffer, 0, readSize);
        if (output != nulloutput.Write(buffer, 0, count);
        _TotalBytesRead += count;
        while (count > 0)
        {
          for (int i = 0; i < count; i++)
          {
            crc32Result = ((crc32Result>> 8^ crc32Table[(buffer[i]) ((crc32Result0x000000FF)];
          }
          count = input.Read(buffer, 0, readSize);
          if (output != nulloutput.Write(buffer, 0, count);
          _TotalBytesRead += count;

        }

        return ~crc32Result;
      }
    }


    /// <summary>
    /// Construct an instance of the CRC32 class, pre-initialising the table
    /// for speed of lookup.
    /// </summary>
    public CRC32()
    {
      unchecked
      {
        // This is the official polynomial used by CRC32 in PKZip.
        // Often the polynomial is shown reversed as 0x04C11DB7.
        UInt32 dwPolynomial = 0xEDB88320;
        UInt32 i, j;

        crc32Table = new UInt32[256];

        UInt32 dwCrc;
        for (i = 0; i < 256; i++)
        {
          dwCrc = i;
          for (j = 8; j > 0; j--)
          {
            if ((dwCrc & 1== 1)
            {
              dwCrc = (dwCrc >> 1^ dwPolynomial;
            }
            else
            {
              dwCrc >>= 1;
            }
          }
          crc32Table[i= dwCrc;
        }
      }
    }
  }
}

   
  
Related examples in the same category
1.This class is responsible for calculating the crc32 value of a byte buffer and returning it as a hexadecimal string.
2.A utility class to compute CRC32
3.Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
4.Do CRC32 hashing.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.