Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions src/hashing/fletcher.rs
Original file line number Diff line number Diff line change
@@ -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: <https://en.wikipedia.org/wiki/Fletcher%27s_checksum>

/// 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);
}
}
2 changes: 2 additions & 0 deletions src/hashing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod blake2b;
mod fletcher;
mod hashing_traits;
mod md5;
mod sha1;
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;
Expand Down
Loading