use crate::hash::Hasher;
pub struct OneAtATime {
state: u32,
}
impl Hasher for OneAtATime {
type Output = u32;
fn new() -> Self {
Self { state: 0 }
}
fn write(&mut self, input: &[u8]) {
for &b in input {
self.state = self.state.wrapping_add(b as u32);
self.state = self.state.wrapping_add(self.state << 10);
self.state ^= self.state >> 6;
}
}
fn finalize(mut self) -> Self::Output {
self.state = self.state.wrapping_add(self.state << 3);
self.state ^= self.state >> 11;
self.state = self.state.wrapping_add(self.state << 15);
self.state
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let mut hasher = OneAtATime::new();
hasher.write(b"kt1_lod_1_2_5_6_17");
let hash = hasher.finalize();
assert_eq!(hash, 373823972);
}
}