< 返回版块

Borber 发表于 2023-01-09 00:06

Tags:JavaScript

目前有一个js函数: 在计算到 v0 += ((v1 << 4) + k2[0]) ^ (v1 + sum) ^ ((v1 >>> 5) + k2[1]); v1 += ((v0 << 4) + k2[2]) ^ (v0 + sum) ^ ((v0 >>> 5) + k2[3]); 这里时死活无法调试出来,

目前有两个疑问, 一者是 js 中 的 v0 v1 会超过 i32 的范围, 但也不是像是 i64 , 另一个就是如果rust无法很好的翻译这段JavaScript代码, 有什么比较好的方法嵌入运行JavaScript代码吗?

以及js代码可以在 虎牙的直播间内 打开控制台 然后通过 底部教程中的方法进行调用.

let ccc = function (xx0, xx1, xx2) {
    var cb = xx0 + xx1 + xx2 + "220120200217";
    var rb = CryptoJS.MD5(cb).toString();
    console.log("rb", rb);
    var re = [];
    for (var i = 0; i < rb.length / 8; i++) {
        console.log(
            parseInt(rb.substr(i * 8, 2), 16) & 0xff,
            (parseInt(rb.substr(i * 8 + 2, 2), 16) << 8) & 0xff00,
            (parseInt(rb.substr(i * 8 + 4, 2), 16) << 24) >>> 8,
            parseInt(rb.substr(i * 8 + 6, 2), 16) << 24
        );
        re[i] =
            (parseInt(rb.substr(i * 8, 2), 16) & 0xff) |
            ((parseInt(rb.substr(i * 8 + 2, 2), 16) << 8) & 0xff00) |
            ((parseInt(rb.substr(i * 8 + 4, 2), 16) << 24) >>> 8) |
            (parseInt(rb.substr(i * 8 + 6, 2), 16) << 24);
    }

    console.log(re);
    var k2 = [0x7c2717b7, 0x1f83b724, 0x471c00, 0x64957544];
    for (var I = 0; I < 2; I++) {
        var v0 = re[I * 2],
            v1 = re[I * 2 + 1],
            sum = 0,
            i = 0;
        var delta = 0x9e3779b9;
        for (i = 0; i < 32; i++) {
            sum += delta;
            // v0 += ((v1 << 4) + k2[0]) ^ (v1 + sum) ^ ((v1 >>> 5) + k2[1]);
            v0 += ((v1 << 4) + k2[0]);
            // v1 += ((v0 << 4) + k2[2]) ^ (v0 + sum) ^ ((v0 >>> 5) + k2[3]);


            v1 += ((v0 << 4) + k2[2]);

            // 此处的 v0 v1 

            console.log(v0, v1);



        }
        re[I * 2] = v0;
        re[I * 2 + 1] = v1;
    }
    console.log(re);
    re[0] = (re[0] >>> k2[0] % 16) | (re[0] << (32 - (k2[0] % 16)));
    re[0] += k2[2];
    re[0] -= k2[2];
    re[1] = (re[1] << k2[1] % 16) | (re[1] >>> (32 - (k2[1] % 16)));
    re[1] -= k2[3];
    re[1] = (re[1] << k2[3] % 16) | (re[1] >>> (32 - (k2[3] % 16)));
    re[2] = (re[2] << k2[0] % 16) | (re[2] >>> (32 - (k2[0] % 16)));
    re[2] = (re[2] >>> k2[2] % 16) | (re[2] << (32 - (k2[2] % 16)));
    re[2] = (re[2] >>> k2[0] % 16) | (re[2] << (32 - (k2[0] % 16)));
    re[2] = (re[2] << k2[2] % 16) | (re[2] >>> (32 - (k2[2] % 16)));
    re[2] ^= k2[2];
    re[3] = (re[3] >>> k2[1] % 16) | (re[3] << (32 - (k2[1] % 16)));
    re[3] -= k2[3];
    re[3] = (re[3] << k2[3] % 16) | (re[3] >>> (32 - (k2[3] % 16)));
    re[0] += k2[0];
    re[0] -= k2[2];
    re[0] = (re[0] >>> k2[2] % 16) | (re[0] << (32 - (k2[2] % 16)));
    re[1] += k2[1];
    re[1] = (re[1] >>> k2[3] % 16) | (re[1] << (32 - (k2[3] % 16)));
    re[1] += k2[3];
    re[1] -= k2[3];
    re[2] = (re[2] >>> k2[0] % 16) | (re[2] << (32 - (k2[0] % 16)));
    re[2] ^= k2[2];
    re[2] = (re[2] << k2[2] % 16) | (re[2] >>> (32 - (k2[2] % 16)));
    re[3] -= k2[1];
    re[3] -= k2[3];
    re[3] -= k2[3];
    re[3] ^= k2[3];
    {
        var hc = "0123456789abcdef".split("");
        for (var i = 0; i < re.length; i++) {
            var j = 0,
                s = "";
            for (; j < 4; j++)
                s +=
                    hc[(re[i] >> (j * 8 + 4)) & 15] +
                    hc[(re[i] >> (j * 8)) & 15];
            re[i] = s;
        }
        re = re.join("");
    }
    var rt = "v=220120200217" + "&did=" + xx1 + "&tt=" + xx2 + "&sign=" + re;
    return rt;
};

资料来源: https://zhuanlan.zhihu.com/p/107330805


Ext Link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3bde85e5bec484906f87a00cfa986b76

评论区

写评论
作者 Borber 2023-01-10 01:33

已经解决了, 具体的问题在于 , js 中 数字相加在rust中应该转化为 i64扩大范围 , 位操作转换为 i32 , 以及 >>> 无符号 应该转换为 u32, 位移均需要使用 wrapping 进行溢出处理

--
👇
songzhi: JS里的Number是f64,不是i32/i64。

songzhi 2023-01-09 12:27

JS里的Number是f64,不是i32/i64。

1 共 2 条评论, 1 页