1+ extern crate line_bot_sdk_rust as line;
2+
3+ use line:: webhook:: validate_signature;
4+
15use base64:: encode;
26use hmac:: { Hmac , Mac } ;
37use sha2:: Sha256 ;
@@ -13,44 +17,51 @@ pub fn create_signature(channel_secret: &str, body: &str) -> String {
1317}
1418
1519fn main ( ) {
20+ let channel_secret: & str = "channel_secret" ;
1621 let request_body: & str = r#"
17- {
22+ {
1823 "destination": "xxxxxxxxxx",
1924 "events": [
20- {
21- "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
22- "type": "message",
23- "mode": "active",
24- "timestamp": 1462629479859,
25- "source": {
26- "type": "user",
27- "userId": "U4af4980629..."
28- },
29- "message": {
30- "id": "325708",
31- "type": "text",
32- "text": "@example Hello, world! (love)",
33- "emojis": [
34- {
35- "index": 14,
36- "length": 6,
37- "productId": "5ac1bfd5040ab15980c9b435",
38- "emojiId": "001"
25+ {
26+ "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
27+ "type": "message",
28+ "mode": "active",
29+ "timestamp": 1462629479859,
30+ "source": {
31+ "type": "user",
32+ "userId": "U4af4980629..."
33+ },
34+ "message": {
35+ "id": "325708",
36+ "type": "text",
37+ "text": "@example Hello, world! (love)",
38+ "emojis": [
39+ {
40+ "index": 14,
41+ "length": 6,
42+ "productId": "5ac1bfd5040ab15980c9b435",
43+ "emojiId": "001"
44+ }
45+ ],
46+ "mention": {
47+ "mentionees": [
48+ {
49+ "index": 0,
50+ "length": 8,
51+ "userId": "U850014438e..."
52+ }
53+ ]
54+ }
3955 }
40- ],
41- "mention": {
42- "mentionees": [
43- {
44- "index": 0,
45- "length": 8,
46- "userId": "U850014438e..."
47- }
48- ]
49- }
5056 }
51- }
5257 ]
53- }
54- "# ;
55- println ! ( "{}" , create_signature( "channel_secret" , request_body) ) ;
58+ }
59+ "# ;
60+ let signature: String = create_signature ( channel_secret, request_body) ;
61+
62+ if validate_signature ( channel_secret, & signature, request_body) {
63+ println ! ( "Success: {}" , signature) ;
64+ } else {
65+ println ! ( "NG" ) ;
66+ }
5667}
0 commit comments