#checksum #data-integrity #crc #free #error #checking #algorithm

no-std fletcher

A dependency free implementation of the Fletcher's checksum algorithm

5 releases (1 stable)

Uses old Rust 2015

1.0.0 Sep 5, 2024
0.3.0 Dec 29, 2021
0.2.0 Oct 2, 2021
0.1.0 Apr 24, 2020
0.0.1 Aug 9, 2017

#200 in Algorithms

Download history 658/week @ 2024-08-16 659/week @ 2024-08-23 628/week @ 2024-08-30 503/week @ 2024-09-06 765/week @ 2024-09-13 1141/week @ 2024-09-20 1068/week @ 2024-09-27 832/week @ 2024-10-04 643/week @ 2024-10-11 410/week @ 2024-10-18 615/week @ 2024-10-25 736/week @ 2024-11-01 722/week @ 2024-11-08 1086/week @ 2024-11-15 1374/week @ 2024-11-22 388/week @ 2024-11-29

3,846 downloads per month
Used in cxl_rs

MIT license

18KB
318 lines

fletcher

A dependency free implementation of the Fletcher's checksum algorithm

crates.io docs.rs Build Status Crates.io License

Fletcher's Checksum was developed to provide nearly the same error checking capability as a CRC but with a faster software implementation. This is not a cryptographically secure checksum, it's only meant to be used for checking the integrity of data NOT the authenticity.

Algorithm Pros ✅

This algorithm is faster to run in software than most CRCs. This is because the CRC algorithm was originally designed to be simple to implement in hardware, but not neccesarily in software. The Fletcher Checksum was designed specifically to be suited for implementation in software.

Algorithm Cons ❌

This checksum algorithm does suffer from not being able to distinguish 0xFF from 0x00. Meaning a block of data with all bits set to 1 will have the exact same the same checksum as a block of data with all bits set to 0. This comes from the fact that the algorithm uses one's complement math.

Fletcher's checksum isn't quite as good at detecting bit errors in data as a CRC with a well choosen polynomial.

How To Use

If you have an entire block of data you want to get the checksum of you can use the calc functions (calc_fletcher16, calc_fletcher32, calc_fletcher64) to get the checksum in a single function call.

If you are getting the data in chunks you can make a Fletcher object (Fletcher16, Fletcher32, Fletcher64) to manage the intermediate state between chunks of data. The checksum objects take in slices of data to process. There is no minimum length required of the slices, all of the provided data will be processed to completion. The type of the input data is dictated by the size of the checksum value. i.e. a 64-bit checksum operates on 32-bit wide values.

The checksum object can be queried for it's current checksum value as any time with the [Fletcher::value()] function.

Example

let data: [u8; 6] = [0xC1, 0x77, 0xE9, 0xC0, 0xAB, 0x1E];
assert_eq!(fletcher::calc_fletcher16(&data), 0x3FAD);
// Or if you want to work on smaller chunks of data
let mut checksum = fletcher::Fletcher16::new();
checksum.update(&data[0..3]);
checksum.update(&data[3..]);
assert_eq!(checksum.value(), 0x3FAD);

No runtime deps