#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(bench, feature(test))]
#![warn(missing_docs)]
#![cfg_attr(hashes_fuzz, allow(dead_code, unused_imports))]
#![allow(clippy::needless_question_mark)] #![allow(clippy::manual_range_contains)] #![allow(clippy::needless_borrows_for_generic_args)]
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(any(test, feature = "std"))]
extern crate core;
#[cfg(feature = "serde")]
pub extern crate serde;
#[cfg(all(test, feature = "serde"))]
extern crate serde_test;
#[cfg(bench)]
extern crate test;
pub extern crate hex;
#[doc(hidden)]
pub mod _export {
pub mod _core {
pub use core::*;
}
}
#[cfg(feature = "schemars")]
extern crate schemars;
mod internal_macros;
#[macro_use]
pub mod serde_macros;
pub mod cmp;
pub mod hmac;
#[cfg(feature = "bitcoin-io")]
mod impls;
pub mod ripemd160;
pub mod sha1;
pub mod sha256;
pub mod sha256t;
pub mod sha384;
pub mod sha512;
pub mod sha512_256;
pub mod siphash24;
use core::{convert, fmt, hash};
pub trait HashEngine: Clone + Default {
type Digest: Copy
+ Clone
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ hash::Hash
+ convert::AsRef<[u8]>;
type Midstate;
const BLOCK_SIZE: usize;
fn new() -> Self { Default::default() }
fn input(&mut self, data: &[u8]);
fn n_bytes_hashed(&self) -> usize;
fn finalize(self) -> Self::Digest;
fn hash(bytes: &[u8]) -> Self::Digest {
let mut engine = Self::new();
engine.input(bytes);
engine.finalize()
}
fn hash_byte_chunks<B, I>(byte_slices: I) -> Self::Digest
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
let mut engine = Self::new();
for slice in byte_slices {
engine.input(slice.as_ref());
}
engine.finalize()
}
fn midstate(&self) -> Self::Midstate;
fn from_midstate(midstate: Self::Midstate, length: usize) -> Self;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FromSliceError {
pub expected: usize,
pub got: usize,
}
impl FromSliceError {
pub fn new(got: usize, expected: usize) -> Self { Self { got, expected } }
pub fn expected_length(&self) -> usize { self.expected }
pub fn invalid_length(&self) -> usize { self.got }
}
impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid slice length {} (expected {})", self.got, self.expected)
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromSliceError {}