Compute a checksum for a given string. : 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.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# / C Sharp » Security » ChecksumScreenshots 
Compute a checksum for a given string.
 

// HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
using System;
using System.IO;

namespace HtmlAgilityPack
{
  /// <summary>
  /// A utility class to compute CRC32.
  /// </summary>
  public class Crc32
  {
    private uint _crc32 = 0;
    static private uint[] crc_32_tab =  // CRC polynomial 0xedb88320 
      {
        0x000000000x770730960xee0e612c0x990951ba0x076dc4190x706af48f,
        0xe963a5350x9e6495a30x0edb88320x79dcb8a40xe0d5e91e0x97d2d988,
        0x09b64c2b0x7eb17cbd0xe7b82d070x90bf1d910x1db710640x6ab020f2,
        0xf3b971480x84be41de0x1adad47d0x6ddde4eb0xf4d4b5510x83d385c7,
        0x136c98560x646ba8c00xfd62f97a0x8a65c9ec0x14015c4f0x63066cd9,
        0xfa0f3d630x8d080df50x3b6e20c80x4c69105e0xd56041e40xa2677172,
        0x3c03e4d10x4b04d4470xd20d85fd0xa50ab56b0x35b5a8fa0x42b2986c,
        0xdbbbc9d60xacbcf9400x32d86ce30x45df5c750xdcd60dcf0xabd13d59,
        0x26d930ac0x51de003a0xc8d751800xbfd061160x21b4f4b50x56b3c423,
        0xcfba95990xb8bda50f0x2802b89e0x5f0588080xc60cd9b20xb10be924,
        0x2f6f7c870x58684c110xc1611dab0xb6662d3d0x76dc41900x01db7106,
        0x98d220bc0xefd5102a0x71b185890x06b6b51f0x9fbfe4a50xe8b8d433,
        0x7807c9a20x0f00f9340x9609a88e0xe10e98180x7f6a0dbb0x086d3d2d,
        0x91646c970xe6635c010x6b6b51f40x1c6c61620x856530d80xf262004e,
        0x6c0695ed0x1b01a57b0x8208f4c10xf50fc4570x65b0d9c60x12b7e950,
        0x8bbeb8ea0xfcb9887c0x62dd1ddf0x15da2d490x8cd37cf30xfbd44c65,
        0x4db261580x3ab551ce0xa3bc00740xd4bb30e20x4adfa5410x3dd895d7,
        0xa4d1c46d0xd3d6f4fb0x4369e96a0x346ed9fc0xad6788460xda60b8d0,
        0x44042d730x33031de50xaa0a4c5f0xdd0d7cc90x5005713c0x270241aa,
        0xbe0b10100xc90c20860x5768b5250x206f85b30xb966d4090xce61e49f,
        0x5edef90e0x29d9c9980xb0d098220xc7d7a8b40x59b33d170x2eb40d81,
        0xb7bd5c3b0xc0ba6cad0xedb883200x9abfb3b60x03b6e20c0x74b1d29a,
        0xead547390x9dd277af0x04db26150x73dc16830xe3630b120x94643b84,
        0x0d6d6a3e0x7a6a5aa80xe40ecf0b0x9309ff9d0x0a00ae270x7d079eb1,
        0xf00f93440x8708a3d20x1e01f2680x6906c2fe0xf762575d0x806567cb,
        0x196c36710x6e6b06e70xfed41b760x89d32be00x10da7a5a0x67dd4acc,
        0xf9b9df6f0x8ebeeff90x17b7be430x60b08ed50xd6d6a3e80xa1d1937e,
        0x38d8c2c40x4fdff2520xd1bb67f10xa6bc57670x3fb506dd0x48b2364b,
        0xd80d2bda0xaf0a1b4c0x36034af60x41047a600xdf60efc30xa867df55,
        0x316e8eef0x4669be790xcb61b38c0xbc66831a0x256fd2a00x5268e236,
        0xcc0c77950xbb0b47030x220216b90x5505262f0xc5ba3bbe0xb2bd0b28,
        0x2bb45a920x5cb36a040xc2d7ffa70xb5d0cf310x2cd99e8b0x5bdeae1d,
        0x9b64c2b00xec63f2260x756aa39c0x026d930a0x9c0906a90xeb0e363f,
        0x720767850x050057130x95bf4a820xe2b87a140x7bb12bae0x0cb61b38,
        0x92d28e9b0xe5d5be0d0x7cdcefb70x0bdbdf210x86d3d2d40xf1d4e242,
        0x68ddb3f80x1fda836e0x81be16cd0xf6b9265b0x6fb077e10x18b74777,
        0x88085ae60xff0f6a700x66063bca0x11010b5c0x8f659eff0xf862ae69,
        0x616bffd30x166ccf450xa00ae2780xd70dd2ee0x4e0483540x3903b3c2,
        0xa76726610xd06016f70x4969474d0x3e6e77db0xaed16a4a0xd9d65adc,
        0x40df0b660x37d83bf00xa9bcae530xdebb9ec50x47b2cf7f0x30b5ffe9,
        0xbdbdf21c0xcabac28a0x53b393300x24b4a3a60xbad036050xcdd70693,
        0x54de57290x23d967bf0xb3667a2e0xc4614ab80x5d681b020x2a6f2b94,
        0xb40bbe370xc30c8ea10x5a05df1b0x2d02ef8d 
      };      

