#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms, missing_debug_implementations)]
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;
#[cfg(feature = "rand_core")]
pub use crypto_common::rand_core;
#[cfg(feature = "zeroize")]
pub use zeroize;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "dev")]
pub mod dev;
#[cfg(feature = "block-api")]
pub mod block_api;
mod buffer_macros;
mod digest;
#[cfg(feature = "mac")]
mod mac;
mod xof_fixed;
#[cfg(feature = "block-api")]
pub use block_buffer;
#[cfg(feature = "oid")]
pub use const_oid;
pub use crypto_common;
#[cfg(feature = "const-oid")]
pub use crate::digest::DynDigestWithOid;
pub use crate::digest::{Digest, DynDigest, HashMarker};
#[cfg(feature = "mac")]
pub use crypto_common::{InnerInit, InvalidLength, Key, KeyInit};
pub use crypto_common::{Output, OutputSizeUser, Reset, array, typenum, typenum::consts};
#[cfg(feature = "mac")]
pub use mac::{CtOutput, Mac, MacError, MacMarker};
pub use xof_fixed::XofFixedWrapper;
#[cfg(feature = "block-api")]
#[deprecated(
since = "0.11.0",
note = "`digest::core_api` has been replaced by `digest::block_api`"
)]
pub use block_api as core_api;
use core::fmt;
use crypto_common::typenum::Unsigned;
pub trait Update {
fn update(&mut self, data: &[u8]);
#[must_use]
fn chain(mut self, data: impl AsRef<[u8]>) -> Self
where
Self: Sized,
{
self.update(data.as_ref());
self
}
}
pub trait FixedOutput: Update + OutputSizeUser + Sized {
fn finalize_into(self, out: &mut Output<Self>);
#[inline]
fn finalize_fixed(self) -> Output<Self> {
let mut out = Default::default();
self.finalize_into(&mut out);
out
}
}
pub trait FixedOutputReset: FixedOutput + Reset {
fn finalize_into_reset(&mut self, out: &mut Output<Self>);
#[inline]
fn finalize_fixed_reset(&mut self) -> Output<Self> {
let mut out = Default::default();
self.finalize_into_reset(&mut out);
out
}
}
pub trait XofReader {
fn read(&mut self, buffer: &mut [u8]);
#[cfg(feature = "alloc")]
fn read_boxed(&mut self, n: usize) -> Box<[u8]> {
let mut buf = vec![0u8; n].into_boxed_slice();
self.read(&mut buf);
buf
}
}
pub trait ExtendableOutput: Sized + Update {
type Reader: XofReader;
fn finalize_xof(self) -> Self::Reader;
fn finalize_xof_into(self, out: &mut [u8]) {
self.finalize_xof().read(out);
}
fn digest_xof(input: impl AsRef<[u8]>, output: &mut [u8])
where
Self: Default,
{
let mut hasher = Self::default();
hasher.update(input.as_ref());
hasher.finalize_xof().read(output);
}
#[cfg(feature = "alloc")]
fn finalize_boxed(self, output_size: usize) -> Box<[u8]> {
let mut buf = vec![0u8; output_size].into_boxed_slice();
self.finalize_xof().read(&mut buf);
buf
}
}
pub trait ExtendableOutputReset: ExtendableOutput + Reset {
fn finalize_xof_reset(&mut self) -> Self::Reader;
fn finalize_xof_reset_into(&mut self, out: &mut [u8]) {
self.finalize_xof_reset().read(out);
}
#[cfg(feature = "alloc")]
fn finalize_boxed_reset(&mut self, output_size: usize) -> Box<[u8]> {
let mut buf = vec![0u8; output_size].into_boxed_slice();
self.finalize_xof_reset().read(&mut buf);
buf
}
}
pub trait CustomizedInit: Sized {
fn new_customized(customization: &[u8]) -> Self;
}
pub trait CollisionResistance {
type CollisionResistance: Unsigned;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct InvalidOutputSize;
impl fmt::Display for InvalidOutputSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid output size")
}
}
impl core::error::Error for InvalidOutputSize {}
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct InvalidBufferSize;
impl fmt::Display for InvalidBufferSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid buffer length")
}
}
impl core::error::Error for InvalidBufferSize {}