[kgm] refactor: convert to enum for dispatch

This commit is contained in:
鲁树人
2024-09-16 20:59:05 +01:00
parent e011f75d36
commit e45d09cf8e
5 changed files with 48 additions and 36 deletions

View File

@@ -1,6 +1,4 @@
use crate::v2::DecipherV2;
use crate::v3::DecipherV3;
use crate::{Decipher, KugouError};
use crate::KugouError;
use byteorder::{ByteOrder, LE};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -48,25 +46,8 @@ impl Header {
})
}
pub fn make_decipher(&self) -> Result<Box<dyn Decipher>, KugouError> {
let slot_key: &[u8] = match self.key_slot {
1 => b"l,/'",
slot => Err(KugouError::UnsupportedKeySlot(slot))?,
};
let decipher: Box<dyn Decipher> = match self.crypto_version {
2 => Box::new(DecipherV2::new(self, slot_key)?),
3 => Box::new(DecipherV3::new(self, slot_key)?),
version => Err(KugouError::UnsupportedCipherVersion(version))?,
};
let mut test_data = self.decrypt_test_data;
decipher.decrypt(&mut test_data, 0);
if self.challenge_data != test_data {
Err(KugouError::SelfTestFailed)?;
}
Ok(decipher)
pub fn get_challenge_data(&self) -> [u8; 0x10] {
self.challenge_data
}
}