源文件超过50行,已替换为不超过50行的伪代码。辅助函数为逻辑抽象,不直接执行。
1async function randomBytes({ serverSeed, clientSeed, nonce }, count) {2 const bytes = [];3 let round = 0;45 while (bytes.length < count) {6 const message = `${clientSeed}:${nonce}:${round++}`;7 const digest = await hmacSha256(8 utf8(serverSeed), // 原始文本编码,不能当作十六进制解码。9 utf8(message)10 );11 bytes.push(...digest);12 }13 return bytes.slice(0, count);14}1516function bytesToFloat([b0, b1, b2, b3]) {17 return b0 / 256 + b1 / 256 ** 218 + b2 / 256 ** 3 + b3 / 256 ** 4;19}2021function diceResult(float) {22 return Math.floor(float * 10001) / 100;23}2425function drawWithoutReplacement(pool, floats) {26 const remaining = [...pool];27 return floats.map(value => {28 const index = Math.floor(value * remaining.length);29 return remaining.splice(index, 1)[0];30 });31}已确认说明
- 没有捕获路径中的 Worker
- HMAC 原文编码
- 默认 4 字节浮点
- HMAC 分轮
- bytes 到 float 再到整数
- 洗牌是不放回抽样
- Limbo / Crash / Slide
- 扑克牌存在两种算法