pub struct GlobalState<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> { /* private fields */ }Expand description
A BuildHasher that uses a global seed and secrets, randomized only once on startup.
The global seed and secrets are randomized on the first instantiation, and then every subsequent instance of GlobalState will re-use the same seed and secrets, ensuring consistent hash outputs for the duration of the program.
The one-time randomization is derived from OS/platform entropy when the getrandom_04 feature is
enabled, or the standard library’s secure RNG when the std feature is enabled, falling back
to ASLR and weaker entropy sources otherwise. Because every
instance shares a single seed and secret set, GlobalState only offers minimal HashDoS
resistance: an attacker cannot predict the secrets, but every map in the process shares the same
collision structure. Prefer RandomState when each map should be
seeded independently.
See RandomState’s portability docs for enabling true
randomisation on wasm32 and embedded targets via the getrandom feature.
§Performance
GlobalState is faster and smaller than RandomState while still providing some minimal DoS
resistance, and so may be preferable to RandomState when instantiating many hashmaps in a
tight loop.
§Example
use std::collections::HashMap;
use std::hash::Hasher;
use rapidhash::fast::GlobalState;
let mut map = HashMap::with_hasher(GlobalState::default());
map.insert(42, "the answer");Implementations§
Source§impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new global state with a global seed and secrets.
The seed and secrets are randomized once, on the first instantiation of any GlobalState,
and then cached for the lifetime of the program; all subsequent instances share them. With
the std feature the one-time randomization is derived from the standard library’s secure
RNG; without std it falls back to ASLR and other weaker sources of entropy.
Because every instance shares the same seed and secrets, all maps in the process share the
same collision structure. Use RandomState if you need each map
to be seeded independently.
On platforms which do not support atomic pointers, the secrets will be the default rapidhash
secrets, which are not randomized. Therefore, GlobalState on targets without atomic
pointer support has no HashDoS resistance guarantees: there is nowhere to store a
randomized value, so even the getrandom_04 feature cannot help. Prefer
RandomState with the getrandom_04 feature on these targets,
which draws a fresh random seed per instance instead.
Trait Implementations§
Source§impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> BuildHasher for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> BuildHasher for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Source§type Hasher = RapidHasher<'static, AVALANCHE, SPONGE, COMPACT, PROTECTED>
type Hasher = RapidHasher<'static, AVALANCHE, SPONGE, COMPACT, PROTECTED>
Source§fn build_hasher(&self) -> Self::Hasher
fn build_hasher(&self) -> Self::Hasher
Source§impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Clone for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Clone for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Copy for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Source§impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Debug for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Debug for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Source§impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Default for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Warning that GlobalState only randomizes the seed on platforms that support atomic pointers.
impl<const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Default for GlobalState<AVALANCHE, SPONGE, COMPACT, PROTECTED>
Warning that GlobalState only randomizes the seed on platforms that support atomic pointers.