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
//! [BSD][1] checksum implementation.
//!
//! Known in UNIX as the `sum` command.
//!
//! [1]: https://en.wikipedia.org/wiki/BSD_checksum


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

use core::hash::Hasher;


/// The BSD hasher.
#[derive(Copy, Clone)]
pub struct Bsd {
    state: u16,
}


impl Default for Bsd {
    fn default() -> Self {
        Bsd { state: 0 }
    }
}


impl Hasher for Bsd {
    #[inline]
    fn write(&mut self, input: &[u8]) {
        for &byte in input.iter() {
            // Rotate one bit right, add next byte and prevent
            self.state = self.state.rotate_right(1).wrapping_add(byte as u16);
        }
    }
    #[inline]
    fn finish(&self) -> u64 {
        self.state as u64
    }
}

implement_digest!(Bsd, U1024, U2);

#[cfg(test)]
#[cfg(feature = "generic")]
mod tests {
    unit_test_no_data!(Bsd, 0);
    unit_test_part_data!(Bsd);
    unit_test_single_byte!(Bsd, b"a", b'a');
}