crypto_api/
hash.rs

1use std::error::Error;
2use std::ops::Range;
3
4
5/// Information about a hash implementation
6#[derive(Debug, Eq, PartialEq, Clone)]
7pub struct HashInfo {
8	/// The name
9	pub name: &'static str,
10	
11	/// The default/selected hash length
12	pub hash_len: usize,
13	/// the supported hash lengths
14	pub hash_len_r: Range<usize>
15}
16
17
18/// A stateless (oneshot) hash interface
19pub trait Hash {
20	/// Returns information about the hash
21	fn info(&self) -> HashInfo;
22	
23	/// Hashes `data` into `buf` and returns the hash length
24	fn hash(&self, buf: &mut[u8], data: &[u8]) -> Result<usize, Box<dyn Error + 'static>>;
25}
26
27/// A variable length extension for `Hash`
28pub trait VarlenHash: Hash {
29	/// Hashes `data` into `buf` and returns the hash length
30	fn varlen_hash(&self, buf: &mut[u8], data: &[u8]) -> Result<usize, Box<dyn Error + 'static>>;
31}
32
33
34/// A stateful (streaming) hash interface
35pub trait StreamingHash {
36	/// Returns information about the hash
37	fn info(&self) -> HashInfo;
38	
39	/// (Re-)initializes the hash state
40	fn init(&mut self) -> Result<(), Box<dyn Error + 'static>>;
41	/// Adds the data in `input` to the hash state
42	fn update(&mut self, input: &[u8]) -> Result<(), Box<dyn Error + 'static>>;
43	/// Computes the hash into `buf` and returns the hash length
44	fn finish(&mut self, buf: &mut[u8]) -> Result<usize, Box<dyn Error + 'static>>;
45}
46
47/// A variable length extension for `StreamingHash`
48pub trait StreamingVarlenHash: StreamingHash {
49	/// (Re-)initializes the hash state to produce an `hash_len`-sized hash
50	fn varlen_init(&mut self, hash_len: usize) -> Result<(), Box<dyn Error + 'static>>;
51}