| 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 super::sip128::{Hasher128, SipHasher, SipHasher13, SipHasher24}; |
| 12 |
use std::hash::{Hash, Hasher}; |
| 13 |
|
| 14 |
// Hash just the bytes of the slice, without length prefix |
| 15 |
struct Bytes<'a>(&'a [u8]); |
| 16 |
|
| 17 |
impl<'a> Hash for Bytes<'a> { |
| 18 |
#[allow(unused_must_use)] |
| 19 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 20 |
let Bytes(v) = *self; |
| 21 |
state.write(v); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
fn hash_with<H: Hasher + Hasher128, T: Hash>(mut st: H, x: &T) -> [u8; 16] { |
| 26 |
x.hash(&mut st); |
| 27 |
st.finish128().as_bytes() |
| 28 |
} |
| 29 |
|
| 30 |
fn hash<T: Hash>(x: &T) -> [u8; 16] { |
| 31 |
hash_with(SipHasher::new(), x) |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
#[allow(unused_must_use)] |
| 36 |
fn test_siphash128_1_3() { |
| 37 |
let vecs: [[u8; 16]; 1] = [[ |
| 38 |
231, 126, 188, 178, 39, 136, 165, 190, 253, 98, 219, 106, 221, 48, 48, 1, |
| 39 |
]]; |
| 40 |
|
| 41 |
let k0 = 0x_07_06_05_04_03_02_01_00; |
| 42 |
let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08; |
| 43 |
let mut buf = Vec::new(); |
| 44 |
let mut t = 0; |
| 45 |
let mut state_inc = SipHasher13::new_with_keys(k0, k1); |
| 46 |
|
| 47 |
while t < 1 { |
| 48 |
let vec = vecs[t]; |
| 49 |
let out = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf)); |
| 50 |
assert_eq!(vec, out[..]); |
| 51 |
|
| 52 |
let full = hash_with(SipHasher13::new_with_keys(k0, k1), &Bytes(&buf)); |
| 53 |
let i = state_inc.finish128().as_bytes(); |
| 54 |
|
| 55 |
assert_eq!(full, i); |
| 56 |
assert_eq!(full, vec); |
| 57 |
|
| 58 |
buf.push(t as u8); |
| 59 |
Hasher::write(&mut state_inc, &[t as u8]); |
| 60 |
|
| 61 |
t += 1; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
#[allow(unused_must_use)] |
| 67 |
fn test_siphash128_2_4() { |
| 68 |
let vecs: [[u8; 16]; 1] = [[ |
| 69 |
163, 129, 127, 4, 186, 37, 168, 230, 109, 246, 114, 20, 199, 85, 2, 147, |
| 70 |
]]; |
| 71 |
|
| 72 |
let k0 = 0x_07_06_05_04_03_02_01_00; |
| 73 |
let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08; |
| 74 |
let mut buf = Vec::new(); |
| 75 |
let mut t = 0; |
| 76 |
let mut state_inc = SipHasher24::new_with_keys(k0, k1); |
| 77 |
|
| 78 |
while t < 1 { |
| 79 |
let vec = vecs[t]; |
| 80 |
let out = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf)); |
| 81 |
assert_eq!(vec, out[..]); |
| 82 |
|
| 83 |
let full = hash_with(SipHasher24::new_with_keys(k0, k1), &Bytes(&buf)); |
| 84 |
let i = state_inc.finish128().as_bytes(); |
| 85 |
|
| 86 |
assert_eq!(full, i); |
| 87 |
assert_eq!(full, vec); |
| 88 |
|
| 89 |
buf.push(t as u8); |
| 90 |
Hasher::write(&mut state_inc, &[t as u8]); |
| 91 |
|
| 92 |
t += 1; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_siphash128_serde() { |
| 98 |
let val64 = 0xdead_beef_dead_beef_u64; |
| 99 |
let hash = hash(&val64); |
| 100 |
let serialized = serde_json::to_string(&hash).unwrap(); |
| 101 |
let deserialized: [u8; 16] = serde_json::from_str(&serialized).unwrap(); |
| 102 |
assert_eq!(hash, deserialized); |
| 103 |
} |