Struct massa_hash::hash::Hash
source · pub struct Hash(Hash);
Expand description
Hash wrapper, the underlying hash type is Blake3
The motivations for selecting Blake3 were- Speed: Blake3 is significantly faster than other popular hashing algorithms, such as SHA-256 and SHA-3. This is largely due to its ability to leverage modern CPU architectures and instruction sets, as well as its optimized implementation.
Security: Blake3 is designed to be highly secure and resistant to a wide range of attacks, including collision attacks, length-extension attacks, and timing attacks. It also offers better resistance to side-channel attacks than many other hashing algorithms.
Flexibility: Blake3 is highly flexible and can be used in a variety of applications, including as a general-purpose hash function, as a key derivation function, and as a message authentication code. It also supports a wide range of input sizes and can produce output of any desired length. Scalability: Blake3 can efficiently take advantage of multiple cores and SIMD (single instruction, multiple data) instructions, allowing it to scale well on modern CPUs.
Improved Compression Function: The compression function used in Blake3 is an improved version of the one used in its predecessor, Blake2. This improved compression function offers better diffusion and mixing properties, which contributes to its increased security.
Keyed Hashing: Blake3 supports keyed hashing, which allows users to use a secret key to generate a unique hash value. This feature can be useful in applications that require message authentication or integrity verification.
Tree Hashing: Blake3 supports tree hashing, which allows users to hash large files or data structures in a parallel and efficient manner. This feature can be useful in applications that involve large-scale data processing, such as cloud storage or distributed file systems.
Open Source: Blake3 is an open-source algorithm, which means that its code is publicly available for review and auditing by anyone. This can help improve its security and reliability, as well as increase transparency and trust among users.
Tuple Fields§
§0: Hash
Implementations§
source§impl Hash
impl Hash
sourcepub fn compute_from(data: &[u8]) -> Self
pub fn compute_from(data: &[u8]) -> Self
sourcepub fn compute_from_tuple(data: &[&[u8]]) -> Self
pub fn compute_from_tuple(data: &[&[u8]]) -> Self
Compute a hash from tuple of byte arrays.
§Example
let hash = Hash::compute_from_tuple(&[&"hello".as_bytes(), &"world".as_bytes()]);
sourcepub fn to_bs58_check(&self) -> String
pub fn to_bs58_check(&self) -> String
Serialize a Hash using bs58
encoding with checksum.
§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = hash.to_bs58_check();
Motivations for using base58 encoding:
base58_check is like base64 but-
- fully standardized (no = vs /)
- no weird characters (eg. +) only alphanumeric
- ambiguous letters combined (eg. O vs 0, or l vs 1)
- contains a checksum at the end to detect typing errors
sourcepub fn to_bytes(&self) -> &[u8; 32]
pub fn to_bytes(&self) -> &[u8; 32]
Serialize a Hash as bytes.
§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized = hash.to_bytes();
sourcepub fn into_bytes(self) -> [u8; 32]
pub fn into_bytes(self) -> [u8; 32]
Convert into bytes.
§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized = hash.into_bytes();
sourcepub fn from_bs58_check(data: &str) -> Result<Hash, MassaHashError>
pub fn from_bs58_check(data: &str) -> Result<Hash, MassaHashError>
Deserialize using bs58
encoding with checksum.
§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = hash.to_bs58_check();
let deserialized: Hash = Hash::from_bs58_check(&serialized).unwrap();
Trait Implementations§
source§impl<'de> Deserialize<'de> for Hash
impl<'de> Deserialize<'de> for Hash
source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hash, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hash, D::Error>
::serde::Deserialize
trait for Hash
if the deserializer is human readable,
deserialization is done using deserialize_bs58_check
else, it uses deserialize_binary
§Example
Human readable deserialization :
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = serde_json::to_string(&hash).unwrap();
let deserialized: Hash = serde_json::from_str(&serialized).unwrap();
source§impl Deserializer<Hash> for HashDeserializer
impl Deserializer<Hash> for HashDeserializer
source§fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
&self,
buffer: &'a [u8],
) -> IResult<&'a [u8], Hash, E>
fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( &self, buffer: &'a [u8], ) -> IResult<&'a [u8], Hash, E>
§Example
use massa_hash::{Hash, HashDeserializer};
use massa_serialization::{Serializer, Deserializer, DeserializeError};
let hash_deserializer = HashDeserializer::new();
let hash = Hash::compute_from(&"hello world".as_bytes());
let (rest, deserialized) = hash_deserializer.deserialize::<DeserializeError>(hash.to_bytes()).unwrap();
assert_eq!(deserialized, hash);
assert_eq!(rest.len(), 0);
source§impl Ord for Hash
impl Ord for Hash
In massa, this function is generally useful for data structures that performs ordering and where hashes are used as keys. For e.g., it is used for the BTreeMap where the order of the addresses is to be maintained. This function helps to have a single coherent BTreeMap which is then used to perform the draw See Pos-Worker for more details.
source§impl PartialEq for Hash
impl PartialEq for Hash
source§impl PartialOrd for Hash
impl PartialOrd for Hash
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for Hash
impl Serialize for Hash
source§fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>
::serde::Serialize
trait for Hash
if the serializer is human readable,
serialization is done using serialize_bs58_check
else, it uses serialize_binary
§Example
Human readable serialization :
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = serde_json::to_string(&hash).unwrap();
source§impl Serializer<Hash> for HashSerializer
impl Serializer<Hash> for HashSerializer
impl Copy for Hash
impl Eq for Hash
impl StructuralPartialEq for Hash
Auto Trait Implementations§
impl Freeze for Hash
impl RefUnwindSafe for Hash
impl Send for Hash
impl Sync for Hash
impl Unpin for Hash
impl UnwindSafe for Hash
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)