From 022068a97066ce5cfacc8848674101a491bf9a38 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Wed, 8 Apr 2026 12:50:31 -0700 Subject: [PATCH] feat: add Fletcher checksum to hashing --- DIRECTORY.md | 1 + src/hashing/fletcher.rs | 77 +++++++++++++++++++++++++++++++++++++++++ src/hashing/mod.rs | 2 ++ 3 files changed, 80 insertions(+) create mode 100644 src/hashing/fletcher.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index 0abd922e3a1..9e27335e9d5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -214,6 +214,7 @@ * [Stable Matching](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/stable_matching.rs) * Hashing * [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/blake2b.rs) + * [Fletcher](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/fletcher.rs) * [Hashing Traits](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/hashing_traits.rs) * [MD5](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/md5.rs) * [SHA-1](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/sha1.rs) diff --git a/src/hashing/fletcher.rs b/src/hashing/fletcher.rs new file mode 100644 index 00000000000..775f98c4ec4 --- /dev/null +++ b/src/hashing/fletcher.rs @@ -0,0 +1,77 @@ +//! The Fletcher checksum is an algorithm for computing a position-dependent +//! checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs +//! in the late 1970s. The objective of the Fletcher checksum was to provide +//! error-detection properties approaching those of a cyclic redundancy check +//! but with the lower computational effort associated with summation techniques. +//! +//! Reference: + +/// Computes the Fletcher-16 checksum of an ASCII string. +/// +/// Iterates over every byte in the input, maintaining two running sums +/// (`sum1` and `sum2`) each reduced modulo 255. The final 16-bit checksum +/// is produced by packing `sum2` into the high byte and `sum1` into the low byte. +/// +/// # Arguments +/// +/// * `data` - An ASCII string slice to checksum. +/// +/// # Returns +/// +/// A `u16` containing the Fletcher-16 checksum. +/// +/// # Examples +/// +/// ``` +/// use the_algorithms_rust::hashing::fletcher; +/// +/// assert_eq!(fletcher("hello world"), 6752); +/// assert_eq!(fletcher("onethousandfourhundredthirtyfour"), 28347); +/// assert_eq!(fletcher("The quick brown fox jumps over the lazy dog."), 5655); +/// ``` +pub fn fletcher(data: &str) -> u16 { + let mut sum1: u16 = 0; + let mut sum2: u16 = 0; + + for byte in data.bytes() { + sum1 = (sum1 + byte as u16) % 255; + sum2 = (sum2 + sum1) % 255; + } + + (sum2 << 8) | sum1 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hello_world() { + assert_eq!(fletcher("hello world"), 6752); + } + + #[test] + fn test_long_word() { + assert_eq!(fletcher("onethousandfourhundredthirtyfour"), 28347); + } + + #[test] + fn test_pangram() { + assert_eq!( + fletcher("The quick brown fox jumps over the lazy dog."), + 5655 + ); + } + + #[test] + fn test_empty_string() { + assert_eq!(fletcher(""), 0); + } + + #[test] + fn test_single_char() { + // 'A' = 65; sum1 = 65 % 255 = 65, sum2 = 65 % 255 = 65 + // result = (65 << 8) | 65 = 16705 + assert_eq!(fletcher("A"), 16705); + } +} diff --git a/src/hashing/mod.rs b/src/hashing/mod.rs index 9481fbed64a..3996a789deb 100644 --- a/src/hashing/mod.rs +++ b/src/hashing/mod.rs @@ -1,4 +1,5 @@ mod blake2b; +mod fletcher; mod hashing_traits; mod md5; mod sha1; @@ -6,6 +7,7 @@ mod sha2; mod sha3; pub use self::blake2b::blake2b; +pub use self::fletcher::fletcher; pub use self::hashing_traits::{Hasher, HMAC}; pub use self::md5::{md5, md5_hex}; pub use self::sha1::sha1;