From fe4612253738f7c88379101c46dbbe83e33f116f Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Thu, 2 May 2024 12:19:07 +0800 Subject: [PATCH] Refactor next_i32 method to handle edge case --- namerena-runner/src/rc4.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/namerena-runner/src/rc4.rs b/namerena-runner/src/rc4.rs index 7ca3c03..c21cabd 100644 --- a/namerena-runner/src/rc4.rs +++ b/namerena-runner/src/rc4.rs @@ -165,21 +165,26 @@ impl RC4 { } /// 生成 i32 随机数 - /// ```dart - /// int nextInt(int max) { - /// int round = max ; - /// int v = nextByte(); - /// do { - /// v = v <<8 | nextByte(); - /// if (v >= max) { - /// v %= max; - /// } - /// round >>= 6; - /// } while(round != 0); - /// return v; + /// ```javascript + /// ax(a) { + /// // nextInt + /// var n, round + /// if (a === 0) return 0 + /// n = this.n() + /// round = a + /// do { + /// n = (n << 8 | this.n()) >>> 0 + /// if (n >= a) n = C.JsInt.V(n, a) + /// round = C.JsInt.am(round, 8) + /// } while (round !== 0) + /// return n /// } + /// /// ``` pub fn next_i32(&mut self, max: i32) -> i32 { + if max == 0 { + return 0; + } let mut round = max; let mut v = self.next_u8() as i32; loop { @@ -187,7 +192,8 @@ impl RC4 { if v >= max { v %= max; } - round >>= 6; + round >>= 8; + // rc4 lib 里是 6, md5.js( 继承的 R ) 里是 8 if round == 0 { break; }