From f75a3ccb34de9abf2d01f8f3e2e1567f5ad0c508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sun, 15 Sep 2024 15:40:03 +0100 Subject: [PATCH] [qmc2/rc4] refactor: create box of slice instead of convert later --- um_crypto/qmc/src/v2_rc4/cipher.rs | 6 +++--- um_crypto/qmc/src/v2_rc4/rc4.rs | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/um_crypto/qmc/src/v2_rc4/cipher.rs b/um_crypto/qmc/src/v2_rc4/cipher.rs index 40e9d01..ad63fba 100644 --- a/um_crypto/qmc/src/v2_rc4/cipher.rs +++ b/um_crypto/qmc/src/v2_rc4/cipher.rs @@ -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, } } diff --git a/um_crypto/qmc/src/v2_rc4/rc4.rs b/um_crypto/qmc/src/v2_rc4/rc4.rs index c7724e1..b273b52 100644 --- a/um_crypto/qmc/src/v2_rc4/rc4.rs +++ b/um_crypto/qmc/src/v2_rc4/rc4.rs @@ -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(&mut self, buffer: &mut T) + where + T: AsMut<[u8]> + ?Sized, + { + for item in buffer.as_mut().iter_mut() { *item ^= self.generate(); } }