Difference between revisions of "SHA-1"

From Second Life Wiki
Jump to navigation Jump to search
m (Added to Library and Examples)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}


(http://www.gnu.org/copyleft/fdl.html) in the spirit of which this script is GPL'd. Copyright (C) 2007 [[User:Xaviar Czervik|Xaviar Czervik]]
Preforms a SHA1 Hash on the text.  Similar to and MD5 hash, although (slightly) more 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).


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details.  
View http://en.wikipedia.org/wiki/SHA1 for more information.


Preforms a SHA1 Hash on the textSimilar to and MD5 hash, although (slightly) more secure.
<pre>//////////////////////////////////////////////////////////////////////////////////////
//
// UTF-8 SHA1
// Version 1.0
// ESL Compiled: "Oct 19 2007", "23:50:33"
// Copyright (C) 2007  Strife Onizuka
//
// https://wiki.secondlife.com/wiki/SHA1
//
// 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 libraryIf 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
//
//////////////////////////////////////////////////////////////////////////////////////


View http://en.wikipedia.org/wiki/SHA1 for more information.
//===================================================//
//                Combined Library                  //
//            "Oct 19 2007", "23:50:33"            //
//  Copyright (C) 2004-2007, Strife Onizuka (cc-by)  //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
//{


<pre>
string TrimRight(string src, string chrs)//Mono Unsafe, LSO Safe
integer ascii(string chr) {
{
     string ASCIII = "              \n                !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz";
     integer i = llStringLength(src);
     if(chr == " ") return 32;
     do;while(~llSubStringIndex(chrs, llGetSubString(src, (i = ~-i), i)) && i);
    return llSubStringIndex(ASCIII, chr);
     return llDeleteSubString(src, (-~(i)), 0xFFFF);
}
integer shiftright(integer number, integer ammount) {
    if (number >= 0) return number >> ammount;
     return (number & 0x7FFFFFFF) >> ammount | 1 << (31 - ammount);
}
}
string hexc="0123456789ABCDEF";
//} Combined Library


integer rol(integer number, integer ammount) {
integer rol(integer number, integer ammount) {
     return (number << ammount) | shiftright(number,(32 - ammount));
     return (number << ammount) | ((number >> (32 - ammount)) & (~(-1 << ammount)));
}
}


