Skip to main content

SeedableState

Struct SeedableState 

Source
pub struct SeedableState<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool = false, const PROTECTED: bool = false> { /* private fields */ }
Expand description

A [std::hash::BuildHasher] that initializes a RapidHasher with a user-provided seed and secrets.

SeedableState should rarely be used as providing DoS resistance requires a randomized seed and secrets. Users should instead prefer either:

The lifetime 's is for the reference to the secrets. When using SeedableState::random or SeedableState::fixed secrets, this lifetime will be 'static.

§Example

use std::collections::HashMap;
use std::hash::Hasher;

use rapidhash::quality::SeedableState;

let mut map = HashMap::with_hasher(SeedableState::default());
map.insert(42, "the answer");

Implementations§

Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source

pub fn new(seed: u64) -> Self

Create a new seedable state with a custom seed and automatically generated secrets.

The seed will be pre-mixed to improve entropy. The global secrets are randomly generated once at program start, and then will be re-used for all subsequent calls to this function.

§Example
use core::hash::BuildHasher;
use rapidhash::quality::SeedableState;

let state = SeedableState::new(0);

let hash: u64 = state.hash_one(b"hello");
println!("hash: {hash}");
Source

pub fn random() -> Self

Create a new seedable state with a random seed.

This is slower than using crate::inner::RandomState, please use that instead.

Source

pub fn fixed() -> Self

Create a new seedable state with the default seed and secrets.

Using the default secrets does not offer HashDoS resistance, but they will be fixed between different runs of the program.

Please note that fast::RapidHasher and quality::RapidHasher are not guaranteed to produce the same hash outputs between different crate versions, compiler versions, or platforms.

Also see [GlobalState] for a faster zero-sized alternative that uses global secrets that are fixed only for the lifetime of the program.

Source

pub fn custom(seed: u64, secrets: &'s [u64; 7]) -> Self

Create a new seedable state with a custom seed and secrets.

§Warning

This constructor uses the provided seed and secrets as the initial state without any pre-mixing or validation. Supplying low-entropy or structured values (e.g., 0, all-zero arrays, counters, timestamps) can produce degenerate hashing (high collision rates or identical outputs).

§Requirements
  • seed and secrets must not be zero; avoid any all-zero/near-zero state.
  • Generate seed and secrets with a cryptographically secure PRNG and treat them as independent for each hasher instance.
  • Do not derive successive seeds from predictable data (time, PID/TID, memory addresses) or by simple incrementation.
§Recommendation

If you cannot pre-mix the seed yourself, use SeedableState::new instead.

§Example (secure generation)
use core::hash::BuildHasher;
use rapidhash::quality::SeedableState;

// randomly generate secrets
let seed: u64 = rand::random();
let secrets: [u64; 7] = rand::random();

// create the state
let state = SeedableState::custom(seed, &secrets);

// hash using the state
let hash: u64 = state.hash_one(b"hello");
println!("hash: {hash}");
Source

pub fn with_seed(seed: u64, secrets: &'s [u64; 7]) -> Self

👎Deprecated since 4.1.0:

Use custom() or new() instead.

Deprecated and renamed to SeedableState::custom.

Trait Implementations§

Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> BuildHasher for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

type Hasher = RapidHasher<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Type of the hasher that will be created.
Source§

fn build_hasher(&self) -> Self::Hasher

Creates a new hasher. Read more
1.71.0 · Source§

fn hash_one<T>(&self, x: T) -> u64
where T: Hash, Self: Sized, Self::Hasher: Hasher,

Calculates the hash of a single value. Read more
Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Clone for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn clone(&self) -> SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Copy for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Debug for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Default for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn default() -> Self

Create a new SeedableState with a random seed. See SeedableState::random for more details.

Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Eq for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> PartialEq for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Source§

fn eq( &self, other: &SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>, ) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> StructuralPartialEq for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Auto Trait Implementations§

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Freeze for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> RefUnwindSafe for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Send for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Sync for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> Unpin for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> UnsafeUnpin for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

§

impl<'s, const AVALANCHE: bool, const SPONGE: bool, const COMPACT: bool, const PROTECTED: bool> UnwindSafe for SeedableState<'s, AVALANCHE, SPONGE, COMPACT, PROTECTED>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.