User:Chaser Zaks/hmac

From Second Life Wiki
< User:Chaser Zaks
Revision as of 09:31, 11 July 2017 by Chaser Zaks (talk | contribs) (Created page with ""Probably" works. Use at your own risk, it does stuff and seems to work. Keep in mind LSL doesn't allow \0 as a string, so it may still be secure but might not work with other...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

"Probably" works. Use at your own risk, it does stuff and seems to work. Keep in mind LSL doesn't allow \0 as a string, so it may still be secure but might not work with other libraries.

/*
MIT License

Copyright (c) 2017 Chaser Zaks (softhyena.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

list string2Bytes(string str){
    list result;
    integer i;
    integer l = llStringLength(str);
    for(;i<l;i=i+4){
        integer num = llBase64ToInteger(llStringToBase64(llGetSubString(str, i, i+4)));
        integer x;
        for(;x<4;x++){
            result = result + [num >> (24-(8*x)) & 0xFF];
        }
    }
    return llList2List(result, 0, l-1);
}

string bytes2String(list input){
    string result;
    integer i;
    integer l = llGetListLength(input);
    for(;i<l;i=i+4){
        integer num = 0;
        integer x;
        for(;x<4;x++){
            num = num | (llList2Integer(input, i+x) << (24-(8*x)));
            //result = result + [num >> (24-(8*x)) & 0xFF];
        }
        result = result + llBase64ToString(llIntegerToBase64(num));
    }
    return llGetSubString(result, 0, l-1);
}

list xorList(list a, list b){
    list result;
    integer i;
    integer asize = llGetListLength(a);
    integer bsize = llGetListLength(b);
    for(;i<asize;i++){
        result = result + [llList2Integer(a, i) ^ llList2Integer(b, i%bsize)];
    }
    return result;
}

list fillList(list value, integer length){
    list result;
    integer i;
    for(;i<length;i++){
        result = result + value;
    }
    return result;
}

string hmac(string secret, string message){
    if(llStringLength(secret) > 64){
        secret = llSHA1String(secret);
    }
    
    list lSecret = string2Bytes(secret);
    if(llStringLength(secret) < 64){
        lSecret = lSecret + fillList([0x00], (64 - llStringLength(secret)));
    }
    
    list o_key_pad = xorList(fillList([0x5c], 64), lSecret);
    list i_key_pad = xorList(fillList([0x36], 64), lSecret);
   
    return llSHA1String(bytes2String(o_key_pad) + llSHA1String(bytes2String(i_key_pad) + message));
}