wechat_work_crypto 0.1.0

Pure Rust implementation of WeChat Work (企业微信) message encryption/decryption library
Documentation
#![allow(warnings)]

use wechat_work_crypto::WXBizMsgCrypt;

#[test]
fn test_generate_valid_key() {
    // Generate 32 random bytes
    let key_bytes: [u8; 32] = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
    ];
    
    // Encode to base64 (should be 44 chars including padding)
    let base64_encoded = base64::encode(&key_bytes);
    println!("Base64 encoded (44 chars): {} (length: {})", base64_encoded, base64_encoded.len());
    
    // Remove padding to get 43 chars (WeChat format)
    let encoding_key = &base64_encoded[..43];
    println!("Encoding key (43 chars): {} (length: {})", encoding_key, encoding_key.len());
    
    // Test that we can create WXBizMsgCrypt with this key
    let result = WXBizMsgCrypt::new("test_token", encoding_key, "test_corpid");
    assert!(result.is_ok());
    
    // Test full encrypt/decrypt cycle
    let crypt = result.unwrap();
    let msg = "Hello World";
    let timestamp = "1234567890";
    let nonce = "123456";
    
    let encrypted = crypt.encrypt_msg(msg, timestamp, nonce).unwrap();
    println!("Encrypted: {}", encrypted);
    
    let signature = WXBizMsgCrypt::get_xml_field(&encrypted, "MsgSignature").unwrap();
    let decrypted = crypt.decrypt_msg(&signature, timestamp, nonce, &encrypted).unwrap();
    println!("Decrypted: {}", decrypted);
    
    assert_eq!(msg, decrypted);
}