use wx_rust_common::util::crypto::{EncryptContext, WxCryptUtil};
use crate::config::WxOpenConfigStorage;
#[derive(Debug, Clone)]
pub struct WxOpenCryptUtils {
inner: WxCryptUtil,
}
impl WxOpenCryptUtils {
pub fn new(config: &dyn WxOpenConfigStorage) -> Result<Self, String> {
let aes_key = config
.component_aes_key()
.unwrap_or_default()
.replace(' ', "");
let inner = WxCryptUtil::new(
config.component_token().unwrap_or_default(),
aes_key,
config.component_app_id().unwrap_or_default(),
)?;
Ok(Self { inner })
}
pub fn decrypt_xml(
&self,
msg_signature: &str,
timestamp: &str,
nonce: &str,
encrypted_xml: &str,
) -> Result<String, String> {
self.inner
.decrypt_xml(msg_signature, timestamp, nonce, encrypted_xml)
}
pub fn decrypt_content(
&self,
msg_signature: &str,
timestamp: &str,
nonce: &str,
cipher_text: &str,
) -> Result<String, String> {
self.inner
.decrypt_content(msg_signature, timestamp, nonce, cipher_text)
}
pub fn encrypt(&self, plain_xml: &str) -> Result<String, String> {
self.inner.encrypt(plain_xml)
}
pub fn encrypt_context(&self, plain_xml: &str) -> Result<EncryptContext, String> {
self.inner.encrypt_context(plain_xml)
}
}