Skip to main content

Hash256

Struct Hash256 

Source
#[repr(transparent)]
pub struct Hash256(pub [u8; 32]);

Tuple Fields§

§0: [u8; 32]

Implementations§

Source§

impl<'a> Hash256

Source

pub fn new(bytes: &[u8; 32]) -> Self

Source

pub fn bytes(&'a self) -> Array<'a, u8, 32>

Source

pub fn set_bytes(&mut self, items: &[u8; 32])

Source§

impl Hash256

Source

pub fn digest<T: AsRef<[u8]>>(bytes: &T) -> Self

Creates a new hash from a byte representation of some data.

let data = "This is some data that can be hashed !";
let hash = Hash256::digest(&data);
Source

pub fn digest_w_x00<T: AsRef<[u8]>>(bytes: &T) -> Self

Creates a new hash from a byte representation of some data.

A special byte \x00 is fed into the hasher, before the data is added. Can be used for merkle hash trees that conform to RFC6962.

Source

pub fn digest_w_x01<T: AsRef<[u8]>>(bytes: &T) -> Self

Creates a new hash from a byte representation of some data.

A special byte \x01 is fed into the hasher, before the data is added. Can be used for merkle hash trees that conform to RFC6962.

Source

pub fn zero() -> Self

Creates a Hash256, where all bits are set to 0.

This is mostly used for testing, where we want to create hashes without some data.

Source

pub fn empty() -> Self

Creates a new hash without any data.

This returns the initial state of the hasher, without updating it with data. The following variants all create the same output hash:

let hash = Hash256::empty();
let h2 = Hash256::digest(b"");
let mut hasher = Hasher::new();
let h3 = hasher.finalize();

assert_eq!(hash, h2);
assert_eq!(hash, h3);
Source

pub fn sum(h1: &Hash256, h2: &Hash256) -> Hash256

Constructs a new hash from two other hashes.

Note: The sum of two hashes is not commutative, so the order of the hashes matter:

let h1 = Hash256::empty();
let h2 = Hash256::zero();

assert_ne!(Hash256::sum(&h1, &h2), Hash256::sum(&h2, &h1));

Alternatively you can use the + operator directly, since the Hash256 implements Add:

let h1 = Hash256::empty();
let h2 = Hash256::zero();
let sum = Hash256::sum(&h1, &h2);

assert_eq!(h1 + h2, sum);
Source

pub fn to_u64(&self) -> u64

Converts the hash to an u64 value.

The Hash256 can also be used to generate random values (which are equally distributed, due to the cryptographic properties of the underlying hash-function).

Source

pub fn into_vec(self) -> Vec<u8>

Consumes the hash and returns the underlying byte-slice as a vector

Source

pub fn into_slice(self) -> [u8; 32]

Consumes the hash and returns the underlying byte-slice

Trait Implementations§

Source§

impl Add for Hash256

Source§

type Output = Hash256

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AsRef<[u8; 32]> for Hash256

Source§

fn as_ref(&self) -> &[u8; 32]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for Hash256

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Hash256

Source§

fn clone(&self) -> Hash256

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 Hash256

Source§

impl Debug for Hash256

Source§

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

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

impl Default for Hash256

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Hash256

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Hash256

Source§

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

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

impl Eq for Hash256

Source§

impl<'a> Follow<'a> for Hash256

Source§

type Inner = &'a Hash256

Source§

unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner

Safety Read more
Source§

impl<'a> Follow<'a> for &'a Hash256

Source§

type Inner = &'a Hash256

Source§

unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner

Safety Read more
Source§

impl From<&[u8; 32]> for Hash256

Source§

fn from(slice: &[u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl From<GenericArray<u8, <CoreWrapper<Sha3_256Core> as OutputSizeUser>::OutputSize>> for Hash256

Source§

fn from(hash: Output<Sha3_256>) -> Self

Converts to this type from the input type.
Source§

impl From<Hash256> for u64

Source§

fn from(hash: Hash256) -> Self

Converts to this type from the input type.
Source§

impl From<Hash256> for [u8; 32]

Source§

fn from(h: Hash256) -> Self

Converts to this type from the input type.
Source§

impl From<Hash256> for Vec<u8>

Source§

fn from(h: Hash256) -> Self

Converts to this type from the input type.
Source§

impl From<Hash256> for String

Source§

fn from(h: Hash256) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 32]> for Hash256

Source§

fn from(slice: [u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl Hash for Hash256

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 Hashed for Hash256

Source§

fn hash(&self) -> &Hash256

Returns a reference to the pre-calculated hash of the datatype.
Source§

impl Ord for Hash256

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Hash256

Source§

fn eq(&self, other: &Hash256) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Hash256

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'b> Push for Hash256

Source§

type Output = Hash256

Source§

unsafe fn push(&self, dst: &mut [u8], _written_len: usize)

Safety Read more
Source§

fn size() -> usize

Source§

fn alignment() -> PushAlignment

Source§

impl Serialize for Hash256

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl SimpleToVerifyInSlice for Hash256

Source§

impl StructuralPartialEq for Hash256

Source§

impl TryFrom<&Vec<u8>> for Hash256

Source§

type Error = InvalidSliceLength

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

fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&[u8]> for Hash256

Source§

type Error = InvalidSliceLength

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

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Vec<u8>> for Hash256

Source§

type Error = InvalidSliceLength

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

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> Verifiable for Hash256

Source§

fn run_verifier( v: &mut Verifier<'_, '_>, pos: usize, ) -> Result<(), InvalidFlatbuffer>

Runs the verifier for this type, assuming its at position pos in the verifier’s buffer. Should not need to be called directly.

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Hashable for T
where T: Hashed,

Source§

fn calc_hash(&self) -> Hash256

Calculates and returns the hash of the datatype
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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.