-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathhmac_impl.rs
More file actions
58 lines (49 loc) · 1.6 KB
/
Copy pathhmac_impl.rs
File metadata and controls
58 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use hmac::digest::{
Digest, FixedOutput, KeyInit, Output, Update,
block_api::{BlockSizeUser, OutputSizeUser},
};
use hmac::{EagerHash, Hmac, SimpleHmac};
/// Trait representing a HMAC implementation.
///
/// Most users should use [`Hmac`] or [`SimpleHmac`].
pub trait HmacImpl<H: OutputSizeUser>: Clone {
/// Create new HMAC state with the given key.
fn new_from_slice(key: &[u8]) -> Self;
/// Update HMAC state.
fn update(&mut self, data: &[u8]);
/// Finalize the HMAC state and get generated tag.
fn finalize(self) -> Output<H>;
}
impl<H: EagerHash> HmacImpl<H> for Hmac<H> {
#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
}
#[inline(always)]
fn update(&mut self, data: &[u8]) {
Update::update(self, data);
}
#[inline(always)]
fn finalize(self) -> Output<H> {
Output::<H>::try_from(&self.finalize_fixed()[..])
.expect("Output<H> and Output<Hmac<H>> are always equal to each other")
}
}
impl<H> HmacImpl<H> for SimpleHmac<H>
where
H: Digest + BlockSizeUser + Clone,
{
#[inline(always)]
fn new_from_slice(key: &[u8]) -> Self {
KeyInit::new_from_slice(key).expect("HMAC can take a key of any size")
}
#[inline(always)]
fn update(&mut self, data: &[u8]) {
Update::update(self, data);
}
#[inline(always)]
fn finalize(self) -> Output<H> {
Output::<H>::try_from(&self.finalize_fixed()[..])
.expect("Output<H> and Output<SimpleHmac<H>> are always equal to each other")
}
}