Compute HMAC-SHA256 signature for webhook payload.
(secret: &str, message: &str)
| 200 | |
| 201 | /// Compute HMAC-SHA256 signature for webhook payload. |
| 202 | fn compute_hmac(secret: &str, message: &str) -> String { |
| 203 | use hmac::{Hmac, Mac}; |
| 204 | use sha2::Sha256; |
| 205 | |
| 206 | type HmacSha256 = Hmac<Sha256>; |
| 207 | |
| 208 | let Ok(mut mac) = HmacSha256::new_from_slice(secret.as_bytes()) else { |
| 209 | return String::new(); |
| 210 | }; |
| 211 | mac.update(message.as_bytes()); |
| 212 | let result = mac.finalize(); |
| 213 | result |
| 214 | .into_bytes() |
| 215 | .iter() |
| 216 | .map(|b| format!("{b:02x}")) |
| 217 | .collect() |
| 218 | } |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod tests { |