stream.rs 3.38 KB
Newer Older
Tom Almeida's avatar
Tom Almeida committed
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use core::hash::Hasher;

use {hash_seeded, helper};

/// The streaming version of the algorithm.
pub struct SeaHasher {
    /// The state of the hasher.
    state: u64,
    /// The first key.
    k1: u64,
    /// The second key.
    k2: u64,
    /// The third key.
    k3: u64,
    /// The fourth key.
    k4: u64,
}

impl Default for SeaHasher {
    fn default() -> SeaHasher {
        SeaHasher::with_seeds(0xe7b0c93ca8525013, 0x011d02b854ae8182, 0x7bcc5cf9c39cec76, 0xfa336285d102d083)
    }
}

impl SeaHasher {
    /// Create a new `SeaHasher` with default state.
    pub fn new() -> SeaHasher {
        SeaHasher::default()
    }

    /// Construct a new `SeaHasher` given some seed.
    ///
    /// For maximum quality, these seeds should be chosen at random.
    pub fn with_seeds(k1: u64, k2: u64, k3: u64, k4: u64) -> SeaHasher {
        SeaHasher {
            state: k1 ^ k3,
            k1: k1,
            k2: k2,
            k3: k3,
            k4: k4,
        }
    }

    /// Write some integer in.
    ///
    /// This applies XEX key whitening with the keys given as argument.
    fn write(&mut self, n: u64, k1: u64, k2: u64) {
        self.state ^= n ^ k1;
        self.state = helper::diffuse(self.state) ^ k2;
    }
}

impl Hasher for SeaHasher {
    fn finish(&self) -> u64 {
        helper::diffuse(self.state ^ self.k3) ^ self.k4
    }

    fn write(&mut self, bytes: &[u8]) {
        self.state ^= hash_seeded(bytes, self.k1, self.k2, self.k3, self.k4);
        self.state = helper::diffuse(self.state);
    }

    fn write_u64(&mut self, n: u64) {
        let k1 = self.k1;
        let k2 = self.k2;
        self.write(n, k1, k2)
    }

    fn write_u8(&mut self, n: u8) {
        let k1 = self.k1;
        let k3 = self.k3;
        self.write(n as u64, k1, k3)
    }

    fn write_u16(&mut self, n: u16) {
        let k1 = self.k1;
        let k2 = self.k2;
        self.write(n as u64, k2, k1)
    }

    fn write_u32(&mut self, n: u32) {
        let k2 = self.k2;
        let k3 = self.k3;
        self.write(n as u64, k2, k3)
    }

    fn write_usize(&mut self, n: usize) {
        let k2 = self.k2;
        let k3 = self.k3;
        self.write(n as u64, k3, k2)
    }

    fn write_i64(&mut self, n: i64) {
        let k1 = self.k1;
        let k2 = self.k2;
        self.write(n as u64, !k1, !k2)
    }

    fn write_i8(&mut self, n: i8) {
        let k1 = self.k1;
        let k3 = self.k3;
        self.write(n as u64, !k1, !k3)
    }

    fn write_i16(&mut self, n: i16) {
        let k1 = self.k1;
        let k2 = self.k2;
        self.write(n as u64, !k2, !k1)
    }

    fn write_i32(&mut self, n: i32) {
        let k2 = self.k2;
        let k3 = self.k3;
        self.write(n as u64, !k2, !k3)
    }

    fn write_isize(&mut self, n: isize) {
        let k2 = self.k2;
        let k3 = self.k3;
        self.write(n as u64, !k3, !k2)
    }
}
123
124
125
126
127
128
129
130


#[cfg(test)]
mod tests {
    use super::*;
    use core::hash::Hasher;

    #[test]
131
    fn chunked_equiv() {
132
133
134
135
136
137
138
139
140
141
142
143
        let test_buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0];

        let mut stream_hasher1 = SeaHasher::default();
        Hasher::write(&mut stream_hasher1, test_buf);

        let mut stream_hasher2 = SeaHasher::default();
        Hasher::write(&mut stream_hasher2, &test_buf[..8]);
        Hasher::write(&mut stream_hasher2, &test_buf[8..]);

        assert_eq!(stream_hasher1.finish(), stream_hasher2.finish())
    }
}