Class ChecksumFletcher32
Fletcher-32 checksum algorithm implementation. Produces a 32-bit checksum by maintaining two running sums over 16-bit words derived from the input data, using a modulus of 65535 (0xFFFF).
Implements
Inherited Members
Namespace: Scylla.Core.Util.Checksum
Assembly: ScyllaCore.dll
Syntax
public sealed class ChecksumFletcher32 : IChecksum
Remarks
Algorithm details:
- Input word width: 16 bits (byte pairs in little-endian order).
- Both accumulators (
c0andc1) are kept modulo0xFFFF(65535). - Initial values:
c0 = 0,c1 = 0. - Output: 32-bit value
(c1 << 16) | c0. - Odd-length inputs: the trailing byte is treated as the low byte of a 16-bit word padded with zero. Padding is deferred until Value is read, so streaming updates correctly handle boundaries that split a word across chunks.
Like ChecksumAdler32, Fletcher-32 defers modulo reduction to block boundaries for throughput. The block size for Fletcher-32 is 360 16-bit words (720 bytes), which is the largest block that keeps both accumulators safely within 32-bit unsigned arithmetic.
Comparison with Adler-32: Both algorithms share a similar two-accumulator structure and similar performance. Fletcher-32 uses a modulus of 65535 (not a prime), while Adler-32 uses the prime 65521. Adler-32 is typically preferred for zlib compatibility; Fletcher-32 is preferred in embedded systems and network protocols (e.g., ATM AAL5, SCTP) where its particular error-detection properties are specified by the standard.
Security note: Fletcher-32 is NOT cryptographically secure. For security-sensitive hashing use the Scylla Crypto module instead.
Properties
Value
Gets the current Fletcher-32 checksum value as the standard 32-bit composite
(c1 << 16) | c0.
Declaration
public long Value { get; }
Property Value
| Type | Description |
|---|---|
| long | A non-negative |
Methods
Reset()
Resets the Fletcher-32 checksum to its initial state
(c0 = 0, c1 = 0, no pending byte), discarding any previously
processed data.
Declaration
public void Reset()
Remarks
After calling Reset, Value returns 0 and any pending
odd byte from the previous computation is cleared. Use this to reuse the same
instance for multiple independent checksum operations without allocating a new object.
Update(byte[])
Updates the Fletcher-32 checksum by incorporating all bytes in
buffer.
Declaration
public void Update(byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | The byte array to process. If |
Update(byte[], int, int)
Updates the Fletcher-32 checksum by incorporating count bytes
from buffer starting at offset.
Declaration
public void Update(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | The source byte array. If |
| int | offset | Zero-based index of the first byte to read in |
| int | count | Number of bytes to process. Must not cause |
Remarks
Bytes are combined into 16-bit little-endian words before accumulation. If a
previous call left a pending odd byte (stored in _pendingByte), the
first byte of this call is paired with it to form a complete word before
continuing with the rest of the input.
Words are processed in blocks of up to NMAX (360) words, with modulo
reduction applied once per block. If the final count is odd, the trailing byte
is stored in _pendingByte for pairing on the next call or for zero-padding
when Value is read.
Multiple calls accumulate into the same state; the final result is equivalent to a single call with the fully concatenated input.