1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! [Fletcher16][1] checksum implementation.
//!
//! [1]: https://en.wikipedia.org/wiki/Fletcher%27s_checksum#Fletcher-16

#[cfg(feature = "generic")]
extern crate generic_array;
#[cfg(feature = "generic")]
extern crate digest;

use core::hash::Hasher;
use core::borrow::BorrowMut;


mod consts {
    /// The maximum number of consecutive sums before an u8 overflow could happen.
    pub const NMAX: usize = 20;
}


/// The Fletcher16 hasher.
#[derive(Copy, Clone)]
pub struct Fletcher16 {
    sum1: u16,
    sum2: u16,
}


impl Default for Fletcher16 {
    fn default() -> Self {
        Fletcher16 { sum1: 0, sum2: 0 }
    }
}


impl Fletcher16 {
    #[inline]
    fn finalize(&self) -> [u16; 2] {
        [
            self.sum1 % u8::max_value() as u16,
            self.sum2 & u8::max_value() as u16,
        ]
    }
}


impl Hasher for Fletcher16 {
    #[inline]
    fn write(&mut self, input: &[u8]) {
        let mut byte_it = input.iter();
        let mut i: usize;
        loop {
            i = 0;
            // Read bytes by block of 20 (max value before u8 overflow)
            for &byte in byte_it.borrow_mut().take(consts::NMAX) {
                self.sum1 += byte as u16;
                self.sum2 += self.sum1;
                i += 1;
            }
            // Reduce sums to u8
            self.sum1 %= u8::max_value() as u16;
            self.sum2 %= u8::max_value() as u16;
            // If the last block was read, stop
            if i < consts::NMAX {
                break;
            }
        }
    }
    #[inline]
    fn finish(&self) -> u64 {
        let sums = self.finalize();
        ((sums[1] << 8) | sums[0]) as u64
    }
}

implement_digest!(Fletcher16, U1024, U2);

#[cfg(test)]
#[cfg(feature = "generic")]
mod tests {
    unit_test_no_data!(Fletcher16, 0);
    unit_test_part_data!(Fletcher16);
}