diff --git a/Cargo.toml b/Cargo.toml index b45820a..63e9eb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dingtalk" -version = "1.1.1" +version = "1.1.2" authors = ["Hatter Jiang "] edition = "2018" description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot, 钉钉机器人" @@ -11,11 +11,12 @@ license = "MIT" [dependencies] base64 = "0.11.0" -rust-crypto = "0.2.36" reqwest = "0.10.0" urlencoding = "1.0.0" json = "0.12.0" futures = "0.3.1" +hmac = "0.7.1" +sha2 = "0.8.1" [dev-dependencies] tokio-test = "0.2.0" diff --git a/README.md b/README.md index 0aa1808..5f89c43 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ dt.send_message(&DingTalkMessage::new_action_card("action card 002", "action car #### Changelog +* v1.1.2 + * Use `hmac` and `sha2` crates, replace `rust-crypto` crate * v1.1.1 * `DingTalk::from_json` add `direct_url` * Fix problems by clippy diff --git a/src/lib.rs b/src/lib.rs index b8531bf..118532e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,13 +11,10 @@ use std::{ ErrorKind, }, }; -use crypto::{ - mac::{ - Mac, - MacResult, - }, - hmac::Hmac, - sha2::Sha256, +use sha2::Sha256; +use hmac::{ + Hmac, + Mac, }; pub type XResult = Result>; @@ -448,7 +445,7 @@ impl <'a> DingTalk<'a> { /// Direct send JSON message pub async fn send(&self, json_message: &str) -> XResult<()> { let client = reqwest::Client::new(); - let response = match client.post(&self.generate_signed_url()) + let response = match client.post(&self.generate_signed_url()?) .header(CONTENT_TYPE, APPLICATION_JSON_UTF8) .body(json_message.as_bytes().to_vec()) .send().await { @@ -463,9 +460,9 @@ impl <'a> DingTalk<'a> { } /// Generate signed dingtalk webhook URL - pub fn generate_signed_url(&self) -> String { + pub fn generate_signed_url(&self) -> XResult { if !self.direct_url.is_empty() { - return self.direct_url.into(); + return Ok(self.direct_url.into()); } let mut signed_url = String::with_capacity(1024); signed_url.push_str(self.default_webhook_url); @@ -486,7 +483,7 @@ impl <'a> DingTalk<'a> { if self.sec_token != "" { let timestamp = &format!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis()); let timestamp_and_secret = &format!("{}\n{}", timestamp, self.sec_token); - let hmac_sha256 = base64::encode(calc_hmac_sha256(self.sec_token.as_bytes(), timestamp_and_secret.as_bytes()).code()); + let hmac_sha256 = base64::encode(&calc_hmac_sha256(self.sec_token.as_bytes(), timestamp_and_secret.as_bytes())?[..]); signed_url.push_str("×tamp="); signed_url.push_str(timestamp); @@ -494,7 +491,7 @@ impl <'a> DingTalk<'a> { signed_url.push_str(&urlencoding::encode(&hmac_sha256)); } - signed_url + Ok(signed_url) } // SAFE? may these codes cause memory leak? @@ -504,10 +501,13 @@ impl <'a> DingTalk<'a> { } /// calc hma_sha256 digest -fn calc_hmac_sha256(key: &[u8], message: &[u8]) -> MacResult { - let mut hmac = Hmac::new(Sha256::new(), key); - hmac.input(message); - hmac.result() +fn calc_hmac_sha256(key: &[u8], message: &[u8]) -> XResult> { + let mut mac = match Hmac::::new_varkey(key) { + Ok(m) => m, + Err(e) => return Err(Box::new(Error::new(ErrorKind::Other, format!("Hmac error: {}", e)))) + }; + mac.input(message); + Ok(mac.result().code().to_vec()) } #[test]