2024-09-15 21:15:02 +00:00
|
|
|
use crate::header::Header;
|
2024-09-16 19:59:05 +00:00
|
|
|
use crate::KugouError;
|
2024-09-15 21:15:02 +00:00
|
|
|
|
|
|
|
pub struct DecipherV2 {
|
|
|
|
key: [u8; 4],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecipherV2 {
|
|
|
|
pub fn new(_header: &Header, slot_key: &[u8]) -> Result<Self, KugouError> {
|
|
|
|
let mut key = [0u8; 4];
|
|
|
|
key.copy_from_slice(slot_key);
|
|
|
|
Ok(Self { key })
|
|
|
|
}
|
|
|
|
|
2024-09-16 19:59:05 +00:00
|
|
|
pub fn decrypt<T: AsMut<[u8]> + ?Sized>(&self, buffer: &mut T, offset: usize) {
|
|
|
|
for (datum, offset) in buffer.as_mut().iter_mut().zip(offset..) {
|
2024-09-15 22:25:19 +00:00
|
|
|
*datum ^= self.key[offset % self.key.len()];
|
2024-09-15 21:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_v2_init() -> Result<(), KugouError> {
|
|
|
|
let hdr_v2 = Header::from_buffer(include_bytes!("__fixtures__/kgm_v2_hdr.bin"))?;
|
|
|
|
DecipherV2::new(&hdr_v2, b"1234")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|