Porting SHA512 Javascript implemention to Actionscript

前端 未结 2 585
耶瑟儿~
耶瑟儿~ 2021-01-16 11:49

I am wondering if anyone can help me in porting a SHA-512 implemention in Javascript to Actionscript. Since both Javascript and Actionscript share the same origin, I think p

2条回答
  •  难免孤独
    2021-01-16 12:25

    I am not able to post a comment on the above (where this should be), but there is an error with some signatures using this code due to Actionscript's imaginative behaviour when appending the null character to a string (it doesn't).

    For a quick and dirty fix to the hex encoded hmac which is what I am using, I changed the hex_hmac_sha512 function (just for the encoding it calls) and added a binb2hex function as follows:

        private function hex_hmac_sha512(key, data) {
            key = str2rstr_utf8(key);
            data = str2rstr_utf8(data);
            var bkey = rstr2binb(key);
            if(bkey.length > 32) bkey = binb_sha512(bkey, key.length * 8);
    
            var ipad = new Array(32)
            var opad = new Array(32);
            for(var i = 0; i < 32; i++) {
                    ipad[i] = bkey[i] ^ 0x36363636;
                    opad[i] = bkey[i] ^ 0x5C5C5C5C;
            }
    
            var hash = binb_sha512(ipad.concat(rstr2binb(data)), 1024 + data.length * 8);
            return binb2hex(binb_sha512(opad.concat(hash), 1024 + 512));
    }
    
    private function binb2hex(input) {
        var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";        
        var output = "";
        for(var i = 0; i < input.length * 32; i += 8){
                var schar = (input[i>>5] >>> (24 - i % 32)) & 0xFF;
                output += hex_tab.charAt((schar >>> 4) & 0x0F) + hex_tab.charAt(schar & 0x0F);              
        }
        return output;      
    }
    

提交回复
热议问题