Click here to Skip to main content
Click here to Skip to main content

CrcStream stream checksum calculator

, 8 Oct 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Make better use of time by calculating CRCs on-the-fly.


CRC (Cyclic Redundancy Check) is commonly used as a way to confirm that a file had not corrupted during download. While convenient, it takes some time to read the data off of the disk after downloading for the check. It would be convenient if applications checked the CRC on-the-fly during download, so as not to waste idle CPU time and disk read time.

Downloading is done at a relatively leisurely pace (typically anywhere between 5-300kb/s) and over a long period of time, so it makes for a great opportunity to process data without impeding performance. Although ugly and impractical for most applications (it'd be safe to assume that most users think they've "broken the intarweb" when they see a hex number), displaying the CRC to the user immediately as a download finishes can often be a well-appreciated bonus.

This class passively calculates CRCs as data passes through it, ready to be used at any time.

Using the code

To calculate the CRC of a file as it is read to the end, create a new CrcStream passing the FileStream as an argument, and use the ReadCrc property to retrieve the CRC. Be sure to use the new CrcStream instead of the file stream to read from the file; otherwise the checksum will not be calculated.

//Open a file stream, encapsulate it in CrcStream
FileStream file = new FileStream(filename, FileMode.Open);
CrcStream stream = new CrcStream(file);

//Use the file somehow -- in this case, read it as a string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

//Print the checksum
Console.WriteLine("CRC: " + stream.ReadCrc.ToString("X8"));

There are four public members in addition to the abstract Stream overrides:

  • ReadCrc - gets the checksum of the data that was read through the stream.
  • WriteCrc - gets the checksum of the data that was written to the stream.
  • ResetChecksum - resets the CRC values.
  • Stream - gets the encapsulated stream.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rei Miyasaka

Canada Canada
The cows are here to take me home now...

Comments and Discussions

GeneralSmall Enhancement Pinmembernerd_biker20-Mar-07 3:37 
Great bit of code and very useful.
I wanted to be able to read sections of a file and calculate the CRC for only that part, so I added a ReadLine method. I thought I'd post it here in case anyone else finds it useful. It's a bit of a hack but it works!
public string ReadLine()
    StringBuilder sb = new StringBuilder();
    int b;
    b = ReadByte();
    while (b >= 0 && b != '\n' && b != '\r')
        b = ReadByte();
    if (b == -1) // End of file reached
        return null;
        // Discard any EOL characters.
        int nextChar = ReadByte();
        if (nextChar != '\n' && nextChar != '\r')
            Seek(-1, SeekOrigin.Current);
        return sb.ToString();
The file I am checking contains blocks of text, separated with blank lines. I want a CRC of each block so I use the ReadLine method to read up to the next blank line, get the checksum, reset the CRC by calling ResetChecksum() and continue reading the file to the next blank line.
I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone.
-Bjarne Stroustrup

GeneralRe: Small Enhancement Pinmemberreinux20-Mar-07 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 8 Oct 2005
Article Copyright 2005 by Rei Miyasaka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid