Bzip2 checksum algorithm : Checksum « 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 » ChecksumScreenshots 
Bzip2 checksum algorithm
 

// StrangeCRC.cs - computes a crc used in the bziplib
//
// Copyright (C) 2001 Mike Krueger
//
// This file was translated from java, it was part of the GNU Classpath
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library.  Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
// 
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent
// modules, and to copy and distribute the resulting executable under
// terms of your choice, provided that you also meet, for each linked
// independent module, the terms and conditions of the license of that
// module.  An independent module is a module which is not derived from
// or based on this library.  If you modify this library, you may extend
// this exception to your version of the library, but you are not
// obligated to do so.  If you do not wish to do so, delete this
// exception statement from your version.

using System;

namespace ICSharpCode.SharpZipLib.Checksums 
{
  /// <summary>
  /// Bzip2 checksum algorithm
  /// </summary>
  public class StrangeCRC
  {
    readonly static uint[] crc32Table = {
      0x000000000x04c11db70x09823b6e0x0d4326d9,
      0x130476dc0x17c56b6b0x1a864db20x1e475005,
      0x2608edb80x22c9f00f0x2f8ad6d60x2b4bcb61,
      0x350c9b640x31cd86d30x3c8ea00a0x384fbdbd,
      0x4c11db700x48d0c6c70x4593e01e0x4152fda9,
      0x5f15adac0x5bd4b01b0x569796c20x52568b75,
      0x6a1936c80x6ed82b7f0x639b0da60x675a1011,
      0x791d40140x7ddc5da30x709f7b7a0x745e66cd,
      0x9823b6e00x9ce2ab570x91a18d8e0x95609039,
      0x8b27c03c0x8fe6dd8b0x82a5fb520x8664e6e5,
      0xbe2b5b580xbaea46ef0xb7a960360xb3687d81,
      0xad2f2d840xa9ee30330xa4ad16ea0xa06c0b5d,
      0xd4326d900xd0f370270xddb056fe0xd9714b49,
      0xc7361b4c0xc3f706fb0xceb420220xca753d95,
      0xf23a80280xf6fb9d9f0xfbb8bb460xff79a6f1,
      0xe13ef6f40xe5ffeb430xe8bccd9a0xec7dd02d,
      0x348670770x30476dc00x3d044b190x39c556ae,
      0x278206ab0x23431b1c0x2e003dc50x2ac12072,
      0x128e9dcf0x164f80780x1b0ca6a10x1fcdbb16,
      0x018aeb130x054bf6a40x0808d07d0x0cc9cdca,
      0x7897ab070x7c56b6b00x711590690x75d48dde,
      0x6b93dddb0x6f52c06c0x6211e6b50x66d0fb02,
      0x5e9f46bf0x5a5e5b080x571d7dd10x53dc6066,
      0x4d9b30630x495a2dd40x44190b0d0x40d816ba,
      0xaca5c6970xa864db200xa527fdf90xa1e6e04e,
      0xbfa1b04b0xbb60adfc0xb6238b250xb2e29692,
      0x8aad2b2f0x8e6c36980x832f10410x87ee0df6,
      0x99a95df30x9d6840440x902b669d0x94ea7b2a,
      0xe0b41de70xe47500500xe93626890xedf73b3e,
      0xf3b06b3b0xf771768c0xfa3250550xfef34de2,
      0xc6bcf05f0xc27dede80xcf3ecb310xcbffd686,
      0xd5b886830xd1799b340xdc3abded0xd8fba05a,
      0x690ce0ee0x6dcdfd590x608edb800x644fc637,
      0x7a0896320x7ec98b850x738aad5c0x774bb0eb,
      0x4f040d560x4bc510e10x468636380x42472b8f,
      0x5c007b8a0x58c1663d0x558240e40x51435d53,
      0x251d3b9e0x21dc26290x2c9f00f00x285e1d47,
      0x36194d420x32d850f50x3f9b762c0x3b5a6b9b,
      0x0315d6260x07d4cb910x0a97ed480x0e56f0ff,
      0x1011a0fa0x14d0bd4d0x19939b940x1d528623,
      0xf12f560e0xf5ee4bb90xf8ad6d600xfc6c70d7,
      0xe22b20d20xe6ea3d650xeba91bbc0xef68060b,
      0xd727bbb60xd3e6a6010xdea580d80xda649d6f,
      0xc423cd6a0xc0e2d0dd0xcda1f6040xc960ebb3,
      0xbd3e8d7e0xb9ff90c90xb4bcb6100xb07daba7,
      0xae3afba20xaafbe6150xa7b8c0cc0xa379dd7b,
      0x9b3660c60x9ff77d710x92b45ba80x9675461f,
      0x8832161a0x8cf30bad0x81b02d740x857130c3,
      0x5d8a90990x594b8d2e0x5408abf70x50c9b640,
      0x4e8ee6450x4a4ffbf20x470cdd2b0x43cdc09c,
      0x7b827d210x7f4360960x7200464f0x76c15bf8,
      0x68860bfd0x6c47164a0x610430930x65c52d24,
      0x119b4be90x155a565e0x181970870x1cd86d30,
      0x029f3d350x065e20820x0b1d065b0x0fdc1bec,
      0x3793a6510x3352bbe60x3e119d3f0x3ad08088,
      0x2497d08d0x2056cd3a0x2d15ebe30x29d4f654,
      0xc5a926790xc1683bce0xcc2b1d170xc8ea00a0,
      0xd6ad50a50xd26c4d120xdf2f6bcb0xdbee767c,
      0xe3a1cbc10xe760d6760xea23f0af0xeee2ed18,
      0xf0a5bd1d0xf464a0aa0xf92786730xfde69bc4,
      0x89b8fd090x8d79e0be0x803ac6670x84fbdbd0,
      0x9abc8bd50x9e7d96620x933eb0bb0x97ffad0c,
      0xafb010b10xab710d060xa6322bdf0xa2f33668,
      0xbcb4666d0xb8757bda0xb5365d030xb1f740b4
    };
    
