wx_rust_open/util/crypto/
wx_open_crypt_utils.rs1use wx_rust_common::util::crypto::{EncryptContext, WxCryptUtil};
15
16use crate::config::WxOpenConfigStorage;
17
18#[derive(Debug, Clone)]
20pub struct WxOpenCryptUtils {
21 inner: WxCryptUtil,
22}
23
24impl WxOpenCryptUtils {
25 pub fn new(config: &dyn WxOpenConfigStorage) -> Result<Self, String> {
30 let aes_key = config
32 .component_aes_key()
33 .unwrap_or_default()
34 .replace(' ', "");
35 let inner = WxCryptUtil::new(
36 config.component_token().unwrap_or_default(),
37 aes_key,
38 config.component_app_id().unwrap_or_default(),
39 )?;
40 Ok(Self { inner })
41 }
42
43 pub fn decrypt_xml(
47 &self,
48 msg_signature: &str,
49 timestamp: &str,
50 nonce: &str,
51 encrypted_xml: &str,
52 ) -> Result<String, String> {
53 self.inner
54 .decrypt_xml(msg_signature, timestamp, nonce, encrypted_xml)
55 }
56
57 pub fn decrypt_content(
59 &self,
60 msg_signature: &str,
61 timestamp: &str,
62 nonce: &str,
63 cipher_text: &str,
64 ) -> Result<String, String> {
65 self.inner
66 .decrypt_content(msg_signature, timestamp, nonce, cipher_text)
67 }
68
69 pub fn encrypt(&self, plain_xml: &str) -> Result<String, String> {
71 self.inner.encrypt(plain_xml)
72 }
73
74 pub fn encrypt_context(&self, plain_xml: &str) -> Result<EncryptContext, String> {
76 self.inner.encrypt_context(plain_xml)
77 }
78}