Difference between revisions of "SHA-1"

From Second Life Wiki
Jump to navigation Jump to search
m (Speak in past time instead of future. Point out that the function that is no longer necessary is UTF8_SHA1, as there's still no replacement for Base64_SHA1.)
(Updated so it works and so it includes the test string suggested)
Line 12: Line 12:
//
//
// UTF-8 SHA1
// UTF-8 SHA1
// Version 1.1
// Version 1.2
// ESL Compiled: "Nov 25 2007", "20:49:21"
// ESL Compiled: "Dec 12 2010", "04:04:47"
// Copyright (C) 2007 Strife Onizuka
// Copyright (C) 2010 Strife Onizuka
// Based on Pseudo-code from http://en.wikipedia.org/wiki/SHA1
// Based on Pseudo-code from http://en.wikipedia.org/wiki/SHA1
// https://wiki.secondlife.com/wiki/SHA1
// https://wiki.secondlife.com/wiki/SHA1
Line 37: Line 37:
//===================================================//
//===================================================//
//                Combined Library                  //
//                Combined Library                  //
//            "Nov 25 2007", "20:49:21"            //
//            "Dec 12 2010", "04:04:47"            //
//  Copyright (C) 2004-2007, Strife Onizuka (cc-by)  //
//  Copyright (C) 2004-2008, Strife Onizuka (cc-by)  //
//    http://creativecommons.org/licenses/by/3.0/    //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
//===================================================//
//{
//{


//Functions marked "Mono Safe" are safe for use in LSLEditor & Mono
string TrimRight(string src, string chrs)//LSLEditor Safe, LSO Safe
//Functions marked "LSO Safe" are safe for use in LSO
//Functions marked "Double Safe" are safe for use in VM's that support doubles (Mono, LSLEditor).
//Mono is the future VM for LSL, LSO is the current VM of LSL.
//To achieve safety, the functions require more bytecode and sacrifice a bit of performance.
 
string TrimRight(string src, string chrs)//Mono Safe, LSO Safe
{
{
     integer i = llStringLength(src);
     integer i = llStringLength(src);
     integer j = i;
     integer j = i;
     do{;}while(~llSubStringIndex(chrs, llGetSubString(src, i = (~-(j = i)), i)) && j);
     do ; while(~llSubStringIndex(chrs, llGetSubString(src, i = ~-(j = i), i)) && j);
     return llDeleteSubString(src, j, 0xFFFF);
     return llDeleteSubString(src, j, 0x7FFFFFF0);
}
}


Line 61: Line 55:
//} Combined Library
//} Combined Library


 
string Base64_SHA1(string plain, integer bit_length) { //$[E20009]
string UTF8_SHA1(string plain) {
     integer H1 = 0x67452301;
     integer H1 = 0x67452301;
     integer H2 = 0xefcdab89;
     integer H2 = 0xefcdab89;
Line 70: Line 63:


     //OR on the extra bit.
     //OR on the extra bit.
    integer j = llStringLength(plain) << 3;
     integer b = ((bit_length + 40) >> 5) | 15;
     integer b = (~-(((j + 552) & -512) >> 5));
     integer T = llBase64ToInteger((TrimRight(llGetSubString(plain, -4, -1),"=")) + "AAAA");
     integer T = llBase64ToInteger(llGetSubString((TrimRight(llGetSubString(plain = llStringToBase64(plain), -4, -1),"=")) + "AAAAAAAA", 0, 7));
     string buf = "AAA";
     string buf = "AAA";
     integer i = -5;
     integer i = -5;
     do buf += buf; while((i = -~i));
     do buf += buf; while((i = -~i));
     if(j)
     if(bit_length)
     {
     {
         i = 0x800000;
         i = 0x800000;
Line 86: Line 78:
     else
     else
         T = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
         T = 0x80000000;//T is corrupt because of https://jira.secondlife.com/browse/SVC-104
//    llOwnerSay(llList2CSV([i,j]));
//    llOwnerSay(llList2CSV([i,bit_length]));
     plain = llGetSubString( llDeleteSubString(plain, -4, -1) +  
     plain = llGetSubString( llDeleteSubString(plain, -4, -1) +  
                             llGetSubString(llIntegerToBase64(T | i), 0, 5) +  
                             llGetSubString(llIntegerToBase64(T | i), 0, 5) +  
                             buf, 0, (b << 4) / 3) +  
                             buf, 0, (b << 4) / 3) +  
             llIntegerToBase64(j << (6 - ((b % 3) << 1)));
             llIntegerToBase64(bit_length << (6 - ((b % 3) << 1)));
     list x;
     list x;
     integer S = i = 0;
     integer S = i = 0;
Line 100: Line 92:
         integer D = H4;
         integer D = H4;
         integer E = H5;
         integer E = H5;
         x = (list)(j = 0);//the zero gets flushed off the stack by the later loops
         x = (list)(bit_length = 0);//the zero gets flushed off the stack by the later loops
         do
         do
         {
         {
             T = llBase64ToInteger((buf = llGetSubString(plain, T = ((i + j) << 4) / 3, T+6)) + "A") << (S = ((i + j) % 3) << 1);
             T = llBase64ToInteger(buf = llGetSubString(plain, T = ((i + bit_length) << 4) / 3, T+6)) << (S = ((i + bit_length) % 3) << 1);
             if(S)
             if(S)
                 T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1)) + "AA") >> (6 - S));
                 T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1))) >> (6 - S));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             x += T;
             x += T;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
