Crate ahash

source ·
Expand description

AHash is a hashing algorithm is intended to be a high performance, (hardware specific), keyed hash function. This can be seen as a DOS resistant alternative to FxHash, or a fast equivalent to SipHash. It provides a high speed hash algorithm, but where the result is not predictable without knowing a Key. This allows it to be used in a HashMap without allowing for the possibility that an malicious user can induce a collision.

How aHash works

aHash uses the hardware AES instruction on x86 processors to provide a keyed hash function. aHash is not a cryptographically secure hash.

Example

use ahash::{AHasher, RandomState};
use std::collections::HashMap;

let mut map: HashMap<i32, i32, RandomState> = HashMap::default();
map.insert(12, 34);

For convenience, both new-type wrappers and type aliases are provided. The new type wrappers are called called AHashMap and AHashSet. These do the same thing with slightly less typing. The type aliases are called ahash::HashMap, ahash::HashSet are also provided and alias the std::HashMap and std::HashSet. Why are there two options? The wrappers are convenient but can’t be used where a generic std::collection::HashMap<K, V, S> is required.

use ahash::AHashMap;

let mut map: AHashMap<i32, i32> = AHashMap::with_capacity(4);
map.insert(12, 34);
map.insert(56, 78);
// There are also type aliases provieded together with some extension traits to make
// it more of a drop in replacement for the std::HashMap/HashSet
use ahash::{HashMapExt, HashSetExt}; // Used to get with_capacity()
let mut map = ahash::HashMap::with_capacity(10);
map.insert(12, 34);
let mut set = ahash::HashSet::with_capacity(10);
set.insert(10);

Re-exports

pub use crate::random_state::RandomState;

Modules

Structs

A HashMap using RandomState to hash the items. (Requires the std feature to be enabled.)
A HashSet using RandomState to hash the items. (Requires the std feature to be enabled.)
A Hasher for hashing an arbitrary stream of bytes.

Traits

A convenience trait that can be used together with the type aliases defined to get access to the new() and with_capacity() methods for the HashMap type alias.
A convenience trait that can be used together with the type aliases defined to get access to the new() and with_capacity() methods for the HashSet type aliases.

Type Definitions

Type alias for HashMap<K, V, ahash::RandomState>
Type alias for HashSet<K, ahash::RandomState>