pub struct Shake128 { /* private fields */ }Expand description
The Shake128 hasher.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
assert!(hasher.try_update(input.as_slice()).is_ok());
let finalized = hasher.try_finalize::<64>().unwrap();
assert_ne!(finalized.as_slice(), input.as_slice());
assert_eq!(finalized.len(), 64);Implementations§
Source§impl Shake128
impl Shake128
Sourcepub fn new() -> Result<Self, Unspecified>
pub fn new() -> Result<Self, Unspecified>
Create a new Shake128 instance.
§Errors
If the underlying initialization function fails (wc_InitShake128)
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
assert!(hasher.try_update(input.as_slice()).is_ok());
let finalized = hasher.try_finalize::<32>()
.unwrap();
assert_eq!(finalized.len(), 32);
assert_ne!(finalized.as_slice(), input.as_slice());Sourcepub unsafe fn update_unchecked(&mut self, data: &[u8]) -> Res
pub unsafe fn update_unchecked(&mut self, data: &[u8]) -> Res
Update the underlying wc_Shake instance, without performing any safety checks.
§Safety
The length of data is casted to a 32 bit unsigned integer without checking for overflows. While it is incredibly unlikely that this overflow will ever take place, it is not impossible. Thus this function is marked unsafe.
§Arguments
data- The slice to update the underlying hasher state with.
§Returns
This function returns the result of the operation.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
// SAFETY: The length of `hello world` is 11, which
// cannot overflow even an 8 bit integer.
let res = unsafe {
hasher.update_unchecked(input.as_slice())
};
assert!(res.is_ok());
let finalized = hasher.finalize_default().unwrap();
assert_ne!(finalized.as_slice(), input.as_slice());Sourcepub fn try_update(&mut self, data: &[u8]) -> Res
pub fn try_update(&mut self, data: &[u8]) -> Res
Update the underlying wc_Shake instance.
§Arguments
data- The slice to update the underlying hasher state with.
§Returns
This function returns the result of the operation.
§Errors
- If the length of
datacannot be safely casted to au32. - If the underlying
wc_Shake128_Updatefunction fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
assert!(hasher.try_update(input.as_slice()).is_ok());
let finalized = hasher.finalize_default().unwrap();
assert_ne!(finalized.as_slice(), input.as_slice());
assert_eq!(finalized.len(), 32);Note: if the size of the data is known at compile time, see
update_sized for a slight optimization as the safety checks are done at
compilation time.
Sourcepub fn update_sized<const C: usize>(&mut self, data: &[u8; C]) -> Res
pub fn update_sized<const C: usize>(&mut self, data: &[u8; C]) -> Res
Update the underlying wc_Shake instance, with the safety checks performed at compilation time.
§Arguments
data- The slice to update the underlying hasher state with.
§Returns
This function returns the result of the operation.
§Errors
- If the length of
datacannot be safely casted to au32. - If the underlying
wc_Shake128_Updatefunction fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
assert!(hasher.update_sized(input).is_ok());
let finalized = hasher.finalize_default().unwrap();
assert_ne!(finalized.as_slice(), input.as_slice());
assert_eq!(finalized.len(), 32);Note: if the size of the data is not known at compile time, see
try_update for more flexibility.
Sourcepub fn update(&mut self, data: &[u8])
Available on crate feature can-panic only.
pub fn update(&mut self, data: &[u8])
can-panic only.Update the underlying wc_Shake, panicking under any failure.
§Arguments
data- The slice to update the underlying hasher state with.
§Panics
- If the length of
datacannot be safely casted to au32. - If the underlying
wc_Shake128_Updatefunction fails.
If a panic under any failure is not acceptable for your use case, which
generally is true, please consider using try_update.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
let input = b"hello world";
hasher.update(input.as_slice());
let finalized = hasher.try_finalize::<64>().unwrap();
assert_ne!(finalized.as_slice(), input.as_slice());
assert_eq!(finalized.len(), 64);Sourcepub unsafe fn finalize_unchecked(&mut self, output: &mut [u8]) -> Res
pub unsafe fn finalize_unchecked(&mut self, output: &mut [u8]) -> Res
Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state without performing any safety checks on the output buffer size.
§Safety
The length of output is casted to a 32 bit unsigned integer without checking
for overflows. While it is incredibly unlikely that this overflow will ever
take place, it is not impossible. Thus this function is marked unsafe.
§Arguments
output- The buffer to store the variable-length output digest. Its length must be manually verified.
§Errors
If the underlying finalize function fails, the returned result will contain an error.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let mut output = [0u8; 64];
unsafe {
let res = hasher.finalize_unchecked(&mut output);
assert!(res.is_ok());
}Note: Prefer using finalize_into or finalize_into_sized where possible to
benefit from safety checks.
Sourcepub fn finalize_into(&mut self, output: &mut [u8]) -> Res
pub fn finalize_into(&mut self, output: &mut [u8]) -> Res
Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state.
§Arguments
output- The buffer to store the variable-length output digest.
§Errors
- If the size of
outputexceeds what can be represented as au32. - If the underlying finalize function fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let mut output = [0u8; 64];
let res = hasher.finalize_into(output.as_mut_slice());
assert!(res.is_ok());Note: If the size of the output slice is known at compile time, see
finalize_into_sized for a slight optimization.
Sourcepub fn finalize_into_sized<const C: usize>(
&mut self,
output: &mut [u8; C],
) -> Res
pub fn finalize_into_sized<const C: usize>( &mut self, output: &mut [u8; C], ) -> Res
Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state, with the safety checks performed at compilation time.
§Arguments
output- The buffer to store the variable-length output digest.
§Errors
- If the size of
outputexceeds what can be represented as au32. - If the underlying finalize function fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let mut output = [0u8; 64];
let res = hasher.finalize_into_sized(&mut output);
assert!(res.is_ok());Note: If the size of the output buffer is not known at compilation time,
see finalize_into for greater flexibility.
Sourcepub fn try_finalize<const C: usize>(&mut self) -> Result<[u8; C], Unspecified>
pub fn try_finalize<const C: usize>(&mut self) -> Result<[u8; C], Unspecified>
Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state, returning a buffer of the specified output size.
§Returns
On success, this returns the output digest of the given size.
§Errors
If the underlying finalize function fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let res = hasher.try_finalize::<64>().unwrap();
assert_ne!(res.as_slice(), input.as_slice());Sourcepub fn finalize_default(&mut self) -> Result<[u8; 32], Unspecified>
pub fn finalize_default(&mut self) -> Result<[u8; 32], Unspecified>
Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state with the default digest size of 32 bytes.
§Returns
On success, this returns the default output digest.
§Errors
If the underlying finalize function fails.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let res = hasher.finalize_default().unwrap();
assert_ne!(res.as_slice(), input.as_slice());Sourcepub fn finalize<const C: usize>(&mut self) -> [u8; C]
Available on crate feature can-panic only.
pub fn finalize<const C: usize>(&mut self) -> [u8; C]
can-panic only.Calls the wc_Shake128_Final function, finalizing the extensible hashing of data and resetting the underlying wc_Shake instance’s state.
§Panics
If the underlying finalize function fails. If panicking is not acceptable for your
use case, see try_finalize or finalize_into instead.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
// Use the hasher ...
let res = hasher.try_finalize::<24>().unwrap();
assert_ne!(res.as_slice(), input.as_slice());Trait Implementations§
Source§impl Clone for Shake128
impl Clone for Shake128
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Copy the state of the hash (calls the wc_Shake128_Copy function)
§Returns
A distinct new instance of Shake128, with the same state as the hasher that was cloned.
§Example
use wolf_crypto::hash::Shake128;
let mut hasher = Shake128::new().unwrap();
assert!(hasher.update_sized(b"hello world").is_ok());
let mut cloned = hasher.clone();
assert_eq!(
cloned.finalize_default().unwrap(),
hasher.finalize_default().unwrap(),
"The two hashers should have the same output"
);1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more