Struct Hasher

Source
pub struct Hasher<'params>(/* private fields */);
Expand description

A Hasher implements one of the two hash 64-bit functions defined by a specific Params struct, further tweaked by a seed. Construct Hashers with Params::hasher, Params::secondary_hasher, or Params::component_hasher.

The hash value is a function of the Params, the UmashComponent, the seed, and of the bytes written to the Hasher, but independent of the size of the individual slices written to the hasher.

In other words, it doesn’t matter how we partition an input, the Hasher computes the same hash value as a one-shot UMASH call for the parameters and the concatenated input bytes.

Implementations§

Source§

impl<'params> Hasher<'params>

Source

pub fn write(&mut self, bytes: &[u8]) -> &mut Self

Updates the hash state by conceptually concatenating bytes to the hash input.

Examples found in repository?
examples/umash.rs (line 21)
4fn main() {
5    let args: Vec<String> = std::env::args().collect();
6    let input = args
7        .get(1)
8        .cloned()
9        .unwrap_or_else(|| "default input".to_string());
10    let seed = 42u64;
11    let my_params = umash::Params::derive(0, "hello example.c".as_bytes());
12    let fprint = my_params
13        .fingerprinter(seed)
14        .write(input.as_bytes())
15        .digest();
16
17    println!("Input: {}", input);
18    println!("Fingerprint: {:x}, {:x}", fprint.hash[0], fprint.hash[1]);
19    println!(
20        "Hash 0: {:x}",
21        my_params.hasher(seed).write(input.as_bytes()).digest()
22    );
23    println!(
24        "Hash 1: {:x}",
25        my_params
26            .secondary_hasher(seed)
27            .write(input.as_bytes())
28            .digest()
29    );
30
31    let mut h: umash::Hasher = (&my_params).into();
32    h.write(input.as_bytes());
33    println!("Hash: {:x}", h.finish());
34}
Source

pub fn digest(&self) -> u64

Returns the 64-bit hash value for the Hasher’s Params and the bytes passed to Hasher::write so far.

Examples found in repository?
examples/umash.rs (line 21)
4fn main() {
5    let args: Vec<String> = std::env::args().collect();
6    let input = args
7        .get(1)
8        .cloned()
9        .unwrap_or_else(|| "default input".to_string());
10    let seed = 42u64;
11    let my_params = umash::Params::derive(0, "hello example.c".as_bytes());
12    let fprint = my_params
13        .fingerprinter(seed)
14        .write(input.as_bytes())
15        .digest();
16
17    println!("Input: {}", input);
18    println!("Fingerprint: {:x}, {:x}", fprint.hash[0], fprint.hash[1]);
19    println!(
20        "Hash 0: {:x}",
21        my_params.hasher(seed).write(input.as_bytes()).digest()
22    );
23    println!(
24        "Hash 1: {:x}",
25        my_params
26            .secondary_hasher(seed)
27            .write(input.as_bytes())
28            .digest()
29    );
30
31    let mut h: umash::Hasher = (&my_params).into();
32    h.write(input.as_bytes());
33    println!("Hash: {:x}", h.finish());
34}

Trait Implementations§

Source§

impl<'params> Clone for Hasher<'params>

Source§

fn clone(&self) -> Hasher<'params>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'params> From<&'params Params> for Hasher<'params>

Converts a &Params to Hasher by constructing a fresh Hasher for these Params and seed = 0.

Source§

fn from(params: &'params Params) -> Hasher<'params>

Converts to this type from the input type.
Source§

impl Hasher for Hasher<'_>

UMASH’s collision probability bounds only hold if different objects feed different byte streams to the hasher. The standard Rust [std::Hash::hash] implementations (and automatically generated ones) satisfy this requirement for values of the same type.

Source§

fn finish(&self) -> u64

Returns the hash value for the values written so far. Read more
Source§

fn write(&mut self, bytes: &[u8])

Writes some data into this Hasher. Read more
1.3.0 · Source§

fn write_u8(&mut self, i: u8)

Writes a single u8 into this hasher.
1.3.0 · Source§

fn write_u16(&mut self, i: u16)

Writes a single u16 into this hasher.
1.3.0 · Source§

fn write_u32(&mut self, i: u32)

Writes a single u32 into this hasher.
1.3.0 · Source§

fn write_u64(&mut self, i: u64)

Writes a single u64 into this hasher.
1.26.0 · Source§

fn write_u128(&mut self, i: u128)

Writes a single u128 into this hasher.
1.3.0 · Source§

fn write_usize(&mut self, i: usize)

Writes a single usize into this hasher.
1.3.0 · Source§

fn write_i8(&mut self, i: i8)

Writes a single i8 into this hasher.
1.3.0 · Source§

fn write_i16(&mut self, i: i16)

Writes a single i16 into this hasher.
1.3.0 · Source§

fn write_i32(&mut self, i: i32)

Writes a single i32 into this hasher.
1.3.0 · Source§

fn write_i64(&mut self, i: i64)

Writes a single i64 into this hasher.
1.26.0 · Source§

fn write_i128(&mut self, i: i128)

Writes a single i128 into this hasher.
1.3.0 · Source§

fn write_isize(&mut self, i: isize)

Writes a single isize into this hasher.
Source§

fn write_length_prefix(&mut self, len: usize)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a length prefix into this hasher, as part of being prefix-free. Read more
Source§

fn write_str(&mut self, s: &str)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a single str into this hasher. Read more
Source§

impl Write for Hasher<'_>

Hashers compute the same hash value for a given sequence of bytes, regardless of the number of bytes in each write call. This makes it possible to compute a hash by calling a serialisation function.

Call Hasher::digest to find the hash value for the concatenation of all the bytes written to the Hasher.

Source§

fn flush(&mut self) -> Result<()>

Flushing a Hasher cannot compute the fingerprint, due to the trait’s interface; the implementation is a no-op.

See Hasher::digest.

Source§

fn write(&mut self, bytes: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'params> Freeze for Hasher<'params>

§

impl<'params> RefUnwindSafe for Hasher<'params>

§

impl<'params> !Send for Hasher<'params>

§

impl<'params> !Sync for Hasher<'params>

§

impl<'params> Unpin for Hasher<'params>

§

impl<'params> UnwindSafe for Hasher<'params>

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.