我正在尝试将一些js代码转换为python,并在转换按位运算符时遇到麻烦。我已经在python中引入了ctypes.c_int
,但结果仍然不匹配。对于js中的>>>
,我按照建议的here使用。
我的(无效)代码的最小示例:
Javascript:
let R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
let K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
let blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0];
let h0 = 1732584193, h1 = 4023233417, h2 = 2562383102, h3 = 271733878, length = blocks.length;
for (let i = 0; length > i; i += 16) {
var f, g, tmp, x, y, a = h0, b = h1, c = h2, d = h3;
for (let j = 0; 64 > j; ++j) {
if (16 > j) {
f = d ^ b & (c ^ d);
g = j;
tmp = d;
d = c;
c = b;
x = a + f + K[j] + blocks[i + g];
y = R[j];
b += x << y | x >>> 32 - y;
a = tmp;
}
}
}
Python:
import ctypes
R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0]
h0 = 1732584193
h1 = 4023233417
h2 = 2562383102
h3 = 271733878
length = len(blocks)
i = 0
while length > i:
a = h0
b = h1
c = h2
d = h3
j = 0
while 64 > j:
if 16 > j:
f = ctypes.c_int(c ^ d & (b ^ c)).value
g = j
tmp = d
d = c
c = b
x = a + f + K[j] + blocks[i + g]
y = R[j]
b += ctypes.c_int(x << y | ((x & 0xffffffff) >> 32 - y)).value # was >>>
a = tmp
j += 1
i += 16
print('a', a)
print('b', b)
js返回7727443648,而python返回4225785507
答案 0 :(得分:0)
您的值超出了JS限制:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
数字-2147483648和2147483647是可通过32位带符号数字表示的最小和最大整数。