list replace(list l, integer p, integer v) {
string UTF8_SHA1(string plain) {
     return llListReplaceList(l, [v], p, p);
    integer H1 = 0x67452301;
}
    integer H2 = 0xefcdab89;
    integer H3 = 0x98badcfe;
     integer H4 = 0x10325476;
    integer H5 = 0xc3d2e1f0;


string hash(string plain) {
    //OR on the extra bit.
     integer = llStringLength(plain) * 16;
     integer j = llStringLength(plain) << 3;
     integer tb = ( b + 576 ) & -512;
     integer b = (~-(((j + 552) & -512) >> 5));
     integer ml  = tb/32;
     integer T = llBase64ToInteger(TrimRight(llGetSubString(plain = llStringToBase64(plain), -4, -1),"=")+"AAAA");
     list Message;
     string buf;
     integer i = 0;
     integer i = 0;
     while (i < ml) {
     for(;i < 85 ; (i = -~i))
         Message += 0;
        buf += "A";
         i++;
    if(j)
    {
         i = 0x800000;
        if(T & 0xFF00)
            i = 0x00000080;
         else if(T & 0xFF0000)
            i = 0x00008000;
     }
     }
     integer pos;
     else
     for (pos = 0; pos < ml; pos++ ) {
        T = i = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
         if ( pos*32 < b) {
//    llOwnerSay(llList2CSV([i,j]));
             integer unicode = ascii(llGetSubString(plain, pos*2, pos*2));
    plain = llGetSubString( llDeleteSubString(plain, -4, -1) +
             Message = replace(Message, pos, ( unicode << 16));
                            llGetSubString(llIntegerToBase64(T | i), 0, 5) +
                            buf, 0, (b << 4) / 3) +
            llIntegerToBase64(j << (6 - ((b % 3) << 1)));
    buf = "";
 
    list x;
     for (i = 0; i < b; i += 16)
    {
         integer A = H1;
        integer B = H2;
        integer C = H3;
        integer D = H4;
        integer E = H5;
        for(j = 0; j < 16; (j = -~j))
        {
            T = ((i + j) << 4) / 3;
             integer offset = ((i + j) % 3) << 1;
            if(offset)
                T = ((llBase64ToInteger(llGetSubString(plain, T, T+5)) << offset) |
                    (llBase64ToInteger("A"+llGetSubString(plain, T+2, T+6)) >> (6 - offset)));
             else
                T = llBase64ToInteger(llGetSubString(plain, T, T+5));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            x += T;
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
         }
         }
         else if ( pos*32 == b)
// llOwnerSay(llList2CSV(hexm(x)));
             Message = replace(Message, pos, 0x80000000);
         for (; j < 20; (j = -~j))
           
        {
        if ((pos*32+16) < b) {
             x = llList2List(x + T = rol(llList2Integer(x, -3) ^
            integer unicode = ascii(llGetSubString(plain, pos*2+1, pos*2+1));
                                        llList2Integer(x, -8) ^
             Message = replace(Message, pos, llList2Integer(Message, pos) | ( unicode & 0xFFFF));
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
         }
         }
         else if ((pos*32+16) == b) {
         for (; j < 40; (j = -~j))
             Message = replace(Message, pos, llList2Integer(Message, pos) | 0x00008000);
        {
             x = llList2List(x + T = rol(llList2Integer(x, -3) ^
                                        llList2Integer(x, -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0x6ed9eba1;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
         }
         }
        for (; j < 60; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; j < 80; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0xca62c1d6;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        H1 += A;
        H2 += B;
        H3 += C;
        H4 += D;
        H5 += E;
    }
    x = [H1, H2, H3, H4, H5];
    for(i=-6; (i = -~i); buf += hex(llList2Integer(x,i)));
    return buf;
}
string Base64_SHA1(string plain, integer bit_length) {
    integer H1 = 0x67452301;
    integer H2 = 0xefcdab89;
    integer H3 = 0x98badcfe;
    integer H4 = 0x10325476;
    integer H5 = 0xc3d2e1f0;
    //OR on the extra bit.
    integer b = (~-(((bit_length + 552) & -512) >> 5));
    integer T = llBase64ToInteger(TrimRight(llGetSubString(plain, -4, -1),"=")+"AAAA");
    string buf;
    integer i = 0;
    for(;i < 85 ; (i = -~i))
        buf += "A";
    if(bit_length)
    {
        i = 0x800000;
        if(T & 0xFF00)
            i = 0x00000080;
        else if(T & 0xFF0000)
            i = 0x00008000;
     }
     }
     Message = replace(Message, ml-1, b);
     else
      
        T = i = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
    integer H1 = h1;
//    llOwnerSay(llList2CSV([i,bit_length]));
    integer H2 = h2;
     plain = llGetSubString( llDeleteSubString(plain, -4, -1) +
    integer H3 = h3;
                            llGetSubString(llIntegerToBase64(T | i), 0, 5) +
    integer H4 = h4;
                            buf, 0, (b << 4) / 3) +
     integer H5 = h5;
            llIntegerToBase64(bit_length << (6 - ((b % 3) << 1)));
    integer intcount = llGetListLength(Message);
     buf = "";
 
     list x;
     list x;
     i = 0;
     for (i = 0; i < b; i += 16)
    while (i < 80) {
     {
        x += 0;
        i++;
    }
    integer j = 0;
     integer block = 0;
    for (block = 0; block < intcount; block += 16) {
        for (j = 0; j < 16; j++)
            x = replace(x, j, llList2Integer(Message, block+j));
        for (j = 16; j < 80; j++)
            x = replace(x, j, rol(llList2Integer(x,j-3)^
                                  llList2Integer(x,j-8)^
                                  llList2Integer(x,j-14)^
                                  llList2Integer(x,j-16), 1));
        integer T;
         integer A = H1;
         integer A = H1;
         integer B = H2;
         integer B = H2;
Line 85: Line 220:
         integer D = H4;
         integer D = H4;
         integer E = H5;
         integer E = H5;
         for (j = 0; j < 80; j++) {
         for(bit_length = 0; bit_length < 16; (bit_length = -~bit_length))
             T = rol(A,5) + part1(j,B,C,D) + E + llList2Integer(x, j) + part2(j);
        {
             E = D;  
            T = ((i + bit_length) << 4) / 3;
             D = C;  
            integer offset = ((i + bit_length) % 3) << 1;
             C = rol(B, 30);  
            if(offset)
             B = A;  
                T = ((llBase64ToInteger(llGetSubString(plain, T, T+5)) << offset) |
                    (llBase64ToInteger("A"+llGetSubString(plain, T+2, T+6)) >> (6 - offset)));
            else
                T = llBase64ToInteger(llGetSubString(plain, T, T+5));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            x += T;
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
// llOwnerSay(llList2CSV(hexm(x)));
        for (; bit_length < 20; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 40; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0x6ed9eba1;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 60; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 80; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x, -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0xca62c1d6;
             E = D;
             D = C;
             C = rol(B, 30);
             B = A;
             A = T;
             A = T;
         }
         }
Line 99: Line 301:
         H5 += E;
         H5 += E;
     }
     }
     string result;
     x = [H1, H2, H3, H4, H5];
    result += (string)(H1); result += " ";
     for(i=-6; (i = -~i); buf += hex(llList2Integer(x,i)));
     result += (string)(H2); result += " ";
     return buf;
    result += (string)(H3); result += " ";
    result += (string)(H4); result += " ";
    result += (string)(H5);;
     return result;
   
}
}


integer h1 = 0x67452301;
string hex(integer value)
integer h2 = 0xefcdab89;
{
integer h3 = 0x98badcfe;
    integer t = 8;
integer h4 = 0x10325476;
    string buf = "";
integer h5 = 0xc3d2e1f0;
    do
 
    {
integer part1(integer t, integer b, integer c,integer d) {
        integer index = value & 0xF;
    return (t<20)*((b & c) | ((~b) & d)) + (t>=20 && t < 40)*(b ^ c ^ d) +
        buf = llGetSubString(hexc, index, index) + buf;
          (t>=40 && t < 60)*((b & c) | (b & d) | (c & d)) + (t>=60)*(b ^ c ^ d);
        value = (0xfffFFFF & (value >> 4));
    } while ((t = ~-t));
    return buf;
}
}


 
default
integer part2(integer t) {
{
    return (t<20)*0x5a827999 + (t>=20&&t<40)*0x6ed9eba1 + (t>=40&&t<60)*0x8f1bbcdc + (t>=60)*0xca62c1d6;
    state_entry()
}
    {
 
        llOwnerSay("");
 
        llOwnerSay(UTF8_SHA1(""));
default {
        llOwnerSay("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");
    state_entry() {
        llOwnerSay("");
         string msg = hash("The quick brown fox jumps over the lazy dog");
        llOwnerSay(UTF8_SHA1("abc"));
         llOwnerSay(msg);
        llOwnerSay("A9993E364706816ABA3E25717850C26C9CD0D89D");
        llOwnerSay("");
         llOwnerSay(UTF8_SHA1("The quick brown fox jumps over the lazy dog"));
         llOwnerSay("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12");
     }
     }
}
}

Revision as of 20:52, 19 October 2007

Preforms a SHA1 Hash on the text. Similar to and MD5 hash, although (slightly) more 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).

View http://en.wikipedia.org/wiki/SHA1 for more information.

//////////////////////////////////////////////////////////////////////////////////////
//
//	UTF-8 SHA1
//	Version 1.0 
//	ESL Compiled: "Oct 19 2007", "23:50:33"
//	Copyright (C) 2007  Strife Onizuka
//	
//	https://wiki.secondlife.com/wiki/SHA1
//
//	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                  //
//             "Oct 19 2007", "23:50:33"             //
//  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


integer rol(integer number, integer ammount) {
    return (number << ammount) | ((number >> (32 - ammount)) & (~(-1 << ammount)));
}

string UTF8_SHA1(string plain) {
    integer H1 = 0x67452301;
    integer H2 = 0xefcdab89;
    integer H3 = 0x98badcfe;
    integer H4 = 0x10325476;
    integer H5 = 0xc3d2e1f0;

    //OR on the extra bit.
    integer j = llStringLength(plain) << 3;
    integer b = (~-(((j + 552) & -512) >> 5));
    integer T = llBase64ToInteger(TrimRight(llGetSubString(plain = llStringToBase64(plain), -4, -1),"=")+"AAAA");
    string buf;
    integer i = 0;
    for(;i < 85 ; (i = -~i))
        buf += "A";
    if(j)
    {
        i = 0x800000;
        if(T & 0xFF00)
            i = 0x00000080;
        else if(T & 0xFF0000)
            i = 0x00008000;
    }
    else
        T = i = 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) + 
            llIntegerToBase64(j << (6 - ((b % 3) << 1)));
    buf = "";

    list x;
    for (i = 0; i < b; i += 16)
    {
        integer A = H1;
        integer B = H2;
        integer C = H3;
        integer D = H4;
        integer E = H5;
        for(j = 0; j < 16; (j = -~j))
        {
            T = ((i + j) << 4) / 3;
            integer offset = ((i + j) % 3) << 1;
            if(offset)
                T = ((llBase64ToInteger(llGetSubString(plain, T, T+5)) << offset) | 
                     (llBase64ToInteger("A"+llGetSubString(plain, T+2, T+6)) >> (6 - offset)));
            else
                T = llBase64ToInteger(llGetSubString(plain, T, T+5));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            x += T;
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
//		llOwnerSay(llList2CSV(hexm(x)));
        for (; j < 20; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; j < 40; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0x6ed9eba1;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; j < 60; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; j < 80; (j = -~j))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0xca62c1d6;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        H1 += A;
        H2 += B;
        H3 += C;
        H4 += D;
        H5 += E;
    }
    x = [H1, H2, H3, H4, H5];
    for(i=-6; (i = -~i); buf += hex(llList2Integer(x,i)));
    return buf;
}

string Base64_SHA1(string plain, integer bit_length) {
    integer H1 = 0x67452301;
    integer H2 = 0xefcdab89;
    integer H3 = 0x98badcfe;
    integer H4 = 0x10325476;
    integer H5 = 0xc3d2e1f0;

    //OR on the extra bit.
    integer b = (~-(((bit_length + 552) & -512) >> 5));
    integer T = llBase64ToInteger(TrimRight(llGetSubString(plain, -4, -1),"=")+"AAAA");
    string buf;
    integer i = 0;
    for(;i < 85 ; (i = -~i))
        buf += "A";
    if(bit_length)
    {
        i = 0x800000;
        if(T & 0xFF00)
            i = 0x00000080;
        else if(T & 0xFF0000)
            i = 0x00008000;
    }
    else
        T = i = 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) + 
            llIntegerToBase64(bit_length << (6 - ((b % 3) << 1)));
    buf = "";

    list x;
    for (i = 0; i < b; i += 16)
    {
        integer A = H1;
        integer B = H2;
        integer C = H3;
        integer D = H4;
        integer E = H5;
        for(bit_length = 0; bit_length < 16; (bit_length = -~bit_length))
        {
            T = ((i + bit_length) << 4) / 3;
            integer offset = ((i + bit_length) % 3) << 1;
            if(offset)
                T = ((llBase64ToInteger(llGetSubString(plain, T, T+5)) << offset) | 
                     (llBase64ToInteger("A"+llGetSubString(plain, T+2, T+6)) >> (6 - offset)));
            else
                T = llBase64ToInteger(llGetSubString(plain, T, T+5));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            x += T;
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
//		llOwnerSay(llList2CSV(hexm(x)));
        for (; bit_length < 20; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 40; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0x6ed9eba1;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 60; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        for (; bit_length < 80; (bit_length = -~bit_length))
        {
            x = llList2List(x + T = rol(llList2Integer(x,  -3) ^
                                        llList2Integer(x,  -8) ^
                                        llList2Integer(x, -14) ^
                                        llList2Integer(x, -16), 1), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
            T += rol(A,5) + (B ^ C ^ D) + E + 0xca62c1d6;
            E = D;
            D = C;
            C = rol(B, 30);
            B = A;
            A = T;
        }
        H1 += A;
        H2 += B;
        H3 += C;
        H4 += D;
        H5 += E;
    }
    x = [H1, H2, H3, H4, H5];
    for(i=-6; (i = -~i); buf += hex(llList2Integer(x,i)));
    return buf;
}

string hex(integer value)
{
    integer t = 8;
    string buf = "";
    do
    {
        integer index = value & 0xF;
        buf = llGetSubString(hexc, index, index) + buf;
        value = (0xfffFFFF & (value >> 4));
    } while ((t = ~-t));
    return buf;
}

default
{
    state_entry()
    {
        llOwnerSay("");
        llOwnerSay(UTF8_SHA1(""));
        llOwnerSay("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");
        llOwnerSay("");
        llOwnerSay(UTF8_SHA1("abc"));
        llOwnerSay("A9993E364706816ABA3E25717850C26C9CD0D89D");
        llOwnerSay("");
        llOwnerSay(UTF8_SHA1("The quick brown fox jumps over the lazy dog"));
        llOwnerSay("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12");
    }
}