-
Notifications
You must be signed in to change notification settings - Fork 23
/
hmac.rs
31 lines (27 loc) · 1013 Bytes
/
hmac.rs
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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
#![allow(non_snake_case)]
#[cfg(feature = "sha")]
#[cfg_attr(docsrs, doc(cfg(feature = "sha")))]
pub fn HMAC_SHA256(data: &[u8], key: &[u8], mac: &mut [u8; 32]) {
use hmac::Mac;
let mut m = hmac::Hmac::<sha2::Sha256>::new_from_slice(key).unwrap();
m.update(data);
mac.copy_from_slice(&m.finalize().into_bytes())
}
#[cfg(feature = "sha")]
#[cfg_attr(docsrs, doc(cfg(feature = "sha")))]
pub fn HMAC_SHA384(data: &[u8], key: &[u8], mac: &mut [u8; 48]) {
use hmac::Mac;
let mut m = hmac::Hmac::<sha2::Sha384>::new_from_slice(key).unwrap();
m.update(data);
mac.copy_from_slice(&m.finalize().into_bytes())
}
#[cfg(feature = "sha")]
#[cfg_attr(docsrs, doc(cfg(feature = "sha")))]
pub fn HMAC_SHA512(data: &[u8], key: &[u8], mac: &mut [u8; 64]) {
use hmac::Mac;
let mut m = hmac::Hmac::<sha2::Sha512>::new_from_slice(key).unwrap();
m.update(data);
mac.copy_from_slice(&m.finalize().into_bytes())
}