[qmc2/rc4] fix: segment key calculation.

This commit is contained in:
鲁树人 2024-09-15 13:51:03 +01:00
parent 15547f237b
commit 6791dec745
3 changed files with 15 additions and 5 deletions

View File

@ -29,7 +29,7 @@ impl QMCv2Cipher {
let key = key.as_ref();
let cipher = match key.len() {
0 => Err(QmcCryptoError::QMCV2MapKeyEmpty)?,
..=300 => QMCv2Cipher::MapL(QMC2Map::new(key)?),
1..=300 => QMCv2Cipher::MapL(QMC2Map::new(key)?),
_ => QMCv2Cipher::RC4(QMC2RC4::new(key)),
};
Ok(cipher)

View File

@ -11,7 +11,7 @@ const RC4_STREAM_CACHE_SIZE: usize = OTHER_SEGMENT_SIZE + 512;
pub struct QMC2RC4 {
hash: f64,
key: Box<[u8]>,
key_stream: [u8; RC4_STREAM_CACHE_SIZE],
key_stream: Box<[u8; RC4_STREAM_CACHE_SIZE]>,
}
impl QMC2RC4 {
@ -23,7 +23,7 @@ impl QMC2RC4 {
Self {
hash: hash(key),
key: key.into(),
key_stream,
key_stream: key_stream.into(),
}
}

View File

@ -1,9 +1,9 @@
pub fn get_segment_key(id: u64, seed: u8, hash: f64) -> u32 {
pub fn get_segment_key(id: u64, seed: u8, hash: f64) -> u64 {
match seed {
0 => 0,
seed => {
let result = hash / ((id + 1).wrapping_mul(seed.into()) as f64) * 100.0;
result as u32
result as u64
}
}
}
@ -21,4 +21,14 @@ mod tests {
fn test_segment_key_123() {
assert_eq!(get_segment_key(1, 123, 12345.0), 5018);
}
#[test]
fn test_segment_key_large_1() {
assert_eq!(get_segment_key(51, 35, 516402887.0), 28373784);
}
#[test]
fn test_segment_key_large_2() {
assert_eq!(get_segment_key(0, 66, 3908240000.0), 5921575757);
}
}