Skip to content

Instantly share code, notes, and snippets.

@israellot
israellot / crc32.cs
Last active September 25, 2022 17:55
Simple Big-endian Checksum
// Compute a 32-bit big-endian checksum on the N-byte buffer. If the
// buffer is not a multiple of 4 bytes length, compute the sum that would
// have occurred if the buffer was padded with zeros to the next multiple
// of four bytes.
public static uint Checksum(ReadOnlySpan<byte> arr)
{
if (arr.Length == 0) return 0;
uint sum0 = 0, sum1 = 0, sum2 = 0, sum = 0, N = (uint)arr.Length;
int z = 0;