Line 114: Line 106:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(16 > (j = -~j));
         }while(16 > (bit_length = -~bit_length));
// llOwnerSay(llList2CSV(hexm(x)));
// llOwnerSay(llList2CSV(hexm(x)));
         do
         do
Line 120: Line 112:
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             E = D;
             E = D;
Line 127: Line 119:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(20 > (j = -~j));
         }while(20 > (bit_length = -~bit_length));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
             E = D;
             E = D;
Line 139: Line 131:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(40 > (j = -~j));
         }while(40 > (bit_length = -~bit_length));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
             E = D;
             E = D;
Line 151: Line 143:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(60 > (j = -~j));
         }while(60 > (bit_length = -~bit_length));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)j+"]="+hex(T));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
             E = D;
             E = D;
Line 163: Line 155:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(80 > (j = -~j));
         }while(80 > (bit_length = -~bit_length));
         H1 += A;
         H1 += A;
         H2 += B;
         H2 += B;
Line 176: Line 168:
     {
     {
         T = llList2Integer(x,i);
         T = llList2Integer(x,i);
         j = 32;
         bit_length = 32;
         do
         do
             buf += llGetSubString(hexc, b = ((T >> (j -= 4)) & 0xF), b);
             buf += llGetSubString(hexc, b = ((T >> (bit_length -= 4)) & 0xF), b);
         while (j);
         while (bit_length);
     }while ((i = -~i));
     }while ((i = -~i));
     return buf;
     return buf;
}
}


