Computes the CRC32 checksum of a file using the data array The actual CRC32 algorithm is described in RFC 1952 (GZIP file format specification version 4.3), this is also the specification for the algorithm used in java.util.zip. - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:CRC

Description

Computes the CRC32 checksum of a file using the data array The actual CRC32 algorithm is described in RFC 1952 (GZIP file format specification version 4.3), this is also the specification for the algorithm used in java.util.zip.

Demo Code


using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.IO;/*  w ww .  j a va2s . c  o m*/
using System.Configuration;
using System.Data;
using System;

public class Main{
        /// <summary>
    /// Computes the CRC32 checksum of a file using the data array
    /// The actual CRC32 algorithm is described in RFC 1952
    /// (GZIP file format specification version 4.3), this is also
    /// the specification for the algorithm used in java.util.zip.
    /// </summary>
    /// <param name="data">the array of data</param>
    /// <returns>the string form of the calculated CRC32 checksum</returns>
    public static string ComputeCRC32(byte [] data)
    {
        CRC_1952 crc = new CRC_1952();
        ulong csum = 0L;
        try
        {
            sbyte[] sdata = new sbyte[data.Length];
            for (int i = 0; i < data.Length; i++)
                sdata[i] = (sbyte)data[i];

            csum = crc.updateCRC(csum, data,data.Length);
            return csum.ToString("X");
        }
        catch (IOException ex)
        {
            throw new IOException("Exception thrown computing CRC32 checksum using the data array", ex);
        }
    }
        /// <summary>
    /// Computes the CRC32 checksum of a file using the file path. 
    /// The actual CRC32 algorithm is described in RFC 1952
    /// (GZIP file format specification version 4.3), this is also
    /// the specification for the algorithm used in java.util.zip.
    /// </summary>
    /// <param name="filePath">the physical path of the file on the ESS</param>
    /// <returns>the string form of the calculated CRC32 checksum</returns>
    public static string ComputeCRC32(string filePath)
    {
        CRC_1952 crc = new CRC_1952();

        FileStream fs = null;
        ulong csum = 0L;
        try
        {
            fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] buf = new byte[512];
            int byteCount = fs.Read(buf, 0, 512);
            while (byteCount > 0)
            {
               csum =  crc.updateCRC(csum, buf, byteCount);
                byteCount = fs.Read(buf, 0, 512);
            }
            
            fs.Close();
            return csum.ToString("X");
        }
        catch (IOException ex)
        {
            throw new IOException("Exception thrown computing CRC32 checksum using the file path", ex);
        }
    }
}

Related Tutorials