PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.47k stars 261 forks source link

it's has problem with encoding in python3 #238

Open luopeixiong opened 3 years ago

luopeixiong commented 3 years ago

it's my code

`

import js2py

js = """// n1 = function () { var e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var n = { rotl: function (t, e) { return t << e | t >>> 32 - e }, rotr: function (t, e) { return t << 32 - e | t >>> e }, endian: function (t) { if (t.constructor == Number) return 16711935 & n.rotl(t, 8) | 4278255360 & n.rotl(t, 24); for (var e = 0; e < t.length; e++) t[e] = n.endian(t[e]); return t }, randomBytes: function (t) { for (var e = []; t > 0; t--) e.push(Math.floor(256 Math.random())); return e }, bytesToWords: function (t) { for (var e = [], n = 0, r = 0; n < t.length; n++, r += 8) e[r >>> 5] |= t[n] << 24 - r % 32; return e }, wordsToBytes: function (t) { for (var e = [], n = 0; n < 32 t.length; n += 8) e.push(t[n >>> 5] >>> 24 - n % 32 & 255); return e }, bytesToHex: function (t) { for (var e = [], n = 0; n < t.length; n++) e.push((t[n] >>> 4).toString(16)), e.push((15 & t[n]).toString(16)); return e.join("") }, hexToBytes: function (t) { for (var e = [], n = 0; n < t.length; n += 2) e.push(parseInt(t.substr(n, 2), 16)); return e }, bytesToBase64: function (t) { for (var n = [], r = 0; r < t.length; r += 3) for (var o = t[r] << 16 | t[r + 1] << 8 | t[r + 2], i = 0; i < 4; i++) 8 r + 6 i <= 8 t.length ? n.push(e.charAt(o >>> 6 (3 - i) & 63)) : n.push("="); return n.join("") }, base64ToBytes: function (t) { t = t.replace(/[^A-Z0-9+\/]/gi, "");

        for (var n = [], r = 0, o = 0; r < t.length; o = ++r % 4)
            0 != o && n.push((e.indexOf(t.charAt(r - 1)) & Math.pow(2, -2 * o + 8) - 1) << 2 * o | e.indexOf(t.charAt(r)) >>> 6 - 2 * o);
        return n
    }
};
return n

}()

n2 = function () { function n(t) { return !!t.constructor && "function" === typeof t.constructor.isBuffer && t.constructor.isBuffer(t) }

function r(t) {
    return "function" === typeof t.readFloatLE && "function" === typeof t.slice && n(t.slice(0, 0))
}

return function (t) {
    return null != t && (n(t) || r(t) || !!t._isBuffer)
}

}()

n3 = function () { var n = { utf8: { stringToBytes: function (t) { return n.bin.stringToBytes(unescape(encodeURIComponent(t))) }, bytesToString: function (t) { return decodeURIComponent(escape(n.bin.bytesToString(t))) } }, bin: { stringToBytes: function (t) { for (var e = [], n = 0; n < t.length; n++) e.push(255 & t.charCodeAt(n)); return e }, bytesToString: function (t) { for (var e = [], n = 0; n < t.length; n++) e.push(String.fromCharCode(t[n])); return e.join("") } } }; return n }()

var e = n1 , r = n3.utf8 , o = n2 , i = n3.bin , a = function (t, n) { t = r.stringToBytes(t) t = Array.prototype.slice.call(t, 0)

    // t = t.toString()
    // t = Array.prototype.slice.call(t, 0)
t.constructor == String ? t = n && "binary" === n.encoding ? i.stringToBytes(t) : r.stringToBytes(t) : o(t) ? t = Array.prototype.slice.call(t, 0) : Array.isArray(t) || (t = t.toString());
for (var s = e.bytesToWords(t), c = 8 * t.length, u = 1732584193, f = -271733879, l = -1732584194, d = 271733878, p = 0; p < s.length; p++)
    s[p] = 16711935 & (s[p] << 8 | s[p] >>> 24) | 4278255360 & (s[p] << 24 | s[p] >>> 8);
s[c >>> 5] |= 128 << c % 32,
    s[14 + (c + 64 >>> 9 << 4)] = c;
var h = a._ff
    , v = a._gg
    , y = a._hh
    , m = a._ii;
for (p = 0; p < s.length; p += 16) {
    var g = u
        , b = f
        , _ = l
        , w = d;
    u = h(u, f, l, d, s[p + 0], 7, -680876936),
        d = h(d, u, f, l, s[p + 1], 12, -389564586),
        l = h(l, d, u, f, s[p + 2], 17, 606105819),
        f = h(f, l, d, u, s[p + 3], 22, -1044525330),
        u = h(u, f, l, d, s[p + 4], 7, -176418897),
        d = h(d, u, f, l, s[p + 5], 12, 1200080426),
        l = h(l, d, u, f, s[p + 6], 17, -1473231341),
        f = h(f, l, d, u, s[p + 7], 22, -45705983),
        u = h(u, f, l, d, s[p + 8], 7, 1770035416),
        d = h(d, u, f, l, s[p + 9], 12, -1958414417),
        l = h(l, d, u, f, s[p + 10], 17, -42063),
        f = h(f, l, d, u, s[p + 11], 22, -1990404162),
        u = h(u, f, l, d, s[p + 12], 7, 1804603682),
        d = h(d, u, f, l, s[p + 13], 12, -40341101),
        l = h(l, d, u, f, s[p + 14], 17, -1502002290),
        f = h(f, l, d, u, s[p + 15], 22, 1236535329),
        u = v(u, f, l, d, s[p + 1], 5, -165796510),
        d = v(d, u, f, l, s[p + 6], 9, -1069501632),
        l = v(l, d, u, f, s[p + 11], 14, 643717713),
        f = v(f, l, d, u, s[p + 0], 20, -373897302),
        u = v(u, f, l, d, s[p + 5], 5, -701558691),
        d = v(d, u, f, l, s[p + 10], 9, 38016083),
        l = v(l, d, u, f, s[p + 15], 14, -660478335),
        f = v(f, l, d, u, s[p + 4], 20, -405537848),
        u = v(u, f, l, d, s[p + 9], 5, 568446438),
        d = v(d, u, f, l, s[p + 14], 9, -1019803690),
        l = v(l, d, u, f, s[p + 3], 14, -187363961),
        f = v(f, l, d, u, s[p + 8], 20, 1163531501),
        u = v(u, f, l, d, s[p + 13], 5, -1444681467),
        d = v(d, u, f, l, s[p + 2], 9, -51403784),
        l = v(l, d, u, f, s[p + 7], 14, 1735328473),
        f = v(f, l, d, u, s[p + 12], 20, -1926607734),
        u = y(u, f, l, d, s[p + 5], 4, -378558),
        d = y(d, u, f, l, s[p + 8], 11, -2022574463),
        l = y(l, d, u, f, s[p + 11], 16, 1839030562),
        f = y(f, l, d, u, s[p + 14], 23, -35309556),
        u = y(u, f, l, d, s[p + 1], 4, -1530992060),
        d = y(d, u, f, l, s[p + 4], 11, 1272893353),
        l = y(l, d, u, f, s[p + 7], 16, -155497632),
        f = y(f, l, d, u, s[p + 10], 23, -1094730640),
        u = y(u, f, l, d, s[p + 13], 4, 681279174),
        d = y(d, u, f, l, s[p + 0], 11, -358537222),
        l = y(l, d, u, f, s[p + 3], 16, -722521979),
        f = y(f, l, d, u, s[p + 6], 23, 76029189),
        u = y(u, f, l, d, s[p + 9], 4, -640364487),
        d = y(d, u, f, l, s[p + 12], 11, -421815835),
        l = y(l, d, u, f, s[p + 15], 16, 530742520),
        f = y(f, l, d, u, s[p + 2], 23, -995338651),
        u = m(u, f, l, d, s[p + 0], 6, -198630844),
        d = m(d, u, f, l, s[p + 7], 10, 1126891415),
        l = m(l, d, u, f, s[p + 14], 15, -1416354905),
        f = m(f, l, d, u, s[p + 5], 21, -57434055),
        u = m(u, f, l, d, s[p + 12], 6, 1700485571),
        d = m(d, u, f, l, s[p + 3], 10, -1894986606),
        l = m(l, d, u, f, s[p + 10], 15, -1051523),
        f = m(f, l, d, u, s[p + 1], 21, -2054922799),
        u = m(u, f, l, d, s[p + 8], 6, 1873313359),
        d = m(d, u, f, l, s[p + 15], 10, -30611744),
        l = m(l, d, u, f, s[p + 6], 15, -1560198380),
        f = m(f, l, d, u, s[p + 13], 21, 1309151649),
        u = m(u, f, l, d, s[p + 4], 6, -145523070),
        d = m(d, u, f, l, s[p + 11], 10, -1120210379),
        l = m(l, d, u, f, s[p + 2], 15, 718787259),
        f = m(f, l, d, u, s[p + 9], 21, -343485551),
        u = u + g >>> 0,
        f = f + b >>> 0,
        l = l + _ >>> 0,
        d = d + w >>> 0
}
return e.endian([u, f, l, d])

}

a._ff = function (t, e, n, r, o, i, a) { var s = t + (e & n | ~e & r) + (o >>> 0) + a; return (s << i | s >>> 32 - i) + e } , a._gg = function (t, e, n, r, o, i, a) { var s = t + (e & r | n & ~r) + (o >>> 0) + a; return (s << i | s >>> 32 - i) + e } , a._hh = function (t, e, n, r, o, i, a) { var s = t + (e ^ n ^ r) + (o >>> 0) + a; return (s << i | s >>> 32 - i) + e } , a._ii = function (t, e, n, r, o, i, a) { var s = t + (n ^ (e | ~r)) + (o >>> 0) + a; return (s << i | s >>> 32 - i) + e } , a._blocksize = 64, a._digestsize = 64, re = function (t, n) { var r = e.wordsToBytes(a(t, n)); return n && n.asBytes ? r : n && n.asString ? i.bytesToString(r) : e.bytesToHex(r) }

var res = function (data) { console.log(data['searchFCategoryRequestParam']) var t = "yitshenghuoguan.xyz!" return re(Object.keys(data).map(function (t) { return "".concat(t, "=").concat(data[t]) }).sort().join("") + t) }

data = { 'searchFCategoryRequestParam': '{"searchId":8,"searchName":"布艺软装","l1FCategoryId":7,"l1FCategoryName":"居家生活","hasActivity":"","minPrice":0,"maxPrice":0,"l3CategoryIds":[],"textAttributeFilters":[],"generalTagFilters":[],"sortType":"FIRST_ON_SALE","sortAsc":false,"provinceId":"","cityId":"","page":{"offset":0,"limit":36}}', '_aid': '30', '_did': '361439647906561', '_mt': 'node_pcOfficialWebsite.getShopCategoryProduceList', '_sm': 'md5', '_ts': '1614413389170', }

console.log(res(data)) """

s = '{"searchId":8,"searchName":"布艺软装","l1FCategoryId":7,"l1FCategoryName":"居家生活","hasActivity":"","minPrice":0,"maxPrice":0,"l3CategoryIds":[],"textAttributeFilters":[],"generalTagFilters":[],"sortType":"FIRST_ON_SALE","sortAsc":false,"provinceId":"","cityId":"","page":{"offset":0,"limit":36}}'

s = s.encode("utf-8").decode("unicode-escape", "replace")

print(s)

data = { 'searchFCategoryRequestParam': s, '_aid': '30', '_did': '361439647906561', '_mt': 'node_pcOfficialWebsite.getShopCategoryProduceList', '_sm': 'md5', '_ts': '1614413389170', }

def get_sign(): ctx = js2py.EvalJs()

it's wrong
ctx.eval(js)
##### is's right
ctx.eval(js.encode("utf-8").decode("unicode-escape", "replace"))

if name == 'main': get_sign()`

how do it without change encoding