Skip to main content

RapidSecrets

Struct RapidSecrets 

Source
pub struct RapidSecrets {
    pub seed: u64,
    pub secrets: [u64; 7],
}
Expand description

Hold the seed and secrets to be used by rapidhash.

RapidSecrets premix the seed and generate a set of other secrets based on the seed that are all used in the hashing process. There are some quality checks on the random values to ensure a reasonable distribution of entropy in the generated secrets.

Constructing this struct is fairly cheap, but unnecessary in the critical path. We therefore recommend instantiating it once and re-using the same instance for any persistent hashing. The seed method is marked const to also do so at compile time.

§Minimal HashDoS Protection

We recommend changing the default seed and secrets must be changed to avoid trivial collision attacks. For persistent hashing, you can hard code your own randomized seed at compile time.

use rapidhash::v2::RapidSecrets;
const DEFAULT_SECRETS: RapidSecrets = RapidSecrets::seed(0x123456);  // <-- change this value!

/// Export your chosen rapidhash version and secrets for use throughout your project.
pub fn rapidhash(data: &[u8]) -> u64 {
    rapidhash::v2::rapidhash_v2_2_seeded(data, &DEFAULT_SECRETS)
}

TODO: serde or serialization support.

Fields§

§seed: u64

The core rapidhash seed.

§secrets: [u64; 7]

The secrets, effectively other seeds used in the hashing process.

Implementations§

Source§

impl RapidSecrets

Source

pub const fn seed(seed: u64) -> Self

Generate secrets from a given randomized seed.

Note the chosen seed will be pre-mixed to further randomized it, and the secrets will be computed based on the seed.

If compatibility with the C++ implementation is required, use the seed_cpp method instead.

Source

pub const fn reseed(&self) -> Self

Creates a new RapidSecrets instance with a different seed and the same secrets.

This is useful for in-memory hashing, so we can quickly use a different seed for other HashMaps.

Source

pub const fn seed_cpp(seed: u64) -> Self

Creates a new RapidSecrets instance using a seed and secrets that are compatible with the C++ implementation.

Note that these use the default secrets and therefore are liable to some trivial collision attacks, as randomising both the seed and secrets is necessary to provide minimal HashDoS resistance.

Trait Implementations§

Source§

impl Clone for RapidSecrets

Source§

fn clone(&self) -> RapidSecrets

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 Copy for RapidSecrets

Source§

impl Debug for RapidSecrets

Source§

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

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

impl Eq for RapidSecrets

Source§

impl Hash for RapidSecrets

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for RapidSecrets

Source§

fn eq(&self, other: &RapidSecrets) -> 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 StructuralPartialEq for RapidSecrets

Auto Trait Implementations§

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.