2024-09-15 21:15:02 +00:00
|
|
|
use crate::header::Header;
|
|
|
|
use crate::{Decipher, KugouError};
|
|
|
|
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decipher for DecipherV2 {
|
|
|
|
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
|
2024-09-15 22:25:19 +00:00
|
|
|
for (datum, offset) in buffer.iter_mut().zip(offset..) {
|
|
|
|
*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(())
|
|
|
|
}
|