Difference between revisions of "User:Pedro Oval/XorBase64StringsSameLength"
Pedro Oval (talk | contribs) (Created workaround to SCR-35) |
Pedro Oval (talk | contribs) m (<lsl> to <source>) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This | {{Note|This function has been obsoleted by the release of [[llXorBase64]] in [[Release Notes/Second Life Server/13#13.06.21.277682|server version 13.06.21.277682]].}} | ||
< | 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 [http://jira.secondlife.com/browse/SCR-35 SCR-35] to provide a substitutive while that bug isn't fixed. Note that it is not a full substitutive for <code>llXorBase64StringsCorrect</code>, 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). | ||
<source lang="lsl2"> | |||
string XorBase64StringsSameLength(string s1, string s2) | string XorBase64StringsSameLength(string s1, string s2) | ||
{ | { | ||
Line 46: | Line 48: | ||
} | } | ||
} | } | ||
</ | </source> |
Latest revision as of 20:43, 23 January 2015
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
}
}