From d8e821bae9e2496f312fc0f2742c5a55b8ce50c6 Mon Sep 17 00:00:00 2001 From: Ivan Nikulin Date: Fri, 28 Jun 2024 10:26:50 +0100 Subject: [PATCH] Expose hmac_sha1 function --- boring/src/hash.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/boring/src/hash.rs b/boring/src/hash.rs index 1b499e38..76167723 100644 --- a/boring/src/hash.rs +++ b/boring/src/hash.rs @@ -344,6 +344,11 @@ pub fn hmac_sha512(key: &[u8], data: &[u8]) -> Result<[u8; 64], ErrorStack> { hmac(MessageDigest::sha512(), key, data) } +/// Computes HMAC with SHA-1 digest. +pub fn hmac_sha1(key: &[u8], data: &[u8]) -> Result<[u8; 20], ErrorStack> { + hmac(MessageDigest::sha1(), key, data) +} + fn hmac( digest: MessageDigest, key: &[u8], @@ -437,6 +442,19 @@ mod tests { ); } + #[test] + fn test_hmac_sha1() { + let hmac = hmac_sha1(b"That's a secret".as_slice(), b"Hello world!".as_slice()).unwrap(); + + assert_eq!( + hmac, + [ + 0xe1, 0x06, 0x76, 0x46, 0x3b, 0x82, 0x67, 0xa1, 0xae, 0xe5, 0x1c, 0xfa, 0xee, 0x36, + 0x1d, 0x4b, 0xd4, 0x41, 0x6e, 0x37 + ] + ); + } + #[test] fn test_md5() { for test in MD5_TESTS.iter() {