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
//! Functions for webhook

use base64::encode;
use hmac::{Hmac, Mac};
use sha2::Sha256;

/// Signature validator
/// # Note
/// The signature in the `x-line-signature` request header must be verified to confirm that the request was sent from the LINE Platform. [\[detail\]](https://developers.line.biz/en/reference/messaging-api/#signature-validation)
/// # Example
/// ```
/// if webhook::validate_signature(channel_secret, signature, body) {
///     // OK
/// } else {
///     // NG
/// }
/// ```
pub fn validate_signature(channel_secret: &str, signature: &str, body: &str) -> bool {
    type HmacSha256 = Hmac<Sha256>;

    let mut mac =
        HmacSha256::new_varkey(channel_secret.as_bytes()).expect("HMAC can take key of any size");
    mac.input(body.as_bytes());
    encode(&mac.result().code().to_vec()) == signature
}