MD5: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
Preforms a MD5 Hash on the text. Similar to and SHA1 hash, although less secure. Two versions of the function are provided, one for UTF-8 Strings (all strings in LSL are UTF-8) and the other is for Base64 Strings (you need to tell it how many bits long the data is). | Preforms a MD5 Hash on the text. Similar to and SHA1 hash, although less secure. Two versions of the function are provided, one for UTF-8 Strings (all strings in LSL are UTF-8) and the other is for Base64 Strings (you need to tell it how many bits long the data is). | ||
There is also an [[SHA1]] & [SHA2] implementations ([[SHA2#SHA-224|224]] & [[SHA2#SHA-256|256]]). | There is also an [[SHA1]] & two [[SHA2]] implementations ([[SHA2#SHA-224|224]] & [[SHA2#SHA-256|256]]). | ||
View http://en.wikipedia.org/wiki/MD5 for more information. | View http://en.wikipedia.org/wiki/MD5 for more information. | ||
Revision as of 17:31, 25 November 2007
| LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Preforms a MD5 Hash on the text. Similar to and SHA1 hash, although less secure. Two versions of the function are provided, one for UTF-8 Strings (all strings in LSL are UTF-8) and the other is for Base64 Strings (you need to tell it how many bits long the data is).
There is also an SHA1 & two SHA2 implementations (224 & 256).
View http://en.wikipedia.org/wiki/MD5 for more information.
//////////////////////////////////////////////////////////////////////////////////////
//
// UTF-8 MD5
// Version 1.0 Alpha
// ESL Compiled: "Nov 25 2007", "19:23:16"
// Copyright (C) 2007 Strife Onizuka
// Based on Pseudo-code from http://en.wikipedia.org/wiki/MD5
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation;
// version 3 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library. If not, see <http://www.gnu.org/licenses/>
// or write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
// Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////////////
//===================================================//
// Combined Library //
// "Nov 25 2007", "19:23:16" //
// Copyright (C) 2004-2007, Strife Onizuka (cc-by) //
// http://creativecommons.org/licenses/by/3.0/ //
//===================================================//
//{
string TrimRight(string src, string chrs)//Mono Unsafe, LSO Safe
{
integer i = llStringLength(src);
do;while(~llSubStringIndex(chrs, llGetSubString(src, (i = ~-i), i)) && i);
return llDeleteSubString(src, (-~(i)), 0xFFFF);
}
string hexc="0123456789ABCDEF";
//} Combined Library
list k1 = [ -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426, -1473231341, -45705983,
1770035416, -1958414417, -42063, -1990404162, 1804603682, -40341101, -1502002290, 1236535329];
list k2 = [ -165796510, -1069501632, 643717713, -373897302, -701558691, 38016083, -660478335, -405537848,
568446438, -1019803690, -187363961, 1163531501, -1444681467, -51403784, 1735328473, -1926607734];
list k3 = [ -378558, -2022574463, 1839030562, -35309556, -1530992060, 1272893353, -155497632, -1094730640,
681279174, -358537222, -722521979, 76029189, -640364487, -421815835, 530742520, -995338651];
list k4 = [ -198630844, 1126891415, -1416354905, -57434055, 1700485571, -1894986606, -1051523, -2054922799,
1873313359, -30611744, -1560198380, 1309151649, -145523070, -1120210379, 718787259, -343485551];
list r1 = [7, 12, 17, 22];
list r2 = [5, 9, 14, 20];
list r3 = [4, 11, 16, 23];
list r4 = [6, 10, 15, 21];
string UTF8_MD5(string plain) {
integer H1 = 1732584193;
integer H2 = -271733879;
integer H3 = -1732584194;
integer H4 = 271733878;
//OR on the extra bit.
integer j = llStringLength(plain) << 3;
integer b = (~-(((j + 552) & -512) >> 5));
integer T = llBase64ToInteger(llGetSubString((TrimRight(llGetSubString(plain = llStringToBase64(plain), -4, -1),"=")) + "AAAA", 0, 5));
string buf = "AAA";
integer i = -5;
do buf += buf; while((i = -~i));
if(j)
{
i = 0x800000;
if(T & 0xFF00)
i = 0x00000080;
else if(T & 0xFF0000)
i = 0x00008000;
}
else
T = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
// llOwnerSay(llList2CSV([i,j]));
plain = llGetSubString( llDeleteSubString(plain, -4, -1) +
llGetSubString(llIntegerToBase64(T | i), 0, 5) +
buf, 0, ((b << 4) / 3) - 7) +
llGetSubString(llIntegerToBase64((((j & 0xFF) << 20) | ((j & 0xFF00) << 4) | ((j >> 12) & 0xFF0) | ((j >> 28) & 0xF)) >> ((b % 3) << 1)), 0, 5) +
"AAAAAAAA";
integer S = i = 0;
list x;
do
{
integer A = H1;
integer B = H2;
integer C = H3;
integer D = H4;
integer F = j = 0;
x = [];
do
{
T = llBase64ToInteger(llGetSubString((buf = llGetSubString(plain, T = ((i + j) << 4) / 3, T+6)) + "AAAAAA", 0, 5)) << (S = (((i + j) % 3) << 1));
if(S)
T = T | (llBase64ToInteger(llGetSubString("A" + (llDeleteSubString(buf, 0, 1)), 0, 5)) >> (6 - S));
x += (T = (((T & 0xFF) << 24) | ((T & 0xFF00) << 8) | ((T >> 8) & 0xFF00) | ((T >> 24) & 0xFF)));
F = A + llList2Integer(k1, j) + T + (D ^ (B & (C ^ D)));
S = B;
T = llList2Integer(r1, j & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(16 > (j = -~j));
do
{
F = A + llList2Integer(k2, j & 15) + (C ^ (D & (B ^ C))) + llList2Integer(x, (5 * j + 1) & 15);
S = B;
T = llList2Integer(r2, j & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(32 > (j = -~j));
do
{
F = A + llList2Integer(k3, j & 15) + (B ^ C ^ D) + llList2Integer(x, (3 * j + 5) & 15);
S = B;
T = llList2Integer(r3, j & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(48 > (j = -~j));
do
{
S = B;
T = llList2Integer(r4, j & 3);
F = A + llList2Integer(k4, j & 15) + (C ^ (B | (~D))) + llList2Integer(x, (7 * j) & 15);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(64 > (j = -~j));
llOwnerSay(llList2CSV(x));
H1 += A;
H2 += B;
H3 += C;
H4 += D;
}while(b > (i += 16));
x = [H4, H3, H2, H1];
i = -4;
buf = "";
do
{
T = llList2Integer(x,i);
j = 32;
do
buf = llGetSubString(hexc, b = ((T >> (j - 4)) & 0xF), b) + llGetSubString(hexc, b = ((T >> (j - 8)) & 0xF), b) + buf;
while ((j -= 8));
}while ((i = -~i));
return buf;
}
string Base64_MD5(string plain, integer bit_length) {//$[E20009]
integer H1 = 1732584193;
integer H2 = -271733879;
integer H3 = -1732584194;
integer H4 = 271733878;
//OR on the extra bit.
integer b = (~-(((bit_length + 552) & -512) >> 5));
integer T = llBase64ToInteger(TrimRight(llGetSubString(plain = llStringToBase64(plain), -4, -1),"=")+"AAAA");
string buf = "AAA";
integer i = -5;
do buf += buf; while((i = -~i));
if(bit_length)
{
i = 0x800000;
if(T & 0xFF00)
i = 0x00000080;
else if(T & 0xFF0000)
i = 0x00008000;
}
else
T = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
// llOwnerSay(llList2CSV([i,bit_length]));
plain = llGetSubString( llDeleteSubString(plain, -4, -1) +
llGetSubString(llIntegerToBase64(T | i), 0, 5) +
buf, 0, ((b << 4) / 3) - 7) +
llGetSubString(llIntegerToBase64((((bit_length & 0xFF) << 20) | ((bit_length & 0xFF00) << 4) | ((bit_length >> 12) & 0xFF0) | ((bit_length >> 28) & 0xF)) >> ((b % 3) << 1)), 0, 5) +
"AAAAAAAA";
integer S = i = 0;
list x;
do
{
integer A = H1;
integer B = H2;
integer C = H3;
integer D = H4;
integer F = bit_length = 0;
x = [];
do
{
T = llBase64ToInteger(llGetSubString((buf = llGetSubString(plain, T = ((i + bit_length) << 4) / 3, T+6)) + "AAAAAA", 0, 5)) << (S = (((i + bit_length) % 3) << 1));
if(S)
T = T | (llBase64ToInteger(llGetSubString("A" + (llDeleteSubString(buf, 0, 1)), 0, 5)) >> (6 - S));
x += (T = (((T & 0xFF) << 24) | ((T & 0xFF00) << 8) | ((T >> 8) & 0xFF00) | ((T >> 24) & 0xFF)));
F = A + llList2Integer(k1, bit_length) + T + (D ^ (B & (C ^ D)));
S = B;
T = llList2Integer(r1, bit_length & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(16 > (bit_length = -~bit_length));
do
{
F = A + llList2Integer(k2, bit_length & 15) + (C ^ (D & (B ^ C))) + llList2Integer(x, (5 * bit_length + 1) & 15);
S = B;
T = llList2Integer(r2, bit_length & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(32 > (bit_length = -~bit_length));
do
{
F = A + llList2Integer(k3, bit_length & 15) + (B ^ C ^ D) + llList2Integer(x, (3 * bit_length + 5) & 15);
S = B;
T = llList2Integer(r3, bit_length & 3);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(48 > (bit_length = -~bit_length));
do
{
S = B;
T = llList2Integer(r4, bit_length & 3);
F = A + llList2Integer(k4, bit_length & 15) + (C ^ (B | (~D))) + llList2Integer(x, (7 * bit_length) & 15);
B += (F << T) | ((F >> (32 - T)) & ~(-1 << T));
A = D;
D = C;
C = S;
}
while(64 > (bit_length = -~bit_length));
llOwnerSay(llList2CSV(x));
H1 += A;
H2 += B;
H3 += C;
H4 += D;
}while(b > (i += 16));
x = [H4, H3, H2, H1];
i = -4;
buf = "";
do
{
T = llList2Integer(x,i);
bit_length = 32;
do
buf = llGetSubString(hexc, b = ((T >> (bit_length - 4)) & 0xF), b) + llGetSubString(hexc, b = ((T >> (bit_length - 8)) & 0xF), b) + buf;
while ((bit_length -= 8));
}while ((i = -~i));
return buf;
}
go(string in, string answer)
{
llOwnerSay("");
llResetTime();
string out = UTF8_MD5(in);
float t = llGetTime();
llOwnerSay(llList2CSV([out,t,llStringLength(in)]));
if(answer)llOwnerSay(answer);
}
default
{
state_entry()
{
llListen(0, "", "", "");
go("","D41D8CD98F00B204E9800998ECF8427E");
go("abc","900150983CD24FB0D6963F7D28E17F72");
go("The quick brown fox jumps over the lazy dog","9E107D9D372BB6826BD81D3542A419D6");
// llOwnerSay((string)llGetTime());
}
listen(integer a, string b, key c, string d)
{
llOwnerSay(UTF8_MD5(d));
}
}