wx_rust_common/util/crypto/byte_group.rs
1//! 字节组工具。
2//!
3//! 对应 Java `me.chanjar.weixin.common.util.crypto.ByteGroup`。
4
5/// 字节组,用于按序拼接多个字节数组(微信消息加解密中间步骤)。
6#[derive(Debug, Clone, Default)]
7pub struct ByteGroup {
8 bytes: Vec<u8>,
9}
10
11impl ByteGroup {
12 /// 构建空字节组。
13 pub fn new() -> Self {
14 Self::default()
15 }
16
17 /// 追加字节数组。
18 ///
19 /// # 参数
20 /// - `bytes`:要追加的字节数组
21 pub fn add_bytes(&mut self, bytes: &[u8]) {
22 self.bytes.extend_from_slice(bytes);
23 }
24
25 /// 返回拼接后的总长度。
26 pub fn size(&self) -> usize {
27 self.bytes.len()
28 }
29
30 /// 返回拼接后的字节数组。
31 pub fn to_bytes(&self) -> Vec<u8> {
32 self.bytes.clone()
33 }
34}