mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-08 04:29:54 +00:00
[xiami] feat: implement xiami decipher
This commit is contained in:
7
um_crypto/xiami/Cargo.toml
Normal file
7
um_crypto/xiami/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "umc_xiami"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0.63"
|
||||
45
um_crypto/xiami/src/lib.rs
Normal file
45
um_crypto/xiami/src/lib.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum XiamiError {
|
||||
#[error("header too small, require at least {0} bytes")]
|
||||
HeaderTooSmall(usize),
|
||||
|
||||
#[error("not a xiami file")]
|
||||
NotXiamiFile,
|
||||
}
|
||||
|
||||
pub struct XiamiFile {
|
||||
pub copy_len: usize,
|
||||
pub format: [u8; 4],
|
||||
key: u8,
|
||||
}
|
||||
|
||||
impl XiamiFile {
|
||||
pub fn from_header(buffer: &[u8]) -> Result<Self, XiamiError> {
|
||||
if buffer.len() < 0x10 {
|
||||
Err(XiamiError::HeaderTooSmall(0x10))?;
|
||||
}
|
||||
|
||||
let (format, copy_len, key) = match buffer[..0x10] {
|
||||
[b'i', b'f', b'm', b't', f1, f2, f3, f4, 0xfe, 0xfe, 0xfe, 0xfe, a, b, c, key] => {
|
||||
let copy_len = (a as usize) | ((b as usize) << 8) | ((c as usize) << 16);
|
||||
let format = [f1, f2, f3, f4];
|
||||
(format, copy_len, key.wrapping_sub(1))
|
||||
}
|
||||
_ => Err(XiamiError::NotXiamiFile)?,
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
copy_len,
|
||||
format,
|
||||
key,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn decrypt(&self, buffer: &mut [u8]) {
|
||||
for b in buffer.iter_mut() {
|
||||
*b = self.key.wrapping_sub(*b);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user