moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

Unexpected result on string XOR

Open ferib opened this issue 3 years ago • 2 comments

Hello, I was playing around with MoonSharp and noticed the result from my snipper below differs from the results in my Lua 5.2 IDE.

Please have a look at the snippet below, the function v7 will take string arg1 and string arg2, turn them into a byte, and XOR them with each other. The result of the XOR is saved in a table that later on will be concatenated. The expected result for this snippet is to return a string with the value test1234567890, however, when executed in MoonSharp I get test123ô567890

local v0 = string.char;
local v1 = string.byte;
local v2 = string.sub;
local v3 = bit32 or bit;
local v4 = v3.bxor;
local v5 = table.concat;
local v6 = table.insert;
local function v7(v8, v9)
	local v10 = {};
	for v11 = 1, #v8 do
		v6(v10, v0(v4(v1(v2(v8, v11, v11 + 1)), v1(v2(v9, 1 + ((v11 - 1) % #v9), 1 + ((v11 - 1) % #v9) + 1))) % 256));
	end
	return v5(v10);
end
return v7("\211\3\183\226\254\19\213\255\146\80\243\174\246\17", "\167\102\196\150\207\33\230\203");

ferib avatar Jan 07 '23 16:01 ferib

Fixed in my fork by replacing Unicode2Ascii(i) with a 0–255 bitwise mask (i & 0xFF) in char

Unicode2Ascii messing with the bytes seems to be the issue, & 0xFF preserves raw bytes in logic like your XOR snippet I tested in Unity and Lua 5.2, it now returns test1234567890

yogurtgaming2 avatar Jun 22 '25 18:06 yogurtgaming2

Oh neat!

ferib avatar Jun 22 '25 21:06 ferib