sha_to_hex/
lib.rs

1use const_hex::encode as hex_encode;
2use ring::hmac::{sign, Key, HMAC_SHA256};
3/// Encode a message using secret with HMAC-SHA256 encryption
4/// 
5/// returns a hex string
6///
7/// ### Usage
8/// `encode(msg: &str, secret: &str) -> String`
9///
10/// ### Example
11/// ```
12/// use sha_to_hex::encode;
13///
14/// let msg = "The quick brown fox jumps over the lazy dog";
15/// let secret = "543jklefdsjio342jiofdsoij5234orfjdso";
16///
17/// assert_eq!(encode(msg, secret),
18///            "277288e6f134b423756870fbf222f56c8e17d59d9a2df0f8c535a5ab3f84a9bb")
19/// ```
20#[inline(always)]
21pub fn encode(msg: &str, secret: &str) -> String
22{
23    encode_bytes(msg.as_bytes(), secret.as_bytes())
24}
25
26/// Encode a message using secret with HMAC-SHA256 encryption
27/// 
28/// returns a hex string
29/// 
30/// ### Usage
31/// `encode_bytes(msg: &[u8], secret: &[u8]) -> String`
32#[inline(always)]
33pub fn encode_bytes(msg: &[u8], secret: &[u8]) -> String
34{
35    hex_encode(&sign(&Key::new(HMAC_SHA256, secret), msg))
36}
37
38/// [Deprecated] Encode msg using secret return hex string
39/// This is just alias of `encode` for backward compatibility
40/// 
41/// use `encode` instead
42#[inline(always)]
43pub fn get_sha256_hmac_as_hex_str(msg: &str, secret: &str) -> String
44{
45    encode(msg, secret)
46}