string Base64_SHA1(string plain, integer bit_length) {//$[E20009]
string UTF8_SHA1(string plain) {  
     integer H1 = 0x67452301;
     integer H1 = 0x67452301;
     integer H2 = 0xefcdab89;
     integer H2 = 0xefcdab89;
Line 192: Line 184:


     //OR on the extra bit.
     //OR on the extra bit.
     integer b = (~-(((bit_length + 552) & -512) >> 5));
     integer j = llStringLength(plain = llStringToBase64(plain));
    integer T = llBase64ToInteger(llGetSubString((TrimRight(llGetSubString(plain, -4, -1),"=")) + "AAAAAAAA", 0, 7));
    integer T = 0x80000000;
    if (j) {
        do ; while (llGetSubString(plain, (j = ~-j), j) == "=");
        j = (-6 * ~j) & -8;
        T = llBase64ToInteger((llGetSubString(plain, -4, j / 6)) + "AAAAAA") | (0x00000080 << ((j % 3) << 3));
    }
    integer b = ((j + 40) >> 5) | 15;
     string buf = "AAA";
     string buf = "AAA";
     integer i = -5;
     integer i = -5;
     do buf += buf; while((i = -~i));
     do buf += buf; while((i = -~i));
    if(bit_length)
//    llOwnerSay(llList2CSV([i,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,bit_length]));
     plain = llGetSubString( llDeleteSubString(plain, -4, -1) +  
     plain = llGetSubString( llDeleteSubString(plain, -4, -1) +  
                             llGetSubString(llIntegerToBase64(T | i), 0, 5) +  
                             llGetSubString(llIntegerToBase64(T), 0, 5) +  
                             buf, 0, (b << 4) / 3) +  
                             buf, 0, (b << 4) / 3) +
             llIntegerToBase64(bit_length << (6 - ((b % 3) << 1)));
             llIntegerToBase64(j << (6 - ((b % 3) << 1)));
     list x;
     list x;
     integer S = i = 0;
     integer S = i = 0;
Line 221: Line 209:
         integer D = H4;
         integer D = H4;
         integer E = H5;
         integer E = H5;
         x = (list)(bit_length = 0);//the zero gets flushed off the stack by the later loops
         x = (list)(j = 0);//the zero gets flushed off the stack by the later loops
         do
         do
         {
         {
             T = llBase64ToInteger((buf = llGetSubString(plain, T = ((i + bit_length) << 4) / 3, T+6)) + "A") << (S = ((i + bit_length) % 3) << 1);
             T = llBase64ToInteger(buf = llGetSubString(plain, T = ((i + j) << 4) / 3, T+6)) << (S = ((i + j) % 3) << 1);
             if(S)
             if(S)
                 T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1)) + "AA") >> (6 - S));
                 T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1))) >> (6 - S));
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             x += T;
             x += T;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
Line 235: Line 223:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(16 > (bit_length = -~bit_length));
         }while(16 > (j = -~j));
// llOwnerSay(llList2CSV(hexm(x)));
// llOwnerSay(llList2CSV(hexm(x)));
         do
         do
Line 241: Line 229:
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
             E = D;
             E = D;
Line 248: Line 236:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(20 > (bit_length = -~bit_length));
         }while(20 > (j = -~j));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
             E = D;
             E = D;
Line 260: Line 248:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(40 > (bit_length = -~bit_length));
         }while(40 > (j = -~j));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
             E = D;
             E = D;
Line 272: Line 260:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(60 > (bit_length = -~bit_length));
         }while(60 > (j = -~j));
         do
         do
         {
         {
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
             x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);
//            llOwnerSay("W["+(string)bit_length+"]="+hex(T));
//            llOwnerSay("W["+(string)j+"]="+hex(T));
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
             T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
             E = D;
             E = D;
Line 284: Line 272:
             B = A;
             B = A;
             A = T;
             A = T;
         }while(80 > (bit_length = -~bit_length));
         }while(80 > (j = -~j));
         H1 += A;
         H1 += A;
         H2 += B;
         H2 += B;
Line 297: Line 285:
     {
     {
         T = llList2Integer(x,i);
         T = llList2Integer(x,i);
         bit_length = 32;
         j = 32;
         do
         do
             buf += llGetSubString(hexc, b = ((T >> (bit_length -= 4)) & 0xF), b);
             buf += llGetSubString(hexc, b = ((T >> (j -= 4)) & 0xF), b);
         while (bit_length);
         while (j);
     }while ((i = -~i));
     }while ((i = -~i));
     return buf;
     return buf;
}
}


go(string in, string answer)
 
integer go(string in, string answer)
{
{
     llOwnerSay("");
     llOwnerSay("");
Line 311: Line 300:
     string out = UTF8_SHA1(in);
     string out = UTF8_SHA1(in);
     float t = llGetTime();
     float t = llGetTime();
     llOwnerSay(llList2CSV([out,t,llStringLength(in)]));
     llOwnerSay(llDumpList2String(([out,t,llStringLength(in)]),","));
     if(answer)llOwnerSay(answer);
     if(answer)
    {
        llOwnerSay(answer);
        return answer == out;
    }
    return TRUE;
}
}