    int globalCrc;

    /// <summary>
    /// Initialise a default instance of <see cref="StrangeCRC"></see>
    /// </summary>  
    public StrangeCRC() 
    {
      Reset();
    }

    /// <summary>
    /// Reset the state of Crc.
    /// </summary>
    public void Reset()
    {
      globalCrc = -1;
    }

    /// <summary>
    /// Get the current Crc value.
    /// </summary>
    public long Value {
      get {
        return ~globalCrc;
      }
    }
    
    /// <summary>
    /// Update the Crc value.
    /// </summary>
    /// <param name="value">data update is based on</param>
    public void Update(int value)
    {
      int temp = (globalCrc >> 24^ value;
      if (temp < 0) {
        temp = 256 + temp;
      }
      globalCrc = unchecked((int)((globalCrc << 8^ crc32Table[temp]));
    }

    /// <summary>
    /// Update Crc based on a block of data
    /// </summary>
    /// <param name="buffer">The buffer containing data to update the crc with.</param>
    public void Update(byte[] buffer)
    {
      if (buffer == null) {
        throw new ArgumentNullException("buffer");
      }
      
      Update(buffer, 0, buffer.Length);
    }
    
    /// <summary>
    /// Update Crc based on a portion of a block of data
    /// </summary>
    /// <param name="buffer">block of data</param>
    /// <param name="offset">index of first byte to use</param>
    /// <param name="count">number of bytes to use</param>
    public void Update(byte[] buffer, int offset, int count)
    {
      if (buffer == null) {
        throw new ArgumentNullException("buffer");
      }
      
      if offset < )
      {
#if NETCF_1_0
        throw new ArgumentOutOfRangeException("offset");
#else
        throw new ArgumentOutOfRangeException("offset""cannot be less than zero");
#endif        
      }

      if count < )
      {
#if NETCF_1_0
        throw new ArgumentOutOfRangeException("count");
#else
        throw new ArgumentOutOfRangeException("count""cannot be less than zero");
#endif
      }

      if offset + count > buffer.Length )
      {
        throw new ArgumentOutOfRangeException("count");
      }
      
      for (int i = 0; i < count; ++i) {
        Update(buffer[offset++]);
      }
    }
  }
}

   
  
Related examples in the same category
1.Get File Checksum
2.Get Byte Checksum
3.Computes Adler32 checksum for a stream of data
4.Compute a checksum for a given string.
5.Return the checksum of the buffer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.