wx_rust_common/util/sign_utils.rs
1//! 签名工具类。
2//!
3//! 对应 Java `me.chanjar.weixin.common.util.SignUtils`。
4
5use hmac::{Hmac, KeyInit, Mac};
6use sha2::Sha256;
7
8/// HmacSHA256 签名器类型
9type HmacSha256 = Hmac<Sha256>;
10
11/// 签名工具。
12pub struct SignUtils;
13
14impl SignUtils {
15 /// 生成 HmacSHA256 签名(十六进制大写)。
16 ///
17 /// # 参数
18 /// - `message`:签名数据
19 /// - `key`:签名密钥
20 ///
21 /// # 返回
22 /// 十六进制大写签名结果
23 pub fn create_hmac_sha256_sign(message: &str, key: &str) -> String {
24 let mut mac = HmacSha256::new_from_slice(key.as_bytes()).expect("HMAC 初始化不应失败");
25 mac.update(message.as_bytes());
26 let bytes = mac.finalize().into_bytes();
27 hex::encode_upper(bytes)
28 }
29}