[qmc2/rc4] refactor: create box of slice instead of convert later

This commit is contained in:
鲁树人 2024-09-15 15:40:03 +01:00
parent 6791dec745
commit f75a3ccb34
2 changed files with 8 additions and 5 deletions

View File

@ -17,13 +17,13 @@ pub struct QMC2RC4 {
impl QMC2RC4 {
pub fn new(key: &[u8]) -> Self {
let mut rc4 = RC4::new(key);
let mut key_stream = [0u8; RC4_STREAM_CACHE_SIZE];
rc4.derive(&mut key_stream);
let mut key_stream = Box::new([0u8; RC4_STREAM_CACHE_SIZE]);
rc4.derive(&mut key_stream[..]);
Self {
hash: hash(key),
key: key.into(),
key_stream: key_stream.into(),
key_stream,
}
}

View File

@ -42,8 +42,11 @@ impl RC4 {
self.state[index]
}
pub fn derive(&mut self, buffer: &mut [u8]) {
for item in buffer.iter_mut() {
pub fn derive<T>(&mut self, buffer: &mut T)
where
T: AsMut<[u8]> + ?Sized,
{
for item in buffer.as_mut().iter_mut() {
*item ^= self.generate();
}
}