pub enum GeneralHash {
SHA512([u8; 64]),
SHA256([u8; 32]),
SHA1([u8; 20]),
XXH64([u8; 8]),
XXH32([u8; 4]),
NULL,
}Expand description
GeneralHash is an enum that represents a hash value.
The hash value is stored as a byte array of a fixed size. The size of the byte array depends on the hash function used.
The following hash functions are supported: SHA512, SHA256, SHA1, XXH64, XXH32, and NULL.
The hash_type method returns the type of the hash function used.
The hasher method returns a new instance of a GeneralHasher trait object that corresponds to the hash type.
The hasher can then be used to compute a hash of that kind.
§Traits
Display- to allow formatting aGeneralHashinto a string.FromStr- to allow parsing a string into aGeneralHash.Serialize- to allow serializing aGeneralHashinto a string.Deserialize- to allow deserializing aGeneralHashfrom a string.
§Examples
use std::str::FromStr;
use backup_deduplicator::hash::{GeneralHash, GeneralHashType};
#[cfg(feature = "hash-sha2")]
{
let hash = GeneralHash::from_str("SHA256:315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3").unwrap();
let mut hasher = GeneralHashType::SHA256.hasher();
hasher.update(b"Hello, world!".as_slice());
let new_hash = hasher.finalize();
assert_eq!(hash, new_hash);
assert_eq!(hash.to_string(), new_hash.to_string());
}§See also
- GeneralHashType - representation of the different types of hash functions.
- GeneralHasher - trait for computing hash values.
Variants§
Implementations§
Source§impl GeneralHash
impl GeneralHash
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the hash value as a byte array.
§Returns
A reference to the byte array that represents the hash value.
Sourcepub fn new_sha512() -> Self
pub fn new_sha512() -> Self
Returns a new instance of a SHA512 hash value.
Sourcepub fn new_sha256() -> Self
pub fn new_sha256() -> Self
Returns a new instance of a SHA256 hash value.
Sourcepub fn hash_type(&self) -> GeneralHashType
pub fn hash_type(&self) -> GeneralHashType
Sourcepub fn from_type(hash_type: GeneralHashType) -> Self
pub fn from_type(hash_type: GeneralHashType) -> Self
Sourcepub fn hasher(&self) -> Box<dyn GeneralHasher>
pub fn hasher(&self) -> Box<dyn GeneralHasher>
Returns a new instance of a GeneralHash with the specified hash type.
§Arguments
hash_type- The type of the hash function to use.
§Returns
A new instance of a GeneralHash with the specified hash type.
§See also
- GeneralHashType - representation of the different types of hash functions.
Sourcepub fn hash_directory<'a>(
&mut self,
children: impl Iterator<Item = &'a BuildFile>,
) -> Result<u64>
pub fn hash_directory<'a>( &mut self, children: impl Iterator<Item = &'a BuildFile>, ) -> Result<u64>
Trait Implementations§
Source§impl Clone for GeneralHash
impl Clone for GeneralHash
Source§fn clone(&self) -> GeneralHash
fn clone(&self) -> GeneralHash
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GeneralHash
impl Debug for GeneralHash
Source§impl<'de> Deserialize<'de> for GeneralHash
impl<'de> Deserialize<'de> for GeneralHash
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for GeneralHash
impl Display for GeneralHash
Source§impl FromStr for GeneralHash
impl FromStr for GeneralHash
Source§fn from_str(hex: &str) -> Result<Self, Self::Err>
fn from_str(hex: &str) -> Result<Self, Self::Err>
Parses a string into a GeneralHash.
§Arguments
hex- The string to parse, in the formathash_type:hash_data (hex).
§Returns
The GeneralHash that corresponds to the string or an error.
§Errors
Returns an error if the string does not correspond to a GeneralHash.
- If the hash type is not recognized.
- If the hash data is not valid (wrong length or non-hex string).