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

CrcStream stream checksum calculator

By , 8 Oct 2005
 

Introduction

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.

License

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
Member
The cows are here to take me home now...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionchecksum?memberChristoph Ruegg8 Oct '05 - 13:39 
thanks for your contribution! Just a detail: strictly speaking, CRC is not a checksum algorithm. While checksums (used in internet protocols like IPv4) are one class of error detection codes, CRC is another one (used in LAN protocols like Ethernet). Parity checks form yet another such class...
AnswerYep, checksummemberreinux8 Oct '05 - 14:11 
Hmm... what is it called then?
 
From what I know browsing around, it's called a checksum algorithm.
 
Here's the definition at Wikipedia[^]:
 
"A cyclic redundancy check (CRC) is a type of hash function used to produce a checksum, which is a small number of bits, from a large block of data, such as a packet of network traffic or a block of a computer file, in order to detect errors in transmission or storage. "
GeneralRe: Yep, checksummemberChristoph Ruegg8 Oct '05 - 15:01 
Thinking about it, I don't remember a better name for the generated bits. I checked some technical papers: those bits are often called "CRC bits" or just the "CRC". Like you mentioned, "checksum" is very popular, too, maybe due to the lack of a better name. Again Wikipedia: Checksum[^]: "This article is about checksums calculated using addition. The term 'checksum' is sometimes used in a more general sense to refer to any kind of redundancy check."
 
As everyone understands what you mean by the term "checksum": no matter, ignore my post above Wink | ;) (don't want to be captious)
 
btw: I'd call CRC an error detection code.
GeneralRe: Yep, checksummemberreinux9 Oct '05 - 21:02 
Smile | :)

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

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