macro_rules! hash_trait_impls {
($bits:expr, $reverse:expr $(, $gen:ident: $gent:ident)*) => {
$crate::impl_bytelike_traits!(Hash, { $bits / 8 } $(, $gen: $gent)*);
#[cfg(feature = "hex")]
$crate::impl_hex_string_traits!(Hash, { $bits / 8 }, $reverse $(, $gen: $gent)*);
#[cfg(not(feature = "hex"))]
$crate::impl_debug_only!(Hash, { $bits / 8 }, $reverse $(, $gen: $gent)*);
#[cfg(feature = "serde")]
$crate::serde_impl!(Hash, { $bits / 8} $(, $gen: $gent)*);
impl<$($gen: $gent),*> $crate::Hash for Hash<$($gen),*> {
type Bytes = [u8; $bits / 8];
const DISPLAY_BACKWARD: bool = $reverse;
fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) }
fn to_byte_array(self) -> Self::Bytes { self.to_byte_array() }
fn as_byte_array(&self) -> &Self::Bytes { self.as_byte_array() }
}
}
}
pub(crate) use hash_trait_impls;
macro_rules! general_hash_type {
($bits:expr, $reverse:expr, $doc:literal) => {
pub fn hash(data: &[u8]) -> Hash {
use crate::HashEngine as _;
let mut engine = Hash::engine();
engine.input(data);
engine.finalize()
}
pub fn hash_byte_chunks<B, I>(byte_slices: I) -> Hash
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
use crate::HashEngine as _;
let mut engine = Hash::engine();
for slice in byte_slices {
engine.input(slice.as_ref());
}
engine.finalize()
}
$crate::internal_macros::hash_type_no_default!($bits, $reverse, $doc);
impl Hash {
pub fn engine() -> HashEngine { Default::default() }
#[allow(clippy::self_named_constructors)] pub fn hash(data: &[u8]) -> Self { hash(data) }
pub fn hash_byte_chunks<B, I>(byte_slices: I) -> Self
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
hash_byte_chunks(byte_slices)
}
}
};
}
pub(crate) use general_hash_type;
macro_rules! hash_type_no_default {
($bits:expr, $reverse:expr, $doc:literal) => {
internals::transparent_newtype! {
#[doc = $doc]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Hash([u8; $bits / 8]);
impl Hash {
pub fn from_bytes_ref(bytes: &_) -> &Self;
pub fn from_bytes_mut(bytes: &mut _) -> &mut Self;
}
}
impl Hash {
pub const fn from_byte_array(bytes: [u8; $bits / 8]) -> Self { Hash(bytes) }
pub const fn to_byte_array(self) -> [u8; $bits / 8] { self.0 }
pub const fn as_byte_array(&self) -> &[u8; $bits / 8] { &self.0 }
}
$crate::internal_macros::hash_trait_impls!($bits, $reverse);
$crate::internal_macros::impl_write!(
HashEngine,
|us: &mut HashEngine, buf| {
crate::HashEngine::input(us, buf);
Ok(buf.len())
},
|_us| { Ok(()) }
);
};
}
pub(crate) use hash_type_no_default;
macro_rules! impl_write {
($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
#[cfg(feature = "std")]
impl<$($bounded_ty: $bounds),*> std::io::Write for $ty {
#[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
$write_fn(self, buf)
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
$flush_fn(self)
}
}
}
}
pub(crate) use impl_write;
macro_rules! engine_input_impl(
() => (
#[cfg(not(hashes_fuzz))]
fn input(&mut self, mut inp: &[u8]) {
while !inp.is_empty() {
let buf_idx = $crate::incomplete_block_len(self);
let rem_len = <Self as crate::HashEngine>::BLOCK_SIZE - buf_idx;
let write_len = cmp::min(rem_len, inp.len());
self.buffer[buf_idx..buf_idx + write_len]
.copy_from_slice(&inp[..write_len]);
self.bytes_hashed += write_len as u64;
if $crate::incomplete_block_len(self) == 0 {
self.process_block();
}
inp = &inp[write_len..];
}
}
#[cfg(hashes_fuzz)]
fn input(&mut self, inp: &[u8]) {
for c in inp {
self.buffer[0] ^= *c;
}
self.bytes_hashed += inp.len() as u64;
}
)
);
pub(crate) use engine_input_impl;