Return the checksum of the buffer : 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 
Return the checksum of the buffer
 

// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// 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 3 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, see <http://www.gnu.org/licenses/>.

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

namespace crudwork.Utilities
{
  /// <summary>
  /// Math Utility
  /// </summary>
  public static class MathUtil
  {
    /// <summary>
    /// Return the checksum of the buffer
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static long ComputeChecksum(byte[] buffer)
    {
      return ComputeChecksum(buffer, 0);
    }

    /// <summary>
    /// Return the checksum of the buffer at the given offset
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="offset"></param>
    /// <returns></returns>
    public static long ComputeChecksum(byte[] buffer, int offset)
    {
      long sum = 0;
      for (int i = 0; i < buffer.Length; i++)
      {
        sum += buffer[i(i + + offset);
      }
      return sum;
    }

    /// <summary>
    /// Return the checksum value of the given filename
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static long ComputeChecksum(string filename)
    {
      //byte[] buffer = FileUtil.ReadFile(filename, 4096);
      //return ComputeChecksum(buffer);

      long sum = 0;
      int bufSize = 4096;

      using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
      using (BinaryReader r = new BinaryReader(fs))
      {
        byte[] readChar = null;
        int offset = 0;
        
        do
        {
          readChar = r.ReadBytes(bufSize);
          sum += ComputeChecksum(readChar, offset);
          offset += readChar.Length;
        while ((readChar != null&& (readChar.Length > 0));

        r.Close();
        //fs.Close();
      }

      return sum;
    }
  }
}

   
  
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.Compute a checksum for a given string.
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.