wx_rust_common/util/crypto/
pkcs7_encoder.rs1pub struct Pkcs7Encoder;
7
8impl Pkcs7Encoder {
9 const BLOCK_SIZE: usize = 32;
11
12 pub fn encode(count: usize) -> Vec<u8> {
20 let amount_to_pad = Self::BLOCK_SIZE - (count % Self::BLOCK_SIZE);
22 let pad_chr = (amount_to_pad as u8) as char;
24 let mut out = Vec::with_capacity(amount_to_pad);
25 for _ in 0..amount_to_pad {
26 out.push(pad_chr as u8);
27 }
28 out
29 }
30
31 pub fn decode(decrypted: &[u8]) -> Vec<u8> {
39 if decrypted.is_empty() {
40 return decrypted.to_vec();
41 }
42 let pad = decrypted[decrypted.len() - 1] as usize;
43 let pad = if (1..=32).contains(&pad) { pad } else { 0 };
45 let len = decrypted.len().saturating_sub(pad);
46 decrypted[..len].to_vec()
47 }
48}