Skip to content

Commit 8e9b41e

Browse files
committed
refactor: 优化代码格式,调整函数参数和调用方式
1 parent 1c0e263 commit 8e9b41e

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/api/message.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ impl WeChatClient {
108108
nonce: &str,
109109
encrypt_msg: &str,
110110
) -> bool {
111-
check_msg_signature(&self.config.token, msg_signature, timestamp, nonce, encrypt_msg)
111+
check_msg_signature(
112+
&self.config.token,
113+
msg_signature,
114+
timestamp,
115+
nonce,
116+
encrypt_msg,
117+
)
112118
}
113119

114120
/// Parse an encrypted incoming message or event from WeChat.
@@ -201,7 +207,9 @@ impl WeChatClient {
201207
let signature = compute_msg_signature(&self.config.token, timestamp, nonce, &encrypted);
202208

203209
// Generate the final encrypted XML
204-
Ok(generate_encrypted_xml(&encrypted, &signature, timestamp, nonce))
210+
Ok(generate_encrypted_xml(
211+
&encrypted, &signature, timestamp, nonce,
212+
))
205213
}
206214

207215
/// Encrypt a reply XML with auto-generated timestamp and nonce.

src/crypto.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
2-
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
2+
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
33
use rand::Rng;
44
use sha1::{Digest, Sha1};
55

@@ -38,7 +38,12 @@ fn compute_signature(token: &str, timestamp: &str, nonce: &str) -> String {
3838
///
3939
/// Used to verify incoming encrypted messages and sign outgoing encrypted messages.
4040
/// Algorithm: SHA1(sort([token, timestamp, nonce, encrypt_msg]))
41-
pub fn compute_msg_signature(token: &str, timestamp: &str, nonce: &str, encrypt_msg: &str) -> String {
41+
pub fn compute_msg_signature(
42+
token: &str,
43+
timestamp: &str,
44+
nonce: &str,
45+
encrypt_msg: &str,
46+
) -> String {
4247
let mut parts = [token, timestamp, nonce, encrypt_msg];
4348
parts.sort();
4449
let input = parts.join("");
@@ -307,8 +312,20 @@ mod tests {
307312
let encrypt_msg = "encrypted_content";
308313

309314
let sig = compute_msg_signature(token, timestamp, nonce, encrypt_msg);
310-
assert!(check_msg_signature(token, &sig, timestamp, nonce, encrypt_msg));
311-
assert!(!check_msg_signature(token, "wrong", timestamp, nonce, encrypt_msg));
315+
assert!(check_msg_signature(
316+
token,
317+
&sig,
318+
timestamp,
319+
nonce,
320+
encrypt_msg
321+
));
322+
assert!(!check_msg_signature(
323+
token,
324+
"wrong",
325+
timestamp,
326+
nonce,
327+
encrypt_msg
328+
));
312329
}
313330

314331
#[test]

0 commit comments

Comments
 (0)