Line 319: Line 313:
     state_entry()
     state_entry()
     {
     {
         go("", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");
         if(go("", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"))
        go("abc", "A9993E364706816ABA3E25717850C26C9CD0D89D");
            if(go("abc", "A9993E364706816ABA3E25717850C26C9CD0D89D"))
        go("The quick brown fox jumps over the lazy dog", "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12");
                if(go("The quick brown fox jumps over the lazy dog", "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"))
        go("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF", "");
                    if(go("Étude¿º", llSHA1String("Étude¿º")))
                        go("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF", "");
//        llOwnerSay((string)llGetTime());
//        llOwnerSay((string)llGetTime());
     }
     }

Revision as of 02:07, 12 December 2010

 NEW  LSL now includes the function llSHA1String, which removes the need for the UTF8_SHA1 variant from this library.

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).

There is also an SHA2 implementations (224 & 256).

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

<lsl>////////////////////////////////////////////////////////////////////////////////////// // // UTF-8 SHA1 // Version 1.2 // ESL Compiled: "Dec 12 2010", "04:04:47" // Copyright (C) 2010 Strife Onizuka // Based on Pseudo-code from http://en.wikipedia.org/wiki/SHA1 // 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 // // "Dec 12 2010", "04:04:47" // // Copyright (C) 2004-2008, Strife Onizuka (cc-by) // // http://creativecommons.org/licenses/by/3.0/ // //===================================================// //{

string TrimRight(string src, string chrs)//LSLEditor Safe, LSO Safe {

   integer i = llStringLength(src);
   integer j = i;
   do ; while(~llSubStringIndex(chrs, llGetSubString(src, i = ~-(j = i), i)) && j);
   return llDeleteSubString(src, j, 0x7FFFFFF0);

}

string hexc="0123456789ABCDEF";

//} Combined Library

string Base64_SHA1(string plain, integer bit_length) { //$[E20009]

   integer H1 = 0x67452301;
   integer H2 = 0xefcdab89;
   integer H3 = 0x98badcfe;
   integer H4 = 0x10325476;
   integer H5 = 0xc3d2e1f0;
   //OR on the extra bit.
   integer b = ((bit_length + 40) >> 5) | 15;
   integer T = llBase64ToInteger((TrimRight(llGetSubString(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) + 
           llIntegerToBase64(bit_length << (6 - ((b % 3) << 1)));
   list x;
   integer S = i = 0;
   do
   {
       integer A = H1;
       integer B = H2;
       integer C = H3;
       integer D = H4;
       integer E = H5;
       x = (list)(bit_length = 0);//the zero gets flushed off the stack by the later loops
       do
       {
           T = llBase64ToInteger(buf = llGetSubString(plain, T = ((i + bit_length) << 4) / 3, T+6)) << (S = ((i + bit_length) % 3) << 1);
           if(S)
               T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1))) >> (6 - S));

// llOwnerSay("W["+(string)bit_length+"]="+hex(T));

           x += T;
           T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(16 > (bit_length = -~bit_length));

// llOwnerSay(llList2CSV(hexm(x)));

       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)bit_length+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(20 > (bit_length = -~bit_length));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)bit_length+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(40 > (bit_length = -~bit_length));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)bit_length+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(60 > (bit_length = -~bit_length));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)bit_length+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(80 > (bit_length = -~bit_length));
       H1 += A;
       H2 += B;
       H3 += C;
       H4 += D;
       H5 += E;
   }while(b > (i += 16));
   x = [H1, H2, H3, H4, H5];
   i = -5;
   buf = "";
   do
   {
       T = llList2Integer(x,i);
       bit_length = 32;
       do
           buf += llGetSubString(hexc, b = ((T >> (bit_length -= 4)) & 0xF), b);
       while (bit_length);
   }while ((i = -~i));
   return buf;

}

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 = llStringToBase64(plain));
   integer T = 0x80000000;
   if (j) {
       do ; while (llGetSubString(plain, (j = ~-j), j) == "=");
       j = (-6 * ~j) & -8;
       T = llBase64ToInteger((llGetSubString(plain, -4, j / 6)) + "AAAAAA") | (0x00000080 << ((j % 3) << 3));
   }
   integer b = ((j + 40) >> 5) | 15;
   string buf = "AAA";
   integer i = -5;
   do buf += buf; while((i = -~i));

