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 use std::hash::{Hash, Hasher};
12
13 use super::sip128::{Hasher128, SipHasher, 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
hash_with<H: Hasher + Hasher128, T: Hash>(mut st: H, x: &T) -> [u8; 16]26 fn hash_with<H: Hasher + Hasher128, T: Hash>(mut st: H, x: &T) -> [u8; 16] {
27 x.hash(&mut st);
28 st.finish128().as_bytes()
29 }
30
hash<T: Hash>(x: &T) -> [u8; 16]31 fn hash<T: Hash>(x: &T) -> [u8; 16] {
32 hash_with(SipHasher::new(), x)
33 }
34
35 #[test]
test_siphash128_idempotent()36 fn test_siphash128_idempotent() {
37 let val64 = 0xdead_beef_dead_beef_u64;
38 assert_eq!(hash(&val64), hash(&val64));
39 let val32 = 0xdeadbeef_u32;
40 assert_eq!(hash(&val32), hash(&val32));
41 }
42
43 #[test]
44 #[allow(unused_must_use)]
test_siphash128_1_3()45 fn test_siphash128_1_3() {
46 let vecs: [[u8; 16]; 1] = [[
47 231, 126, 188, 178, 39, 136, 165, 190, 253, 98, 219, 106, 221, 48, 48, 1,
48 ]];
49
50 let k0 = 0x_07_06_05_04_03_02_01_00;
51 let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08;
52 let mut buf = Vec::new();
53 let mut t = 0;
54 let mut state_inc = SipHasher13::new_with_keys(k0, k1);
55
56 while t < 1 {
57 let vec = vecs[t];
58 let out = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf));
59 assert_eq!(vec, out[..]);
60
61 let full = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf));
62 let i = state_inc.finish128().as_bytes();
63
64 assert_eq!(full, i);
65 assert_eq!(full, vec);
66
67 buf.push(t as u8);
68 Hasher::write(&mut state_inc, &[t as u8]);
69
70 t += 1;
71 }
72 }
73
74 #[test]
75 #[allow(unused_must_use)]
test_siphash128_2_4()76 fn test_siphash128_2_4() {
77 let vecs: [[u8; 16]; 1] = [[
78 163, 129, 127, 4, 186, 37, 168, 230, 109, 246, 114, 20, 199, 85, 2, 147,
79 ]];
80
81 let k0 = 0x_07_06_05_04_03_02_01_00;
82 let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08;
83 let mut buf = Vec::new();
84 let mut t = 0;
85 let mut state_inc = SipHasher24::new_with_keys(k0, k1);
86
87 while t < 1 {
88 let vec = vecs[t];
89 let out = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf));
90 assert_eq!(vec, out[..]);
91
92 let full = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf));
93 let i = state_inc.finish128().as_bytes();
94
95 assert_eq!(full, i);
96 assert_eq!(full, vec);
97
98 buf.push(t as u8);
99 Hasher::write(&mut state_inc, &[t as u8]);
100
101 t += 1;
102 }
103 }
104
105 #[test]
test_siphash128_simple()106 fn test_siphash128_simple() {
107 let array: &[u8] = &[1, 2, 3];
108 let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
109 let hasher = SipHasher13::new_with_key(key);
110 let h = hasher.hash(array).as_bytes();
111 _ = h;
112 }
113
114 #[test]
test_siphash128_incremental()115 fn test_siphash128_incremental() {
116 let array1: &[u8] = &[1, 2, 3];
117 let array2: &[u8] = &[4, 5, 6];
118 let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
119 let mut hasher = SipHasher13::new_with_key(key);
120 hasher.write(array1);
121 hasher.write(array2);
122 let h = hasher.finish128().as_bytes();
123 _ = h;
124 }
125
126 #[test]
127 #[cfg(all(feature = "serde", feature = "serde_json"))]
test_siphash128_serde()128 fn test_siphash128_serde() {
129 let val64 = 0xdead_beef_dead_beef_u64;
130 let hash = hash(&val64);
131 let serialized = serde_json::to_string(&hash).unwrap();
132 let deserialized: [u8; 16] = serde_json::from_str(&serialized).unwrap();
133 assert_eq!(hash, deserialized);
134 }
135