refactor: unify qmc2 decrypt api

This commit is contained in:
鲁树人
2024-09-06 00:52:13 +01:00
parent c4249226a2
commit a9c7ba9fd4
4 changed files with 55 additions and 13 deletions

View File

@@ -1,5 +1,9 @@
use crate::v2_map::QMC2Map;
use crate::v2_rc4::cipher::QMC2RC4;
use anyhow::Result;
use thiserror::Error;
pub mod ekey;
pub mod footer;
pub mod v1;
pub mod v2_map;
@@ -11,6 +15,36 @@ pub enum QmcCryptoError {
QMCV2MapKeyEmpty,
}
pub enum QMCv2Cipher {
MapL(QMC2Map),
RC4(QMC2RC4),
}
impl QMCv2Cipher {
pub fn new<T>(key: T) -> Result<Self>
where
T: AsRef<[u8]>,
{
let key = key.as_ref();
let cipher = match key.len() {
0 => Err(QmcCryptoError::QMCV2MapKeyEmpty)?,
..=300 => QMCv2Cipher::MapL(QMC2Map::new(key)?),
_ => QMCv2Cipher::RC4(QMC2RC4::new(key)),
};
Ok(cipher)
}
pub fn decrypt<T>(&self, data: &mut T, offset: usize)
where
T: AsMut<[u8]> + ?Sized,
{
match self {
QMCv2Cipher::MapL(cipher) => cipher.decrypt(data, offset),
QMCv2Cipher::RC4(cipher) => cipher.decrypt(data, offset),
}
}
}
#[cfg(test)]
mod test {
pub fn generate_key(len: usize) -> Vec<u8> {