    static private uint UPDC32(byte octet, uint crc)
    {
      return (crc_32_tab[((crc)^((byte)octet)) 0xff((crc>> 8));
    }

    internal uint CheckSum
    {
      get
      {
        return _crc32;
      }
      set
      {
        _crc32 = value;
      }
    }

    internal uint AddToCRC32(int c)
    {
      return AddToCRC32((ushort)c);
    }

    internal uint AddToCRC32(ushort c)
    {
      byte lowByte, hiByte;
      lowByte = (byte)(c & 0x00ff);
      hiByte = (byte)(c >> 8);
      _crc32 = UPDC32(hiByte, _crc32);
      _crc32 = UPDC32(lowByte, _crc32);
      return ~_crc32;
    }

    /// <summary>
    /// Compute a checksum for a given string.
    /// </summary>
    /// <param name="text">The string to compute the checksum for.</param>
    /// <returns>The computed checksum.</returns>
    static public uint CRC32String(string text)
    {
      uint oldcrc32;
      oldcrc32 = 0xFFFFFFFF;        
      int len = text.Length;
      ushort uCharVal;
      byte lowByte, hiByte;
        
      for int i=0; len>0; i++)
      {
        --len;
        uCharVal = text[len];
        unchecked
        {
          lowByte = (byte)(uCharVal & 0x00ff);
          hiByte = (byte)(uCharVal >> 8);
        }
        oldcrc32 = UPDC32(hiByte, oldcrc32);
        oldcrc32 = UPDC32(lowByte, oldcrc32);
      }

      return ~oldcrc32;
    }

    /// <summary>
    /// Compute a checksum for a given array of bytes.
    /// </summary>
    /// <param name="bytes">The array of bytes to compute the checksum for.</param>
    /// <returns>The computed checksum.</returns>
    static public uint CRC32Bytes(byte[] bytes)
    {
      uint oldcrc32;
      oldcrc32 = 0xFFFFFFFF;        
      int len = bytes.Length;
        
      for int i=0; len>0; i++)
      {
        --len;
        oldcrc32 = UPDC32(bytes[len], oldcrc32);
      }
      return ~oldcrc32;
    }
  }
}

   
  
Related examples in the same category
1.Get File Checksum
2.Get Byte Checksum
3.Bzip2 checksum algorithm
4.Computes Adler32 checksum for a stream of data
5.Return the checksum of the buffer
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.