ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use std::hash::Hasher;

#[cfg(feature = "cpu")]
use ihash::cpu;
use ihash::{
  HashMap, HashMapExt, HashSet, HashSetExt, IHasher, Stream, hash_xof, hash32, hash64, hash128,
  hash256, seed,
};

fn main() {
  let msg = b"Hello World";

  // Seedless default API (deterministic & bit-exact across x86_64, aarch64, and pure CPU).
  println!("hash32  = 0x{:08x}", hash32(msg));
  println!("hash64  = 0x{:016x}", hash64(msg));
  println!("hash128 = 0x{:032x}", hash128(msg));
  println!("hash256 = {:02x?}", &hash256(msg)[..8]);

  // Extendable-output hash (XOF).
  let mut xof_li = [0u8; 64];
  hash_xof(msg, &mut xof_li);
  println!("xof[..8]= {:02x?}", &xof_li[..8]);

  // Explicit seed API lives under `ihash::seed`.
  println!("seed64  = 0x{:016x}", seed::hash64(msg, 42));

  // Seekable stream cipher (default unseeded key or seeded keystream).
  let mut buf_li = *b"fast non-cryptographic stream cipher";
  let mut enc = seed::keystream(12345);
  enc.xor_keystream(&mut buf_li);
  let mut dec = seed::keystream(12345);
  dec.xor_keystream(&mut buf_li);
  println!("stream  = {}", String::from_utf8_lossy(&buf_li));
  let mut key_stream = Stream::new(msg);
  key_stream.xor_keystream(&mut buf_li[..8]);

  // Stateful Hasher and standard-style HashMap / HashSet.
  let mut hasher = IHasher::new();
  hasher.write(msg);
  hasher.write_u64(12345);
  println!("hasher  = 0x{:016x}", hasher.finish());

  let mut map: HashMap<&str, usize> = HashMap::new();
  map.insert("ihash", 1);
  let mut set: HashSet<&str> = HashSet::new();
  set.insert("ihash");
  println!(
    "map/set = {:?} / {}",
    map.get("ihash"),
    set.contains("ihash")
  );

  // Optional pure-CPU SIMD fallback (feature = "cpu").
  #[cfg(feature = "cpu")]
  println!("cpu64   = 0x{:016x}", cpu::hash64(msg));
}