User:Pedro Oval/XorBase64StringsSameLength
This code works around a bug in llXorBase64StringsCorrect in order to perform correct XOR of two strings of the same length but without the 0.3 second delay included in llXorBase64Strings. It relies on llBase64ToInteger's unpredictable behaviour not affecting the bits actually used, for speed. It is a workaround to SCR-35 to provide a substitutive while that bug isn't fixed. Note that it is not a full substitutive for llXorBase64StringsCorrect
, because it doesn't deal with the case of the second string being shorter than the first. Its main purpose is stream encryption, where the lengths usually match or at least the second string is longer (that case will be correctly handled by this function, trimming the result to the length of the first string).
string XorBase64StringsSameLength(string s1, string s2)
{
integer i;
string s3;
integer len = llStringLength(s1); // assumed to be the same in s1 and s2
integer pad = (llGetSubString(s1, -2, -2) == "=") + (llGetSubString(s1, -1, -1) == "=");
for (i = 0; i < len; i += 4)
{
s3 += llGetSubString(llIntegerToBase64(llBase64ToInteger(llGetSubString(s1, i, i+3))
^ llBase64ToInteger(llGetSubString(s2, i, i+3))),
0, 3);
}
if (pad)
{
s3 = llGetSubString(s3, 0, -pad-1) + llGetSubString("==", -pad, -1);
}
return s3;
}
/* Test suite */
default
{
state_entry()
{
llOwnerSay((string)llStringLength(XorBase64StringsSameLength("", "whatever"))); // Returns 0 as it should
llOwnerSay(XorBase64StringsSameLength("AABA", "1234")); // Returns 1224 as it should
llOwnerSay(XorBase64StringsSameLength("1234", "AABA")); // Returns 1224 as it should
llOwnerSay(XorBase64StringsSameLength("BAAA", "1234")); // Returns 0234 as it should
llOwnerSay(XorBase64StringsSameLength("1234", "BAAA")); // Returns 0234 as it should
llOwnerSay(XorBase64StringsSameLength("AABA", "AABA")); // Returns AAAA as it should
llOwnerSay(XorBase64StringsSameLength("1234///=", "1234AAA=")); // Returns AAAA//8= as it should
llOwnerSay(XorBase64StringsSameLength("1234///=", "1234AAA/")); // Returns AAAA//8= as it should
llOwnerSay(llXorBase64StringsCorrect ("1234///=", "1234AAA=")); // Returns AAAAKJI= (wrong)
llOwnerSay(llXorBase64StringsCorrect ("1234AAA=", "1234///=")); // Returns AAAA//8= as it should
llOwnerSay(XorBase64StringsSameLength("1234//==", "1234AA==")); // Returns AAAA/w== as it should
llOwnerSay(XorBase64StringsSameLength("1234//==", "1234AA//")); // Returns AAAA/w== as it should
llOwnerSay(llXorBase64StringsCorrect ("1234//==", "1234AA==")); // Returns AAAAKA== (wrong)
llOwnerSay(llXorBase64StringsCorrect ("1234AA==", "1234//==")); // Returns AAAA/w== as it should
}
}