lib_um_crypto_rust/um_crypto/kgm/src/v2.rs

32 lines
781 B
Rust
Raw Normal View History

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) {
let key_stream = self.key.iter().cycle().skip(offset % self.key.len());
for (datum, &k) in buffer.iter_mut().zip(key_stream) {
*datum ^= k;
}
}
}
#[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(())
}