[kgm] refactor #2: slightly improve performance

This commit is contained in:
鲁树人 2024-09-15 22:20:18 +01:00
parent 7b4464bacd
commit fd73e8b9a3

View File

@ -29,17 +29,27 @@ impl DecipherV3 {
impl Decipher for DecipherV3 {
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
let slot_key_stream = self.slot_key.iter().cycle().skip(offset);
let file_key_stream = self.file_key.iter().cycle().skip(offset);
let slot_key_stream = self
.slot_key
.iter()
.cycle()
.skip(offset % self.slot_key.len());
let file_key_stream = self
.file_key
.iter()
.cycle()
.skip(offset % self.file_key.len());
let mut offset = offset as u32;
let key_stream = slot_key_stream.zip(file_key_stream);
for (datum, (&slot_key, &file_key)) in buffer.iter_mut().zip(key_stream) {
let offset_key = offset.to_ne_bytes().iter().fold(0, |acc, &x| acc ^ x);
let mut temp = *datum;
temp ^= file_key;
temp ^= temp.wrapping_shl(4);
temp ^= slot_key;
temp ^= offset.to_ne_bytes().iter().fold(0, |acc, &x| acc ^ x);
temp ^= offset_key;
*datum = temp;
offset = offset.wrapping_add(1);