Recursive AI

Compute an incremental CRC32 checksum in C#

OneArchive is a free, open-source archive library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Checksum separate blocks without joining them into one buffer.

Terminal
dotnet add package RecursiveAI.OneCompress

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneCompress.Checksums;
 
var checksum = new Crc32();
checksum.Append("1234"u8);
checksum.Append("56789"u8);
Console.WriteLine(
    $"{checksum.GetCurrentValue():X8}");
Text
CBF43926

This is the CRC-32 of the UTF-8 bytes 123456789.

Unzip the example, then dotnet run.

The full program

The inputs are the literal byte strings 1234 and 56789. The program compares incremental, one-shot and combined checksums, then resets the state.

C#
using RecursiveAI.OneCompress.Checksums;
 
byte[] first = "1234"u8.ToArray();
byte[] second = "56789"u8.ToArray();
var checksum = new Crc32();
checksum.Append(first);
Console.WriteLine($"After first block: {checksum.GetCurrentValue():X8}");
checksum.Append(second);
uint incremental = checksum.GetCurrentValue();
uint whole = Crc32.Compute("123456789"u8);
uint combined = Crc32.Combine(Crc32.Compute(first), Crc32.Compute(second), second.Length);
Console.WriteLine($"Incremental: {incremental:X8}");
Console.WriteLine($"One-shot:    {whole:X8}");
Console.WriteLine($"Combined:    {combined:X8}");
Console.WriteLine($"All equal: {incremental == whole && whole == combined}");
checksum.Reset();
Console.WriteLine($"After reset: {checksum.GetCurrentValue():X8}");

dotnet run prints:

Text
After first block: 9BE3E0A3
Incremental: CBF43926
One-shot:    CBF43926
Combined:    CBF43926
All equal: True
After reset: 00000000

Combine throws ArgumentOutOfRangeException if the second block’s byte length is negative or exceeds 2^60.

Variations

Continue a saved, finalized value instead of keeping the struct:

C#
using RecursiveAI.OneCompress.Checksums;
 
uint value = Crc32.Compute("1234"u8);
value = Crc32.Update(value, "56789"u8);
Console.WriteLine($"{value:X8}");
Text
CBF43926

Use CRC-32C, a different polynomial, when that is what your format requires:

C#
using RecursiveAI.OneCompress.Checksums;
 
var checksum = new Crc32C();
checksum.Append("1234"u8);
checksum.Append("56789"u8);
Console.WriteLine($"{checksum.GetCurrentValue():X8}");
Text
E3069283

The short answer

For an incremental CRC32 checksum in C#, create Crc32, call Append for each block in order, and call GetCurrentValue for the result. You do not need a buffer containing the complete input. Reading the current value does not finish or disturb the running state, so you can inspect it and continue appending.

OneCompress uses reflected polynomial 0xEDB88320 with initial and final XOR 0xFFFFFFFF. The published checksum definitions give CBF43926 for 123456789 and zero for empty input. Matching the variant is essential: CRC-32C is not another spelling for this CRC-32.

How do I compute an incremental CRC32 checksum in C#?

Append exactly the bytes you mean to check, in their original order. The snippet at the top uses UTF-8 literals, so there is no uncertainty about encoding. For text from elsewhere, decide on the encoding first; the checksum operates on bytes, not C# characters.

Block boundaries do not change the result. You can pass a complete block, split it into smaller spans, or include an empty span. The full program checks that appending 1234 and 56789 yields the same value as computing over the combined literal. default(Crc32) is the empty state, and Reset returns an existing instance to that state.

The checksum is a mutable struct. Keep the same instance throughout a computation; a copy has its own state. Use a separate instance for each independent input. GetCurrentValue returns a numeric uint; the X8 format in the examples prints eight hexadecimal digits. If a file format needs four binary bytes, follow that format’s byte order rather than writing the display string.

How do I combine checksums in C#?

When two blocks have already been checksummed separately, Crc32.Combine(crcA, crcB, lengthB) gives the CRC of A followed by B. The third argument is B’s byte length, not the total length and not a count of text characters. The full program computes this alongside the incremental result so you can see the agreement.

Both arguments are finished, externally visible checksum values. Do not undo the final XOR before passing them. Combining is order-sensitive: the operation describes concatenation, so exchanging A and B generally describes different data. Both values must use the same CRC variant.

Update serves a different situation. Use it when you have A’s finished value and B’s actual bytes; it continues the computation over those bytes. Use Combine when you have both finished values and B’s length, but do not want to read B again. Neither call requires manually manipulating the internal CRC register.

Can I compute CRC64 incrementally too?

Yes. Crc64 has the same Append, GetCurrentValue, Reset, Compute, Update and Combine shape. Its definition is the reflected polynomial 0xC96C5795D7870F42, with all-ones initialization and final XOR, as used by xz. Choose it only when that definition matches the consuming format; a larger checksum does not make incompatible variants interchangeable.

The same package also exposes CRC-32C, Adler-32 and seeded xxHash structs. The documented CRC combine operation applies to Crc32, Crc32C and Crc64, not to every checksum type. A checksum detects changes; it is not authentication. If an adversary can replace both the bytes and the expected checksum, equality alone does not establish who produced the data.

All onearchive recipes