Google HighwayHash 的原生 Rust 端口,它利用 SIMD 指令实现了快速且强大的哈希函数
This crate is a native Rust port of Google's HighwayHash, which is a fast, keyed, and strong hash function, whose output is hardware independent.
no_std compatibleHighwayHash (the algorithm) has not undergone extensive cryptanalysis like SipHash (the default hashing algorithm in Rust), but according to the authors, HighwayHash output bits are uniformly distributed and should withstand differential and rotational attacks. Hence HighwayHash is referred to as a strong hash function, not a cryptographic hash function. I encourage anyone interested to peruse the paper to understand the risks.
The quickest way to get started:
use highway::{HighwayHasher, HighwayHash};
let res: u64 = HighwayHasher::default().hash64(&[]);
let res2: [u64; 2] = HighwayHasher::default().hash128(&[]);
let res3: [u64; 4] = HighwayHasher::default().hash256(&[]);A more complete tour of the API follows:
…Creating a 128bit and 256bit hash is just as simple.
…Use highway hash in standard rust collections
use std::collections::HashMap;
use highway::{HighwayBuildHasher, Key};
let mut map =
HashMap::with_hasher(HighwayBuildHasher::new(Key([
0xcbf29ce484222325,
0xc3a5c85c97cb3127,
0xb492b66fbe98f273,
0x9ae16a3b2f90404f,
])));
map.insert(1, 2);
assert_eq!(map.get(&1), Some(&2));Or if utilizing a key is not important, one can use the default
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use highway::HighwayHasher;
let mut map =
HashMap::with_hasher(BuildHasherDefault::::default());
map.insert(1, 2);
assert_eq!(map.get(&1), Some(&2));Hashing a file, or anything implementing Read
use std::hash::Hasher;
use highway::{PortableHash, HighwayHash};
let mut file = &b"hello world"[..];
// We're using the `PortableHash` to show importing a specific hashing
// implementation (all hash outputs are already portable / hardware agnostic).
// The main reason for directly using `PortableHash` would be if avoiding
// `unsafe` code blocks is a top priority.
let mut hasher = PortableHash::default();
std::io::copy(&mut file, &mut hasher)?;
let hash64 = hasher.finish(); // core Hasher API
let hash256 = hasher.finalize256(); // HighwayHash APIHighwayHash can be used against untrusted user input where weak hashes can't be used due to exploitation, verified cryptographic hashes are too slow, and a strong hash function meets requirements. Some specific scenarios given by the authors of HighwayHash:
HighwayHash may not be a good fit if the payloads trend small ( assets/highway.csv
…bash (cd compare && cargo bench -- builder)
*Explicitly disable avx2*
```bash
(cd compare && RUSTFLAGS="-C target-feature=-avx2" cargo bench -- builder)Explicitly disable avx2 when targeting native cpu
(cd compare && RUSTFLAGS="-C target-cpu=native -C target-feature=+sse4.1,-avx2" \
cargo bench -- builder)暂无开放 Issues,或尚未同步最近议题。