1 //! This crate provides traits which describe functionality of cryptographic hash
4 //! Traits in this repository can be separated into two levels:
5 //! - Low level traits: `Input`, `BlockInput`, `Reset`, `FixedOutput`,
6 //! `VariableOutput`, `ExtendableOutput`. These traits atomically describe
7 //! available functionality of hash function implementations.
8 //! - Convenience trait: `Digest`, `DynDigest`. They are wrappers around
9 //! low level traits for most common hash-function use-cases.
11 //! Additionally hash functions implement traits from `std`: `Default`, `Clone`,
12 //! `Write`. (the latter depends on enabled-by-default `std` crate feature)
14 //! The `Digest` trait is the most commonly used trait.
16 #![doc(html_logo_url =
17 "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
18 pub extern crate generic_array
;
19 #[cfg(feature = "std")]
20 #[macro_use] extern crate std;
21 #[cfg(feature = "dev")]
22 pub extern crate blobby
;
23 use generic_array
::{GenericArray, ArrayLength}
;
24 #[cfg(feature = "std")]
30 #[cfg(feature = "dev")]
33 pub use errors
::InvalidOutputSize
;
34 pub use digest
::Digest
;
35 #[cfg(feature = "std")]
36 pub use dyn_digest
::DynDigest
;
38 /// Trait for processing input data
40 /// Digest input data.
42 /// This method can be called repeatedly, e.g. for processing streaming
44 fn input
<B
: AsRef
<[u8]>>(&mut self, data
: B
);
46 /// Digest input data in a chained manner.
47 fn chain
<B
: AsRef
<[u8]>>(mut self, data
: B
) -> Self where Self: Sized
{
53 /// Trait to indicate that digest function processes data in blocks of size
56 /// The main usage of this trait is for implementing HMAC generically.
57 pub trait BlockInput
{
58 type BlockSize
: ArrayLength
<u8>;
61 /// Trait for returning digest result with the fixed size
62 pub trait FixedOutput
{
63 type OutputSize
: ArrayLength
<u8>;
65 /// Retrieve result and consume hasher instance.
66 fn fixed_result(self) -> GenericArray
<u8, Self::OutputSize
>;
69 /// Trait for returning digest result with the variable size
70 pub trait VariableOutput
: core
::marker
::Sized
{
71 /// Create new hasher instance with the given output size.
73 /// It will return `Err(InvalidOutputSize)` in case if hasher can not return
74 /// specified output size. It will always return an error if output size
76 fn new(output_size
: usize) -> Result
<Self, InvalidOutputSize
>;
78 /// Get output size of the hasher instance provided to the `new` method
79 fn output_size(&self) -> usize;
81 /// Retrieve result via closure and consume hasher.
83 /// Closure is guaranteed to be called, length of the buffer passed to it
84 /// will be equal to `output_size`.
85 fn variable_result
<F
: FnOnce(&[u8])>(self, f
: F
);
87 /// Retrieve result into vector and consume hasher.
88 #[cfg(feature = "std")]
89 fn vec_result(self) -> Vec
<u8> {
90 let mut buf
= Vec
::with_capacity(self.output_size());
91 self.variable_result(|res
| buf
.extend_from_slice(res
));
96 /// Trait for describing readers which are used to extract extendable output
97 /// from XOF (extendable-output function) result.
99 /// Read output into the `buffer`. Can be called unlimited number of times.
100 fn read(&mut self, buffer
: &mut [u8]);
103 /// Trait which describes extendable-output functions (XOF).
104 pub trait ExtendableOutput
: core
::marker
::Sized
{
105 type Reader
: XofReader
;
107 /// Retrieve XOF reader and consume hasher instance.
108 fn xof_result(self) -> Self::Reader
;
110 /// Retrieve result into vector of specified length.
111 #[cfg(feature = "std")]
112 fn vec_result(self, n
: usize) -> Vec
<u8> {
113 let mut buf
= vec
![0u8; n
];
114 self.xof_result().read(&mut buf
);
119 /// Trait for resetting hash instances
121 /// Reset hasher instance to its initial state and return current state.
126 /// Implements `std::io::Write` trait for implementer of `Input`
127 macro_rules
! impl_write
{
129 #[cfg(feature = "std")]
130 impl ::std
::io
::Write
for $hasher
{
131 fn write(&mut self, buf
: &[u8]) -> ::std
::io
::Result
<usize> {
132 Input
::input(self, buf
);
136 fn flush(&mut self) -> ::std
::io
::Result
<()> {