mirror of
https://codeberg.org/ral/rwedid.git
synced 2024-08-16 09:59:49 +02:00
16 lines
437 B
Rust
16 lines
437 B
Rust
// Compute the checksum over the bytes of a block.
|
|
#[allow(clippy::cast_possible_truncation)]
|
|
pub fn checksum(bytes: &[u8]) -> u8 {
|
|
bytes.iter().fold(0u32, |x, y| x + u32::from(*y)) as u8
|
|
}
|
|
|
|
// Validate the checksum
|
|
pub fn valid_checksum(bytes: &[u8]) -> bool {
|
|
checksum(bytes) == 0x00
|
|
}
|
|
|
|
// Print the checksum to stderr
|
|
pub fn print_checksum(bytes: &[u8]) {
|
|
let cs = checksum(bytes);
|
|
eprintln!(" Checksum is: {cs}");
|
|
}
|