use crate::constants::{CRYPTO_SHORTHASH_BYTES, CRYPTO_SHORTHASH_KEYBYTES};
use crate::rng::copy_randombytes;
use crate::siphash24::siphash24;
pub type Hash = [u8; CRYPTO_SHORTHASH_BYTES];
pub type Key = [u8; CRYPTO_SHORTHASH_KEYBYTES];
pub fn crypto_shorthash_keygen() -> Key {
let mut key = Key::default();
copy_randombytes(&mut key);
key
}
pub fn crypto_shorthash(output: &mut Hash, input: &[u8], key: &Key) {
siphash24(output, input, key)
}
#[cfg(all(test, dryoc_native_tests))]
mod tests {
use rand::TryRng;
use super::*;
#[test]
fn test_shorthash() {
use rand::rngs::SysRng;
use sodiumoxide::crypto::shorthash;
for _ in 0..20 {
let key = crypto_shorthash_keygen();
let mut input = vec![0u8; (SysRng.try_next_u32().unwrap() % 69) as usize];
copy_randombytes(&mut input);
let mut output = Hash::default();
crypto_shorthash(&mut output, &input, &key);
let so_output = shorthash::shorthash(
&input,
&shorthash::Key::from_slice(&key).expect("so key failed"),
);
assert_eq!(output, so_output.0);
}
}
}