1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 
12 use std::hash::{Hash, Hasher};
13 use sip128::{Hasher128, SipHasher13, SipHasher24};
14 
15 // Hash just the bytes of the slice, without length prefix
16 struct Bytes<'a>(&'a [u8]);
17 
18 impl<'a> Hash for Bytes<'a> {
19     #[allow(unused_must_use)]
hash<H: Hasher>(&self, state: &mut H)20     fn hash<H: Hasher>(&self, state: &mut H) {
21         let Bytes(v) = *self;
22         state.write(v);
23     }
24 }
25 
26 macro_rules! u8to64_le {
27     ($buf:expr, $i:expr) =>
28     ($buf[0+$i] as u64 |
29      ($buf[1+$i] as u64) << 8 |
30      ($buf[2+$i] as u64) << 16 |
31      ($buf[3+$i] as u64) << 24 |
32      ($buf[4+$i] as u64) << 32 |
33      ($buf[5+$i] as u64) << 40 |
34      ($buf[6+$i] as u64) << 48 |
35      ($buf[7+$i] as u64) << 56);
36     ($buf:expr, $i:expr, $len:expr) =>
37     ({
38         let mut t = 0;
39         let mut out = 0;
40         while t < $len {
41             out |= ($buf[t+$i] as u64) << t*8;
42             t += 1;
43         }
44         out
45     });
46 }
47 
hash_with<H: Hasher + Hasher128, T: Hash>(mut st: H, x: &T) -> [u8; 16]48 fn hash_with<H: Hasher + Hasher128, T: Hash>(mut st: H, x: &T) -> [u8; 16] {
49     x.hash(&mut st);
50     st.finish128().into_bytes()
51 }
52 
53 #[test]
54 #[allow(unused_must_use)]
test_siphash128_1_3()55 fn test_siphash128_1_3() {
56     let vecs: [[u8; 16]; 1] = [[231, 126, 188, 178, 39, 136, 165, 190, 253, 98, 219, 106, 221,
57                                 48, 48, 1]];
58 
59     let k0 = 0x_07_06_05_04_03_02_01_00;
60     let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08;
61     let mut buf = Vec::new();
62     let mut t = 0;
63     let mut state_inc = SipHasher13::new_with_keys(k0, k1);
64 
65     while t < 1 {
66         let vec = vecs[t];
67         let out = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf));
68         assert_eq!(vec, out[..]);
69 
70         let full = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf));
71         let i = state_inc.finish128().into_bytes();
72 
73         assert_eq!(full, i);
74         assert_eq!(full, vec);
75 
76         buf.push(t as u8);
77         Hasher::write(&mut state_inc, &[t as u8]);
78 
79         t += 1;
80     }
81 }
82 
83 #[test]
84 #[allow(unused_must_use)]
test_siphash128_2_4()85 fn test_siphash128_2_4() {
86     let vecs: [[u8; 16]; 1] = [[163, 129, 127, 4, 186, 37, 168, 230, 109, 246, 114, 20, 199, 85,
87                                 2, 147]];
88 
89     let k0 = 0x_07_06_05_04_03_02_01_00;
90     let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08;
91     let mut buf = Vec::new();
92     let mut t = 0;
93     let mut state_inc = SipHasher24::new_with_keys(k0, k1);
94 
95     while t < 1 {
96         let vec = vecs[t];
97         let out = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf));
98         assert_eq!(vec, out[..]);
99 
100         let full = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf));
101         let i = state_inc.finish128().into_bytes();
102 
103         assert_eq!(full, i);
104         assert_eq!(full, vec);
105 
106         buf.push(t as u8);
107         Hasher::write(&mut state_inc, &[t as u8]);
108 
109         t += 1;
110     }
111 }
112