// llOwnerSay(llList2CSV([i,j]));

   plain = llGetSubString( llDeleteSubString(plain, -4, -1) + 
                           llGetSubString(llIntegerToBase64(T), 0, 5) + 
                           buf, 0, (b << 4) / 3) +
           llIntegerToBase64(j << (6 - ((b % 3) << 1)));
   list x;
   integer S = i = 0;
   do
   {
       integer A = H1;
       integer B = H2;
       integer C = H3;
       integer D = H4;
       integer E = H5;
       x = (list)(j = 0);//the zero gets flushed off the stack by the later loops
       do
       {
           T = llBase64ToInteger(buf = llGetSubString(plain, T = ((i + j) << 4) / 3, T+6)) << (S = ((i + j) % 3) << 1);
           if(S)
               T = T | (llBase64ToInteger("A" + (llDeleteSubString(buf, 0, 1))) >> (6 - S));

// llOwnerSay("W["+(string)j+"]="+hex(T));

           x += T;
           T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(16 > (j = -~j));

// llOwnerSay(llList2CSV(hexm(x)));

       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)j+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (D ^ (B & (C ^ D))) + E + 0x5a827999;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(20 > (j = -~j));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)j+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0x6ed9eba1;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(40 > (j = -~j));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)j+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + ((B & C) | (B & D) | (C & D)) + E + 0x8f1bbcdc;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(60 > (j = -~j));
       do
       {
           S = llList2Integer(x,  -3) ^ llList2Integer(x,  -8) ^ llList2Integer(x, -14) ^ llList2Integer(x, -16);
           x = llList2List(x + (T = ((S << 1) | !!(S & 0x80000000))), -16, -1);

// llOwnerSay("W["+(string)j+"]="+hex(T));

           T += ((A << 5) | ((A >> 27) & 0x1F)) + (B ^ C ^ D) + E + 0xca62c1d6;
           E = D;
           D = C;
           C = ((B << 30) | ((B >> 2) & 0x3FFFFFFF));
           B = A;
           A = T;
       }while(80 > (j = -~j));
       H1 += A;
       H2 += B;
       H3 += C;
       H4 += D;
       H5 += E;
   }while(b > (i += 16));
   x = [H1, H2, H3, H4, H5];
   i = -5;
   buf = "";
   do
   {
       T = llList2Integer(x,i);
       j = 32;
       do
           buf += llGetSubString(hexc, b = ((T >> (j -= 4)) & 0xF), b);
       while (j);
   }while ((i = -~i));
   return buf;

}


integer go(string in, string answer) {

   llOwnerSay("");
   llResetTime();
   string out = UTF8_SHA1(in);
   float t = llGetTime();
   llOwnerSay(llDumpList2String(([out,t,llStringLength(in)]),","));
   if(answer)
   {
       llOwnerSay(answer);
       return answer == out;
   }
   return TRUE;

}

default {

   state_entry()
   {
       if(go("", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"))
           if(go("abc", "A9993E364706816ABA3E25717850C26C9CD0D89D"))
               if(go("The quick brown fox jumps over the lazy dog", "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"))
                   if(go("Étude¿º", llSHA1String("Étude¿º")))
                       go("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF95601890AFD80709DA39A3EE5E6B4B0D3255BFEF", "");

// llOwnerSay((string)llGetTime());

   }

}</lsl>