FireFox Extenstion - Using Components - Hex Decoding - javascript

I am working on a FireFox Extension and want to know what are these codes and what do they do :
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
var windows = wm.getEnumerator("navigator:browser");
global_win = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
var my_integer = check_me_1() + check_me_2();
alert(my_integer);
function check_me_1() {
try {
var _0x2cb6 = ["\x4E\x74\x74", "\x77\x69\x64\x74\x68", "\x73\x63\x72\x65\x65\x6E", "\x2E", "\x68\x65\x69\x67\x68\x74", "\x0A", "\x72\x65\x70\x6C\x61\x63\x65", "", "\x6C\x65\x6E\x67\x74\x68", "\x63\x68\x61\x72\x43\x6F\x64\x65\x41\x74", "\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65", "\x30\x30\x30\x30\x30\x30\x30\x30\x20\x37\x37\x30\x37\x33\x30\x39\x36\x20\x45\x45\x30\x45\x36\x31\x32\x43\x20\x39\x39\x30\x39\x35\x31\x42\x41\x20\x30\x37\x36\x44\x43\x34\x31\x39\x20\x37\x30\x36\x41\x46\x34\x38\x46\x20\x45\x39\x36\x33\x41\x35\x33\x35\x20\x39\x45\x36\x34\x39\x35\x41\x33\x20\x30\x45\x44\x42\x38\x38\x33\x32\x20\x37\x39\x44\x43\x42\x38\x41\x34\x20\x45\x30\x44\x35\x45\x39\x31\x45\x20\x39\x37\x44\x32\x44\x39\x38\x38\x20\x30\x39\x42\x36\x34\x43\x32\x42\x20\x37\x45\x42\x31\x37\x43\x42\x44\x20\x45\x37\x42\x38\x32\x44\x30\x37\x20\x39\x30\x42\x46\x31\x44\x39\x31\x20\x31\x44\x42\x37\x31\x30\x36\x34\x20\x36\x41\x42\x30\x32\x30\x46\x32\x20\x46\x33\x42\x39\x37\x31\x34\x38\x20\x38\x34\x42\x45\x34\x31\x44\x45\x20\x31\x41\x44\x41\x44\x34\x37\x44\x20\x36\x44\x44\x44\x45\x34\x45\x42\x20\x46\x34\x44\x34\x42\x35\x35\x31\x20\x38\x33\x44\x33\x38\x35\x43\x37\x20\x31\x33\x36\x43\x39\x38\x35\x36\x20\x36\x34\x36\x42\x41\x38\x43\x30\x20\x46\x44\x36\x32\x46\x39\x37\x41\x20\x38\x41\x36\x35\x43\x39\x45\x43\x20\x31\x34\x30\x31\x35\x43\x34\x46\x20\x36\x33\x30\x36\x36\x43\x44\x39\x20\x46\x41\x30\x46\x33\x44\x36\x33\x20\x38\x44\x30\x38\x30\x44\x46\x35\x20\x33\x42\x36\x45\x32\x30\x43\x38\x20\x34\x43\x36\x39\x31\x30\x35\x45\x20\x44\x35\x36\x30\x34\x31\x45\x34\x20\x41\x32\x36\x37\x37\x31\x37\x32\x20\x33\x43\x30\x33\x45\x34\x44\x31\x20\x34\x42\x30\x34\x44\x34\x34\x37\x20\x44\x32\x30\x44\x38\x35\x46\x44\x20\x41\x35\x30\x41\x42\x35\x36\x42\x20\x33\x35\x42\x35\x41\x38\x46\x41\x20\x34\x32\x42\x32\x39\x38\x36\x43\x20\x44\x42\x42\x42\x43\x39\x44\x36\x20\x41\x43\x42\x43\x46\x39\x34\x30\x20\x33\x32\x44\x38\x36\x43\x45\x33\x20\x34\x35\x44\x46\x35\x43\x37\x35\x20\x44\x43\x44\x36\x30\x44\x43\x46\x20\x41\x42\x44\x31\x33\x44\x35\x39\x20\x32\x36\x44\x39\x33\x30\x41\x43\x20\x35\x31\x44\x45\x30\x30\x33\x41\x20\x43\x38\x44\x37\x35\x31\x38\x30\x20\x42\x46\x44\x30\x36\x31\x31\x36\x20\x32\x31\x42\x34\x46\x34\x42\x35\x20\x35\x36\x42\x33\x43\x34\x32\x33\x20\x43\x46\x42\x41\x39\x35\x39\x39\x20\x42\x38\x42\x44\x41\x35\x30\x46\x20\x32\x38\x30\x32\x42\x38\x39\x45\x20\x35\x46\x30\x35\x38\x38\x30\x38\x20\x43\x36\x30\x43\x44\x39\x42\x32\x20\x42\x31\x30\x42\x45\x39\x32\x34\x20\x32\x46\x36\x46\x37\x43\x38\x37\x20\x35\x38\x36\x38\x34\x43\x31\x31\x20\x43\x31\x36\x31\x31\x44\x41\x42\x20\x42\x36\x36\x36\x32\x44\x33\x44\x20\x37\x36\x44\x43\x34\x31\x39\x30\x20\x30\x31\x44\x42\x37\x31\x30\x36\x20\x39\x38\x44\x32\x32\x30\x42\x43\x20\x45\x46\x44\x35\x31\x30\x32\x41\x20\x37\x31\x42\x31\x38\x35\x38\x39\x20\x30\x36\x42\x36\x42\x35\x31\x46\x20\x39\x46\x42\x46\x45\x34\x41\x35\x20\x45\x38\x42\x38\x44\x34\x33\x33\x20\x37\x38\x30\x37\x43\x39\x41\x32\x20\x30\x46\x30\x30\x46\x39\x33\x34\x20\x39\x36\x30\x39\x41\x38\x38\x45\x20\x45\x31\x30\x45\x39\x38\x31\x38\x20\x37\x46\x36\x41\x30\x44\x42\x42\x20\x30\x38\x36\x44\x33\x44\x32\x44\x20\x39\x31\x36\x34\x36\x43\x39\x37\x20\x45\x36\x36\x33\x35\x43\x30\x31\x20\x36\x42\x36\x42\x35\x31\x46\x34\x20\x31\x43\x36\x43\x36\x31\x36\x32\x20\x38\x35\x36\x35\x33\x30\x44\x38\x20\x46\x32\x36\x32\x30\x30\x34\x45\x20\x36\x43\x30\x36\x39\x35\x45\x44\x20\x31\x42\x30\x31\x41\x35\x37\x42\x20\x38\x32\x30\x38\x46\x34\x43\x31\x20\x46\x35\x30\x46\x43\x34\x35\x37\x20\x36\x35\x42\x30\x44\x39\x43\x36\x20\x31\x32\x42\x37\x45\x39\x35\x30\x20\x38\x42\x42\x45\x42\x38\x45\x41\x20\x46\x43\x42\x39\x38\x38\x37\x43\x20\x36\x32\x44\x44\x31\x44\x44\x46\x20\x31\x35\x44\x41\x32\x44\x34\x39\x20\x38\x43\x44\x33\x37\x43\x46\x33\x20\x46\x42\x44\x34\x34\x43\x36\x35\x20\x34\x44\x42\x32\x36\x31\x35\x38\x20\x33\x41\x42\x35\x35\x31\x43\x45\x20\x41\x33\x42\x43\x30\x30\x37\x34\x20\x44\x34\x42\x42\x33\x30\x45\x32\x20\x34\x41\x44\x46\x41\x35\x34\x31\x20\x33\x44\x44\x38\x39\x35\x44\x37\x20\x41\x34\x44\x31\x43\x34\x36\x44\x20\x44\x33\x44\x36\x46\x34\x46\x42\x20\x34\x33\x36\x39\x45\x39\x36\x41\x20\x33\x34\x36\x45\x44\x39\x46\x43\x20\x41\x44\x36\x37\x38\x38\x34\x36\x20\x44\x41\x36\x30\x42\x38\x44\x30\x20\x34\x34\x30\x34\x32\x44\x37\x33\x20\x33\x33\x30\x33\x31\x44\x45\x35\x20\x41\x41\x30\x41\x34\x43\x35\x46\x20\x44\x44\x30\x44\x37\x43\x43\x39\x20\x35\x30\x30\x35\x37\x31\x33\x43\x20\x32\x37\x30\x32\x34\x31\x41\x41\x20\x42\x45\x30\x42\x31\x30\x31\x30\x20\x43\x39\x30\x43\x32\x30\x38\x36\x20\x35\x37\x36\x38\x42\x35\x32\x35\x20\x32\x30\x36\x46\x38\x35\x42\x33\x20\x42\x39\x36\x36\x44\x34\x30\x39\x20\x43\x45\x36\x31\x45\x34\x39\x46\x20\x35\x45\x44\x45\x46\x39\x30\x45\x20\x32\x39\x44\x39\x43\x39\x39\x38\x20\x42\x30\x44\x30\x39\x38\x32\x32\x20\x43\x37\x44\x37\x41\x38\x42\x34\x20\x35\x39\x42\x33\x33\x44\x31\x37\x20\x32\x45\x42\x34\x30\x44\x38\x31\x20\x42\x37\x42\x44\x35\x43\x33\x42\x20\x43\x30\x42\x41\x36\x43\x41\x44\x20\x45\x44\x42\x38\x38\x33\x32\x30\x20\x39\x41\x42\x46\x42\x33\x42\x36\x20\x30\x33\x42\x36\x45\x32\x30\x43\x20\x37\x34\x42\x31\x44\x32\x39\x41\x20\x45\x41\x44\x35\x34\x37\x33\x39\x20\x39\x44\x44\x32\x37\x37\x41\x46\x20\x30\x34\x44\x42\x32\x36\x31\x35\x20\x37\x33\x44\x43\x31\x36\x38\x33\x20\x45\x33\x36\x33\x30\x42\x31\x32\x20\x39\x34\x36\x34\x33\x42\x38\x34\x20\x30\x44\x36\x44\x36\x41\x33\x45\x20\x37\x41\x36\x41\x35\x41\x41\x38\x20\x45\x34\x30\x45\x43\x46\x30\x42\x20\x39\x33\x30\x39\x46\x46\x39\x44\x20\x30\x41\x30\x30\x41\x45\x32\x37\x20\x37\x44\x30\x37\x39\x45\x42\x31\x20\x46\x30\x30\x46\x39\x33\x34\x34\x20\x38\x37\x30\x38\x41\x33\x44\x32\x20\x31\x45\x30\x31\x46\x32\x36\x38\x20\x36\x39\x30\x36\x43\x32\x46\x45\x20\x46\x37\x36\x32\x35\x37\x35\x44\x20\x38\x30\x36\x35\x36\x37\x43\x42\x20\x31\x39\x36\x43\x33\x36\x37\x31\x20\x36\x45\x36\x42\x30\x36\x45\x37\x20\x46\x45\x44\x34\x31\x42\x37\x36\x20\x38\x39\x44\x33\x32\x42\x45\x30\x20\x31\x30\x44\x41\x37\x41\x35\x41\x20\x36\x37\x44\x44\x34\x41\x43\x43\x20\x46\x39\x42\x39\x44\x46\x36\x46\x20\x38\x45\x42\x45\x45\x46\x46\x39\x20\x31\x37\x42\x37\x42\x45\x34\x33\x20\x36\x30\x42\x30\x38\x45\x44\x35\x20\x44\x36\x44\x36\x41\x33\x45\x38\x20\x41\x31\x44\x31\x39\x33\x37\x45\x20\x33\x38\x44\x38\x43\x32\x43\x34\x20\x34\x46\x44\x46\x46\x32\x35\x32\x20\x44\x31\x42\x42\x36\x37\x46\x31\x20\x41\x36\x42\x43\x35\x37\x36\x37\x20\x33\x46\x42\x35\x30\x36\x44\x44\x20\x34\x38\x42\x32\x33\x36\x34\x42\x20\x44\x38\x30\x44\x32\x42\x44\x41\x20\x41\x46\x30\x41\x31\x42\x34\x43\x20\x33\x36\x30\x33\x34\x41\x46\x36\x20\x34\x31\x30\x34\x37\x41\x36\x30\x20\x44\x46\x36\x30\x45\x46\x43\x33\x20\x41\x38\x36\x37\x44\x46\x35\x35\x20\x33\x31\x36\x45\x38\x45\x45\x46\x20\x34\x36\x36\x39\x42\x45\x37\x39\x20\x43\x42\x36\x31\x42\x33\x38\x43\x20\x42\x43\x36\x36\x38\x33\x31\x41\x20\x32\x35\x36\x46\x44\x32\x41\x30\x20\x35\x32\x36\x38\x45\x32\x33\x36\x20\x43\x43\x30\x43\x37\x37\x39\x35\x20\x42\x42\x30\x42\x34\x37\x30\x33\x20\x32\x32\x30\x32\x31\x36\x42\x39\x20\x35\x35\x30\x35\x32\x36\x32\x46\x20\x43\x35\x42\x41\x33\x42\x42\x45\x20\x42\x32\x42\x44\x30\x42\x32\x38\x20\x32\x42\x42\x34\x35\x41\x39\x32\x20\x35\x43\x42\x33\x36\x41\x30\x34\x20\x43\x32\x44\x37\x46\x46\x41\x37\x20\x42\x35\x44\x30\x43\x46\x33\x31\x20\x32\x43\x44\x39\x39\x45\x38\x42\x20\x35\x42\x44\x45\x41\x45\x31\x44\x20\x39\x42\x36\x34\x43\x32\x42\x30\x20\x45\x43\x36\x33\x46\x32\x32\x36\x20\x37\x35\x36\x41\x41\x33\x39\x43\x20\x30\x32\x36\x44\x39\x33\x30\x41\x20\x39\x43\x30\x39\x30\x36\x41\x39\x20\x45\x42\x30\x45\x33\x36\x33\x46\x20\x37\x32\x30\x37\x36\x37\x38\x35\x20\x30\x35\x30\x30\x35\x37\x31\x33\x20\x39\x35\x42\x46\x34\x41\x38\x32\x20\x45\x32\x42\x38\x37\x41\x31\x34\x20\x37\x42\x42\x31\x32\x42\x41\x45\x20\x30\x43\x42\x36\x31\x42\x33\x38\x20\x39\x32\x44\x32\x38\x45\x39\x42\x20\x45\x35\x44\x35\x42\x45\x30\x44\x20\x37\x43\x44\x43\x45\x46\x42\x37\x20\x30\x42\x44\x42\x44\x46\x32\x31\x20\x38\x36\x44\x33\x44\x32\x44\x34\x20\x46\x31\x44\x34\x45\x32\x34\x32\x20\x36\x38\x44\x44\x42\x33\x46\x38\x20\x31\x46\x44\x41\x38\x33\x36\x45\x20\x38\x31\x42\x45\x31\x36\x43\x44\x20\x46\x36\x42\x39\x32\x36\x35\x42\x20\x36\x46\x42\x30\x37\x37\x45\x31\x20\x31\x38\x42\x37\x34\x37\x37\x37\x20\x38\x38\x30\x38\x35\x41\x45\x36\x20\x46\x46\x30\x46\x36\x41\x37\x30\x20\x36\x36\x30\x36\x33\x42\x43\x41\x20\x31\x31\x30\x31\x30\x42\x35\x43\x20\x38\x46\x36\x35\x39\x45\x46\x46\x20\x46\x38\x36\x32\x41\x45\x36\x39\x20\x36\x31\x36\x42\x46\x46\x44\x33\x20\x31\x36\x36\x43\x43\x46\x34\x35\x20\x41\x30\x30\x41\x45\x32\x37\x38\x20\x44\x37\x30\x44\x44\x32\x45\x45\x20\x34\x45\x30\x34\x38\x33\x35\x34\x20\x33\x39\x30\x33\x42\x33\x43\x32\x20\x41\x37\x36\x37\x32\x36\x36\x31\x20\x44\x30\x36\x30\x31\x36\x46\x37\x20\x34\x39\x36\x39\x34\x37\x34\x44\x20\x33\x45\x36\x45\x37\x37\x44\x42\x20\x41\x45\x44\x31\x36\x41\x34\x41\x20\x44\x39\x44\x36\x35\x41\x44\x43\x20\x34\x30\x44\x46\x30\x42\x36\x36\x20\x33\x37\x44\x38\x33\x42\x46\x30\x20\x41\x39\x42\x43\x41\x45\x35\x33\x20\x44\x45\x42\x42\x39\x45\x43\x35\x20\x34\x37\x42\x32\x43\x46\x37\x46\x20\x33\x30\x42\x35\x46\x46\x45\x39\x20\x42\x44\x42\x44\x46\x32\x31\x43\x20\x43\x41\x42\x41\x43\x32\x38\x41\x20\x35\x33\x42\x33\x39\x33\x33\x30\x20\x32\x34\x42\x34\x41\x33\x41\x36\x20\x42\x41\x44\x30\x33\x36\x30\x35\x20\x43\x44\x44\x37\x30\x36\x39\x33\x20\x35\x34\x44\x45\x35\x37\x32\x39\x20\x32\x33\x44\x39\x36\x37\x42\x46\x20\x42\x33\x36\x36\x37\x41\x32\x45\x20\x43\x34\x36\x31\x34\x41\x42\x38\x20\x35\x44\x36\x38\x31\x42\x30\x32\x20\x32\x41\x36\x46\x32\x42\x39\x34\x20\x42\x34\x30\x42\x42\x45\x33\x37\x20\x43\x33\x30\x43\x38\x45\x41\x31\x20\x35\x41\x30\x35\x44\x46\x31\x42\x20\x32\x44\x30\x32\x45\x46\x38\x44", "\x30\x78", "\x73\x75\x62\x73\x74\x72"];
str = _0x2cb6[0] + global_win[_0x2cb6[2]][_0x2cb6[1]] + _0x2cb6[3] + global_win[_0x2cb6[2]][_0x2cb6[4]];
str = str[_0x2cb6[6]](/\r\n/g, _0x2cb6[5]);
var utftext = _0x2cb6[7];
for (var n = 0; n < str[_0x2cb6[8]]; n++) {
var c = str[_0x2cb6[9]](n);
if (c < 128) {
utftext += String[_0x2cb6[10]](c);
} else {
if ((c > 127) && (c < 2048)) {
utftext += String[_0x2cb6[10]]((c >> 6) | 192);
utftext += String[_0x2cb6[10]]((c & 63) | 128);
} else {
utftext += String[_0x2cb6[10]]((c >> 12) | 224);
utftext += String[_0x2cb6[10]](((c >> 6) & 63) | 128);
utftext += String[_0x2cb6[10]]((c & 63) | 128);
};
};
};
str = utftext;
var table = _0x2cb6[11];
var crc = 0;
var x = 0;
var y = 0;
crc = crc ^ (-1);
for (var i = 0, iTop = str[_0x2cb6[8]]; i < iTop; i++) {
y = (crc ^ str[_0x2cb6[9]](i)) & 0xFF;
x = _0x2cb6[12] + table[_0x2cb6[13]](y * 9, 8);
crc = (crc >>> 8) ^ x;
};
global_variable = crc ^ (-1);
return parseInt(global_variable);
} catch (exc) {
return 0;
}
}
function check_me_2() {
try {
var _0x3ed1 = ["", "\x70\x6C\x75\x67\x69\x6E\x73", "\x6E\x61\x76\x69\x67\x61\x74\x6F\x72", "\x6C\x65\x6E\x67\x74\x68", "\x66\x69\x6C\x65\x6E\x61\x6D\x65", "\x0A", "\x72\x65\x70\x6C\x61\x63\x65", "\x63\x68\x61\x72\x43\x6F\x64\x65\x41\x74", "\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65", "\x30\x30\x30\x30\x30\x30\x30\x30\x20\x37\x37\x30\x37\x33\x30\x39\x36\x20\x45\x45\x30\x45\x36\x31\x32\x43\x20\x39\x39\x30\x39\x35\x31\x42\x41\x20\x30\x37\x36\x44\x43\x34\x31\x39\x20\x37\x30\x36\x41\x46\x34\x38\x46\x20\x45\x39\x36\x33\x41\x35\x33\x35\x20\x39\x45\x36\x34\x39\x35\x41\x33\x20\x30\x45\x44\x42\x38\x38\x33\x32\x20\x37\x39\x44\x43\x42\x38\x41\x34\x20\x45\x30\x44\x35\x45\x39\x31\x45\x20\x39\x37\x44\x32\x44\x39\x38\x38\x20\x30\x39\x42\x36\x34\x43\x32\x42\x20\x37\x45\x42\x31\x37\x43\x42\x44\x20\x45\x37\x42\x38\x32\x44\x30\x37\x20\x39\x30\x42\x46\x31\x44\x39\x31\x20\x31\x44\x42\x37\x31\x30\x36\x34\x20\x36\x41\x42\x30\x32\x30\x46\x32\x20\x46\x33\x42\x39\x37\x31\x34\x38\x20\x38\x34\x42\x45\x34\x31\x44\x45\x20\x31\x41\x44\x41\x44\x34\x37\x44\x20\x36\x44\x44\x44\x45\x34\x45\x42\x20\x46\x34\x44\x34\x42\x35\x35\x31\x20\x38\x33\x44\x33\x38\x35\x43\x37\x20\x31\x33\x36\x43\x39\x38\x35\x36\x20\x36\x34\x36\x42\x41\x38\x43\x30\x20\x46\x44\x36\x32\x46\x39\x37\x41\x20\x38\x41\x36\x35\x43\x39\x45\x43\x20\x31\x34\x30\x31\x35\x43\x34\x46\x20\x36\x33\x30\x36\x36\x43\x44\x39\x20\x46\x41\x30\x46\x33\x44\x36\x33\x20\x38\x44\x30\x38\x30\x44\x46\x35\x20\x33\x42\x36\x45\x32\x30\x43\x38\x20\x34\x43\x36\x39\x31\x30\x35\x45\x20\x44\x35\x36\x30\x34\x31\x45\x34\x20\x41\x32\x36\x37\x37\x31\x37\x32\x20\x33\x43\x30\x33\x45\x34\x44\x31\x20\x34\x42\x30\x34\x44\x34\x34\x37\x20\x44\x32\x30\x44\x38\x35\x46\x44\x20\x41\x35\x30\x41\x42\x35\x36\x42\x20\x33\x35\x42\x35\x41\x38\x46\x41\x20\x34\x32\x42\x32\x39\x38\x36\x43\x20\x44\x42\x42\x42\x43\x39\x44\x36\x20\x41\x43\x42\x43\x46\x39\x34\x30\x20\x33\x32\x44\x38\x36\x43\x45\x33\x20\x34\x35\x44\x46\x35\x43\x37\x35\x20\x44\x43\x44\x36\x30\x44\x43\x46\x20\x41\x42\x44\x31\x33\x44\x35\x39\x20\x32\x36\x44\x39\x33\x30\x41\x43\x20\x35\x31\x44\x45\x30\x30\x33\x41\x20\x43\x38\x44\x37\x35\x31\x38\x30\x20\x42\x46\x44\x30\x36\x31\x31\x36\x20\x32\x31\x42\x34\x46\x34\x42\x35\x20\x35\x36\x42\x33\x43\x34\x32\x33\x20\x43\x46\x42\x41\x39\x35\x39\x39\x20\x42\x38\x42\x44\x41\x35\x30\x46\x20\x32\x38\x30\x32\x42\x38\x39\x45\x20\x35\x46\x30\x35\x38\x38\x30\x38\x20\x43\x36\x30\x43\x44\x39\x42\x32\x20\x42\x31\x30\x42\x45\x39\x32\x34\x20\x32\x46\x36\x46\x37\x43\x38\x37\x20\x35\x38\x36\x38\x34\x43\x31\x31\x20\x43\x31\x36\x31\x31\x44\x41\x42\x20\x42\x36\x36\x36\x32\x44\x33\x44\x20\x37\x36\x44\x43\x34\x31\x39\x30\x20\x30\x31\x44\x42\x37\x31\x30\x36\x20\x39\x38\x44\x32\x32\x30\x42\x43\x20\x45\x46\x44\x35\x31\x30\x32\x41\x20\x37\x31\x42\x31\x38\x35\x38\x39\x20\x30\x36\x42\x36\x42\x35\x31\x46\x20\x39\x46\x42\x46\x45\x34\x41\x35\x20\x45\x38\x42\x38\x44\x34\x33\x33\x20\x37\x38\x30\x37\x43\x39\x41\x32\x20\x30\x46\x30\x30\x46\x39\x33\x34\x20\x39\x36\x30\x39\x41\x38\x38\x45\x20\x45\x31\x30\x45\x39\x38\x31\x38\x20\x37\x46\x36\x41\x30\x44\x42\x42\x20\x30\x38\x36\x44\x33\x44\x32\x44\x20\x39\x31\x36\x34\x36\x43\x39\x37\x20\x45\x36\x36\x33\x35\x43\x30\x31\x20\x36\x42\x36\x42\x35\x31\x46\x34\x20\x31\x43\x36\x43\x36\x31\x36\x32\x20\x38\x35\x36\x35\x33\x30\x44\x38\x20\x46\x32\x36\x32\x30\x30\x34\x45\x20\x36\x43\x30\x36\x39\x35\x45\x44\x20\x31\x42\x30\x31\x41\x35\x37\x42\x20\x38\x32\x30\x38\x46\x34\x43\x31\x20\x46\x35\x30\x46\x43\x34\x35\x37\x20\x36\x35\x42\x30\x44\x39\x43\x36\x20\x31\x32\x42\x37\x45\x39\x35\x30\x20\x38\x42\x42\x45\x42\x38\x45\x41\x20\x46\x43\x42\x39\x38\x38\x37\x43\x20\x36\x32\x44\x44\x31\x44\x44\x46\x20\x31\x35\x44\x41\x32\x44\x34\x39\x20\x38\x43\x44\x33\x37\x43\x46\x33\x20\x46\x42\x44\x34\x34\x43\x36\x35\x20\x34\x44\x42\x32\x36\x31\x35\x38\x20\x33\x41\x42\x35\x35\x31\x43\x45\x20\x41\x33\x42\x43\x30\x30\x37\x34\x20\x44\x34\x42\x42\x33\x30\x45\x32\x20\x34\x41\x44\x46\x41\x35\x34\x31\x20\x33\x44\x44\x38\x39\x35\x44\x37\x20\x41\x34\x44\x31\x43\x34\x36\x44\x20\x44\x33\x44\x36\x46\x34\x46\x42\x20\x34\x33\x36\x39\x45\x39\x36\x41\x20\x33\x34\x36\x45\x44\x39\x46\x43\x20\x41\x44\x36\x37\x38\x38\x34\x36\x20\x44\x41\x36\x30\x42\x38\x44\x30\x20\x34\x34\x30\x34\x32\x44\x37\x33\x20\x33\x33\x30\x33\x31\x44\x45\x35\x20\x41\x41\x30\x41\x34\x43\x35\x46\x20\x44\x44\x30\x44\x37\x43\x43\x39\x20\x35\x30\x30\x35\x37\x31\x33\x43\x20\x32\x37\x30\x32\x34\x31\x41\x41\x20\x42\x45\x30\x42\x31\x30\x31\x30\x20\x43\x39\x30\x43\x32\x30\x38\x36\x20\x35\x37\x36\x38\x42\x35\x32\x35\x20\x32\x30\x36\x46\x38\x35\x42\x33\x20\x42\x39\x36\x36\x44\x34\x30\x39\x20\x43\x45\x36\x31\x45\x34\x39\x46\x20\x35\x45\x44\x45\x46\x39\x30\x45\x20\x32\x39\x44\x39\x43\x39\x39\x38\x20\x42\x30\x44\x30\x39\x38\x32\x32\x20\x43\x37\x44\x37\x41\x38\x42\x34\x20\x35\x39\x42\x33\x33\x44\x31\x37\x20\x32\x45\x42\x34\x30\x44\x38\x31\x20\x42\x37\x42\x44\x35\x43\x33\x42\x20\x43\x30\x42\x41\x36\x43\x41\x44\x20\x45\x44\x42\x38\x38\x33\x32\x30\x20\x39\x41\x42\x46\x42\x33\x42\x36\x20\x30\x33\x42\x36\x45\x32\x30\x43\x20\x37\x34\x42\x31\x44\x32\x39\x41\x20\x45\x41\x44\x35\x34\x37\x33\x39\x20\x39\x44\x44\x32\x37\x37\x41\x46\x20\x30\x34\x44\x42\x32\x36\x31\x35\x20\x37\x33\x44\x43\x31\x36\x38\x33\x20\x45\x33\x36\x33\x30\x42\x31\x32\x20\x39\x34\x36\x34\x33\x42\x38\x34\x20\x30\x44\x36\x44\x36\x41\x33\x45\x20\x37\x41\x36\x41\x35\x41\x41\x38\x20\x45\x34\x30\x45\x43\x46\x30\x42\x20\x39\x33\x30\x39\x46\x46\x39\x44\x20\x30\x41\x30\x30\x41\x45\x32\x37\x20\x37\x44\x30\x37\x39\x45\x42\x31\x20\x46\x30\x30\x46\x39\x33\x34\x34\x20\x38\x37\x30\x38\x41\x33\x44\x32\x20\x31\x45\x30\x31\x46\x32\x36\x38\x20\x36\x39\x30\x36\x43\x32\x46\x45\x20\x46\x37\x36\x32\x35\x37\x35\x44\x20\x38\x30\x36\x35\x36\x37\x43\x42\x20\x31\x39\x36\x43\x33\x36\x37\x31\x20\x36\x45\x36\x42\x30\x36\x45\x37\x20\x46\x45\x44\x34\x31\x42\x37\x36\x20\x38\x39\x44\x33\x32\x42\x45\x30\x20\x31\x30\x44\x41\x37\x41\x35\x41\x20\x36\x37\x44\x44\x34\x41\x43\x43\x20\x46\x39\x42\x39\x44\x46\x36\x46\x20\x38\x45\x42\x45\x45\x46\x46\x39\x20\x31\x37\x42\x37\x42\x45\x34\x33\x20\x36\x30\x42\x30\x38\x45\x44\x35\x20\x44\x36\x44\x36\x41\x33\x45\x38\x20\x41\x31\x44\x31\x39\x33\x37\x45\x20\x33\x38\x44\x38\x43\x32\x43\x34\x20\x34\x46\x44\x46\x46\x32\x35\x32\x20\x44\x31\x42\x42\x36\x37\x46\x31\x20\x41\x36\x42\x43\x35\x37\x36\x37\x20\x33\x46\x42\x35\x30\x36\x44\x44\x20\x34\x38\x42\x32\x33\x36\x34\x42\x20\x44\x38\x30\x44\x32\x42\x44\x41\x20\x41\x46\x30\x41\x31\x42\x34\x43\x20\x33\x36\x30\x33\x34\x41\x46\x36\x20\x34\x31\x30\x34\x37\x41\x36\x30\x20\x44\x46\x36\x30\x45\x46\x43\x33\x20\x41\x38\x36\x37\x44\x46\x35\x35\x20\x33\x31\x36\x45\x38\x45\x45\x46\x20\x34\x36\x36\x39\x42\x45\x37\x39\x20\x43\x42\x36\x31\x42\x33\x38\x43\x20\x42\x43\x36\x36\x38\x33\x31\x41\x20\x32\x35\x36\x46\x44\x32\x41\x30\x20\x35\x32\x36\x38\x45\x32\x33\x36\x20\x43\x43\x30\x43\x37\x37\x39\x35\x20\x42\x42\x30\x42\x34\x37\x30\x33\x20\x32\x32\x30\x32\x31\x36\x42\x39\x20\x35\x35\x30\x35\x32\x36\x32\x46\x20\x43\x35\x42\x41\x33\x42\x42\x45\x20\x42\x32\x42\x44\x30\x42\x32\x38\x20\x32\x42\x42\x34\x35\x41\x39\x32\x20\x35\x43\x42\x33\x36\x41\x30\x34\x20\x43\x32\x44\x37\x46\x46\x41\x37\x20\x42\x35\x44\x30\x43\x46\x33\x31\x20\x32\x43\x44\x39\x39\x45\x38\x42\x20\x35\x42\x44\x45\x41\x45\x31\x44\x20\x39\x42\x36\x34\x43\x32\x42\x30\x20\x45\x43\x36\x33\x46\x32\x32\x36\x20\x37\x35\x36\x41\x41\x33\x39\x43\x20\x30\x32\x36\x44\x39\x33\x30\x41\x20\x39\x43\x30\x39\x30\x36\x41\x39\x20\x45\x42\x30\x45\x33\x36\x33\x46\x20\x37\x32\x30\x37\x36\x37\x38\x35\x20\x30\x35\x30\x30\x35\x37\x31\x33\x20\x39\x35\x42\x46\x34\x41\x38\x32\x20\x45\x32\x42\x38\x37\x41\x31\x34\x20\x37\x42\x42\x31\x32\x42\x41\x45\x20\x30\x43\x42\x36\x31\x42\x33\x38\x20\x39\x32\x44\x32\x38\x45\x39\x42\x20\x45\x35\x44\x35\x42\x45\x30\x44\x20\x37\x43\x44\x43\x45\x46\x42\x37\x20\x30\x42\x44\x42\x44\x46\x32\x31\x20\x38\x36\x44\x33\x44\x32\x44\x34\x20\x46\x31\x44\x34\x45\x32\x34\x32\x20\x36\x38\x44\x44\x42\x33\x46\x38\x20\x31\x46\x44\x41\x38\x33\x36\x45\x20\x38\x31\x42\x45\x31\x36\x43\x44\x20\x46\x36\x42\x39\x32\x36\x35\x42\x20\x36\x46\x42\x30\x37\x37\x45\x31\x20\x31\x38\x42\x37\x34\x37\x37\x37\x20\x38\x38\x30\x38\x35\x41\x45\x36\x20\x46\x46\x30\x46\x36\x41\x37\x30\x20\x36\x36\x30\x36\x33\x42\x43\x41\x20\x31\x31\x30\x31\x30\x42\x35\x43\x20\x38\x46\x36\x35\x39\x45\x46\x46\x20\x46\x38\x36\x32\x41\x45\x36\x39\x20\x36\x31\x36\x42\x46\x46\x44\x33\x20\x31\x36\x36\x43\x43\x46\x34\x35\x20\x41\x30\x30\x41\x45\x32\x37\x38\x20\x44\x37\x30\x44\x44\x32\x45\x45\x20\x34\x45\x30\x34\x38\x33\x35\x34\x20\x33\x39\x30\x33\x42\x33\x43\x32\x20\x41\x37\x36\x37\x32\x36\x36\x31\x20\x44\x30\x36\x30\x31\x36\x46\x37\x20\x34\x39\x36\x39\x34\x37\x34\x44\x20\x33\x45\x36\x45\x37\x37\x44\x42\x20\x41\x45\x44\x31\x36\x41\x34\x41\x20\x44\x39\x44\x36\x35\x41\x44\x43\x20\x34\x30\x44\x46\x30\x42\x36\x36\x20\x33\x37\x44\x38\x33\x42\x46\x30\x20\x41\x39\x42\x43\x41\x45\x35\x33\x20\x44\x45\x42\x42\x39\x45\x43\x35\x20\x34\x37\x42\x32\x43\x46\x37\x46\x20\x33\x30\x42\x35\x46\x46\x45\x39\x20\x42\x44\x42\x44\x46\x32\x31\x43\x20\x43\x41\x42\x41\x43\x32\x38\x41\x20\x35\x33\x42\x33\x39\x33\x33\x30\x20\x32\x34\x42\x34\x41\x33\x41\x36\x20\x42\x41\x44\x30\x33\x36\x30\x35\x20\x43\x44\x44\x37\x30\x36\x39\x33\x20\x35\x34\x44\x45\x35\x37\x32\x39\x20\x32\x33\x44\x39\x36\x37\x42\x46\x20\x42\x33\x36\x36\x37\x41\x32\x45\x20\x43\x34\x36\x31\x34\x41\x42\x38\x20\x35\x44\x36\x38\x31\x42\x30\x32\x20\x32\x41\x36\x46\x32\x42\x39\x34\x20\x42\x34\x30\x42\x42\x45\x33\x37\x20\x43\x33\x30\x43\x38\x45\x41\x31\x20\x35\x41\x30\x35\x44\x46\x31\x42\x20\x32\x44\x30\x32\x45\x46\x38\x44", "\x30\x78", "\x73\x75\x62\x73\x74\x72", "\x77\x69\x64\x74\x68", "\x73\x63\x72\x65\x65\x6E"];
str = function() {
var _0x7691x1 = _0x3ed1[0];
try {
var _0x7691x2 = global_win[_0x3ed1[2]][_0x3ed1[1]];
for (var i = 0; i < _0x7691x2[_0x3ed1[3]]; i++) {
_0x7691x1 += _0x7691x2[i][_0x3ed1[4]];
};
} catch (e) {};
return _0x7691x1;
}();
str = str[_0x3ed1[6]](/\r\n/g, _0x3ed1[5]);
var utftext = _0x3ed1[0];
for (var n = 0; n < str[_0x3ed1[3]]; n++) {
var c = str[_0x3ed1[7]](n);
if (c < 128) {
utftext += String[_0x3ed1[8]](c);
} else {
if ((c > 127) && (c < 2048)) {
utftext += String[_0x3ed1[8]]((c >> 6) | 192);
utftext += String[_0x3ed1[8]]((c & 63) | 128);
} else {
utftext += String[_0x3ed1[8]]((c >> 12) | 224);
utftext += String[_0x3ed1[8]](((c >> 6) & 63) | 128);
utftext += String[_0x3ed1[8]]((c & 63) | 128);
};
};
};
str = utftext;
var table = _0x3ed1[9];
var crc = 0;
var x = 0;
var y = 0;
crc = crc ^ (-1);
for (var i = 0, iTop = str[_0x3ed1[3]]; i < iTop; i++) {
y = (crc ^ str[_0x3ed1[7]](i)) & 0xFF;
x = _0x3ed1[10] + table[_0x3ed1[11]](y * 9, 8);
crc = (crc >>> 8) ^ x;
};
crc = crc ^ (-1);
var z = 0;
for (var zz = 0; global_win[_0x3ed1[13]][_0x3ed1[12]] >= (1 << zz); zz++) {
z += ((global_win[_0x3ed1[13]][_0x3ed1[12]] & (1 << zz)) ? 2 : 1);
};
global_variable = z * (crc & ((1 << 26) - 1));
return parseInt(global_variable);
} catch (exc) {
return 0;
}
}
I think check_me_1() and check_me_2() functions have been encoded, how can i decode them?
EDIT
there is a relation between check_me_1() and check_me_2() functions!
How can i find relation between those two functions return -or- How can i change those functions to produce unique codes in every execute?

You need work :D
But for start you can do a console.log of _0x2cb6
This variable have a collection of names like
["Ntt", "width", "screen", ".", "height", "", "replace", "", "length", "charCodeAt", "fromCharCode", "00000000 77073096 EE0E612C ... 706AF48F , "0x", "substr"]
This name are using to access properties in objects.
I'm not sure that the author want that you do this work...

Related

Reverse engineer RSA algorithm used by HUAWEI Router in Java/Kotlin

I'm trying control my huawei router using its web api, but some things are RSA encrypted. I have the public key, but I am struggling to find the algorithm they use to encrypt things.
The code they use is below:
function doRSAEncrypt(encstring) {
if (encstring == '') {
return '';
}
if (typeof (g_moduleswitch.encrypt_enabled) == 'undefined' || g_moduleswitch.encrypt_enabled != 1) {
return encstring;
}
if (g_encPublickey.e == '') {
if (true == g_scarm_login) {
var pubkeyArray = getPubkey();
g_encPublickey.e = pubkeyArray[1];
g_encPublickey.n = pubkeyArray[0];
} else {
getEncpubkey();
}
}
var rsa = new RSAKey();
rsa.setPublic(g_encPublickey.n, g_encPublickey.e);
encstring = base64_encode(encstring);
var num = encstring.length / 245;
var restotal = '';
for (i = 0; i < num; i++) {
var encdata = encstring.substr(i * 245, 245);
var res = rsa.encrypt(encdata);
restotal += res;
}
if (restotal.length % 256 != 0) {
restotal = doRSAEncrypt(encstring);
}
return restotal;
}
function parseBigInt(str, r) {
return new BigInteger(str, r);
}
// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
function pkcs1pad2(s, n) {
if (n < s.length + 11) { // TODO: fix for utf-8 alert("Message too long for RSA");
return null;
}
var ba = new Array();
var i = s.length - 1;
while (i >= 0 && n > 0) {
var c = s.charCodeAt(i--);
if (c < 128) { // encode using utf-8
ba[--n] = c;
} else if ((c > 127) && (c < 2048)) {
ba[--n] = (c & 63) | 128;
ba[--n] = (c >> 6) | 192;
} else {
ba[--n] = (c & 63) | 128;
ba[--n] = ((c >> 6) & 63) | 128;
ba[--n] = (c >> 12) | 224;
}
}
ba[--n] = 0;
var rng = new SecureRandom();
var x = new Array();
while (n > 2) { // random non-zero pad
x[0] = 0;
while (x[0] == 0)
rng.nextBytes(x);
ba[--n] = x[0];
}
ba[--n] = 2;
ba[--n] = 0;
return new BigInteger(ba);
}
// "empty" RSA key constructor
function RSAKey() {
this.n = null;
this.e = 0;
this.d = null;
this.p = null;
this.q = null;
this.dmp1 = null;
this.dmq1 = null;
this.coeff = null;
}
// Set the public key fields N and e from hex strings
function RSASetPublic(N, E) {
if (N != null && E != null && N.length > 0 && E.length > 0) {
this.n = parseBigInt(N, 16);
this.e = parseInt(E, 16);
} else alert("Invalid RSA public key");
}
// Perform raw public operation on "x": return x^e (mod n)
function RSADoPublic(x) {
return x.modPowInt(this.e, this.n);
}
// Return the PKCS#1 RSA PKCS#1 RSA encryption of "text" as an even-length hex string
function RSAEncrypt(text) {
var m = pkcs1pad2(text, (this.n.bitLength() + 7) >> 3);
if (m == null)
return null;
var c = this.doPublic(m);
if (c == null)
return null;
var h = c.toString(16);
if ((h.length & 1) == 0)
return h;
else
return "0" + h;
}
// Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
function RSAEncryptB64(text) {
var h = this.encrypt(text);
if (h)
return hex2b64(h);
else
return null;
}
// protectedRSAKey.prototype.doPublic = RSADoPublic;
// publicRSAKey.prototype.setPublic = RSASetPublic;
RSAKey.prototype.encrypt = RSAEncrypt;
RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
function base64_encode(input) {
_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
}
function _utf8_encode(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
For modulus d5eeead43ba5133e06cce6703b713db54331141d2707b8701a532173904b4e3bfca4bf73cdb7c56a640319299a083c780fa39d0fdc50aca6e0ea5d39c605cf90b88b33ed71126eea437fcd383576b11276df99425807e4c43bde60fcef38a11a6cbfb327377240b42dcf9e3d3abc1f37e62ca7efebfa247e879adeea9a395ed889916e91fd83199539dd9063f6fc306b106245b630f13ffea18eae7a486316b2c27b551214fd202993581276dfc407047f3f2da3e44161590b4cf5e12eab81633396b0eb17e487b3a12dd4a2a87726b487309801e0984ca222706127018e917ddbad3e9d9a7107e3cb54a9dad49ae7ba77252547758c2a3cf8c2c56e17b13591
and exponent 010001 (both in hex) the string test123test yields 59a5a4fe4056612c9892ea2d5108c1e348b2fb70a85a1f0c7b112bfddf058f969e8fc5e797875f63b75eb59160c1df77c7d23dbe481905226d2001f32b4eee59ec795bd113c3606096f8cbdab8ada6d6df00f1621635b7dec60ba1814208a39a3aba2ea527bda2ea522f6b65273525e7fe03478a154904debb11b7523c50432cda45a3638a6b65f768acbcc3ef1ea5c11235e39343042ea570bf220bbad05973e5c6d58af3e64c0ad183b946252c801567fa11029bdc667d2144413a5f943813ef8daf2318079204ec615e68a871e29556cd43495e3ea0a84adbe6b2dbd7e4a8d3be78bdf64a61db1a8c1dd9683499ce9241dbd6ea5bee0325944d01a17d5199 using their encryption.
I tried various RSA modes from Bouncycastle in Java like
RSA/ECB/OAEPWITHMD5ANDMGF1PADDING
RSA/ECB/OAEPWITHSHA1ANDMGF1PADDING
RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING
RSA/ECB/OAEPWITHSHA-384ANDMGF1PADDING
RSA/ECB/OAEPWITHSHA-512ANDMGF1PADDING
RSA/ECB/OAEPWithSHA-1AndMGF1Padding
RSA/ECB/PKCS1Padding
but none of them yielded the same result as the Huawei encrypt function did
import java.io.ByteArrayOutputStream
import java.math.BigInteger
import java.security.SecureRandom
import java.util.*
import kotlin.math.ceil
import kotlin.math.min
class RSAEncrypt(modulo: String, exponent: String) {
private val modulo = BigInteger(modulo, 16)
private val exponent = BigInteger(exponent, 16)
fun doRSAEncrypt(encstring: String): String {
val base64String = Base64.getEncoder().encodeToString(encstring.toByteArray())
val num = base64String.length / 245.0
var restotal = ""
for (i in 0 until ceil(num).toInt()) {
val encryptedData = base64String.substring(i * 245, min(245, base64String.length))
val res = this.encrypt(encryptedData);
restotal += res;
}
if (restotal.length % 256 !== 0) {
restotal = doRSAEncrypt(base64String)
}
return restotal
}
private fun pkcs1pad2(s: String, n: Int): BigInteger? {
if (n < s.length + 11) { // TODO: fix for utf-8
return null
}
var n = n
val ba = MutableList(n) { 0 }
var i = s.length - 1;
while (i >= 0 && n > 0) {
val c = Character.codePointAt(s, i--)
if (c < 128) { // encode using utf-8
ba[--n] = c;
} else if ((c > 127) && (c < 2048)) {
ba[--n] = (c and 63) or 128;
ba[--n] = (c shr 6) or 192;
} else {
ba[--n] = (c and 63) or 128;
ba[--n] = ((c shr 6) and 63) or 128;
ba[--n] = (c shr 12) or 224;
}
}
ba[--n] = 0;
val rng = SecureRandom()
val x = ByteArray(n) { 0 }
while (n > 2) {
x[0] = 0;
while (x[0].toInt() == 0) {
rng.nextBytes(x)
}
ba[--n] = x[0].toInt()
}
ba[--n] = 2;
ba[--n] = 0;
val baos = ByteArrayOutputStream()
for (item in ba) {
if (item < -128 || item > 127) {
throw Exception("You code is shit")
}
baos.write(item)
}
return BigInteger(baos.toByteArray())
}
private fun doPublic(x: BigInteger): BigInteger {
return x.modPow(this.exponent, this.modulo)
}
private fun encrypt(text: String): String? {
val maxLength = (this.modulo.bitLength() + 7) shr 3
val m = pkcs1pad2(text, maxLength) ?: return null
val c = this.doPublic(m)
var h = c.toString(16);
val length = h.length;
// fix zero before result
for (i in 0 until maxLength * 2 - length) {
h = "0$h";
}
return h
}
}
Implemented in Kotlin

how to generate SHA256 with 32 bytes for a given string?

I am looking for javascript code for generating a SHA256 with 32 bytes for a given string
this code is suppose to work inside a browser so it should work without any dependencies
so far I found the following function that gives 64 bytes SHA256:
/**
* Secure Hash Algorithm (SHA256)
* http://www.webtoolkit.info/
* Original code by Angel Marin, Paul Johnston
**/
function SHA256(s){
var chrsz = 8;
var hexcase = 0;
function safe_add (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function S (X, n) { return ( X >>> n ) | (X << (32 - n)); }
function R (X, n) { return ( X >>> n ); }
function Ch(x, y, z) { return ((x & y) ^ ((~x) & z)); }
function Maj(x, y, z) { return ((x & y) ^ (x & z) ^ (y & z)); }
function Sigma0256(x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); }
function Sigma1256(x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); }
function Gamma0256(x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); }
function Gamma1256(x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); }
function core_sha256 (m, l) {
var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2);
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
var W = new Array(64);
var a, b, c, d, e, f, g, h, i, j;
var T1, T2;
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >> 9) << 4) + 15] = l;
for ( var i = 0; i<m.length; i+=16 ) {
a = HASH[0];
b = HASH[1];
c = HASH[2];
d = HASH[3];
e = HASH[4];
f = HASH[5];
g = HASH[6];
h = HASH[7];
for ( var j = 0; j<64; j++) {
if (j < 16) W[j] = m[j + i];
else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
T2 = safe_add(Sigma0256(a), Maj(a, b, c));
h = g;
g = f;
f = e;
e = safe_add(d, T1);
d = c;
c = b;
b = a;
a = safe_add(T1, T2);
}
HASH[0] = safe_add(a, HASH[0]);
HASH[1] = safe_add(b, HASH[1]);
HASH[2] = safe_add(c, HASH[2]);
HASH[3] = safe_add(d, HASH[3]);
HASH[4] = safe_add(e, HASH[4]);
HASH[5] = safe_add(f, HASH[5]);
HASH[6] = safe_add(g, HASH[6]);
HASH[7] = safe_add(h, HASH[7]);
}
return HASH;
}
function str2binb (str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz) {
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i % 32);
}
return bin;
}
function Utf8Encode(string) {
string = string.replace(/\r\n/g,'\n');
var utftext = '';
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
function binb2hex (binarray) {
var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef';
var str = '';
for(var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i % 4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((3 - i % 4)*8 )) & 0xF);
}
return str;
}
s = Utf8Encode(s);
return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
}
The result you're receiving actually are 32 bytes.
Let's have a look at an example:
console.log(SHA256("test"));
returns:
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
What we see is a string representation of 32 bytes which results in 64 bytes of individual chars. However a group of two chars is actually one byte, ranging from 0 up to 255 in decimal.
9f (hex) -> 10011111 (binary) -> 159 (decimal)
86 (hex) -> 10000110 (binary) -> 134 (decimal)
and so on.
If you want to store the 64 character sequence as 'real' 32 bytes, you need to convert pairs of two characters to an 8-bit unsigned integer value (0-255) and put those in a JavaScript typed array.
For example ...
let hexString = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
let unsignedIntegers = hexString.match(/[\dA-F]{2}/gi).map(function(s) {
return parseInt(s, 16);
});
let typedArray = new Uint8Array(unsignedIntegers);
console.log(typedArray);
... gives a 32 bytes Uint8Array.

How to decode specific characters (unicode) in Google Sheets

I have a Sheets that contains text as défi (défi) or Österreich (Östereeich).
I can decode the first by this script, on the other hand I am not able to do the second (I mean the code takes 3 bytes)
Thanks for any help!
function decode(txt){
var texte = []
for (i=0;i<txt.length;i++){
var n = txt.substring(i,i+1).charCodeAt()
if (n>127){
if ((n & 32) > 0){
//texte.push(decode_utf8(txt.substring(i,i+3))) ??
i+=2
}
else{
texte.push(decode_utf8(txt.substring(i,i+2)))
i++
}
}
else{
texte.push(txt.substring(i,i+1))
}
}
return (texte.join(''))
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
Here is a solution ... based on github
function utf8decode(utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}

Encode and decode skipping the characters

I am trying to store some strings into the database using their encoded format. But when retrieving back the string is malformed.
Here is my code example where you easily can see that String passed to encode is not the same as after decode. Why is this happening?
Is there any other library which can help me encoding and decoding? Any suggestion on the same will be helpful.
var Base64 = {
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode : function(e) {
var t = "";
var n, r, i, s, o, u, a;
var f = 0;
e = Base64._utf8_encode(e);
while (f < e.length) {
n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o)
+ this._keyStr.charAt(u) + this._keyStr.charAt(a)
}
return t
},
decode : function(e) {
var t = "";
var n, r, i;
var s, o, u, a;
var f = 0;
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
while (f < e.length) {
s = this._keyStr.indexOf(e.charAt(f++));
o = this._keyStr.indexOf(e.charAt(f++));
u = this._keyStr.indexOf(e.charAt(f++));
a = this._keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n);
if (u != 64) {
t = t + String.fromCharCode(r)
}
if (a != 64) {
t = t + String.fromCharCode(i)
}
}
t = Base64._utf8_decode(t);
return t
},
_utf8_encode : function(e) {
e = e.replace(/rn/g, "n");
var t = "";
for (var n = 0; n < e.length; n++) {
var r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128)
} else {
t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128)
}
}
return t
},
_utf8_decode : function(e) {
var t = "";
var n = 0;
var r = c1 = c2 = 0;
while (n < e.length) {
r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r);
n++
} else if (r > 191 && r < 224) {
c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2
} else {
c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3
& 63);
n += 3
}
}
return t
}
}
var str = "background:url(/drona-courses/player_assets/skin_0/DRONA_default_skinRightCorner.png) ;"
var encoded = Base64.encode(str);
//console.log(encoded);
var decoded = Base64.decode(encoded);
console.log(str,"......Input");
console.log(decoded,".....Output");
Base64 Encoding in common browsers
In JavaScript there are two functions respectively for decoding and encoding base64 strings:
atob()
btoa()
The atob() function decodes a string of data which has been encoded using base-64 encoding. Conversely, the btoa() function creates a base-64 encoded ASCII string from a "string" of binary data.
Use atob and btoa.
const foo = "bar"
const encodedFoo = btoa(foo)
const decodedFoo = atob(encodedFoo)
console.log(encodedFoo)
console.log(decodedFoo)
You can read more about it here.

Failed to decode base64 in javascript

I receive an id_token as part of my current href. It is encoded in base64. I try to decode it using atob(extractedIdToken), but get the following error:
Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded
When I copy and paste the extracted id_token in my code and go to an online decoding site, it decodes correctly. Do you have suggestion?
I always use this to decode and encode in Base64, try it
var Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function (e) {
var t = "";
var n, r, i, s, o, u, a;
var f = 0;
e = Base64._utf8_encode(e);
while (f < e.length) {
n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
}
return t
},
decode: function (e) {
var t = "";
var n, r, i;
var s, o, u, a;
var f = 0;
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
while (f < e.length) {
s = this._keyStr.indexOf(e.charAt(f++));
o = this._keyStr.indexOf(e.charAt(f++));
u = this._keyStr.indexOf(e.charAt(f++));
a = this._keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n);
if (u != 64) {
t = t + String.fromCharCode(r)
}
if (a != 64) {
t = t + String.fromCharCode(i)
}
}
t = Base64._utf8_decode(t);
return t
},
_utf8_encode: function (e) {
e = e.replace(/rn/g, "n");
var t = "";
for (var n = 0; n < e.length; n++) {
var r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128)
} else {
t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128)
}
}
return t
},
_utf8_decode: function (e) {
var t = "";
var n = 0;
var r = c1 = c2 = 0;
while (n < e.length) {
r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r);
n++
} else if (r > 191 && r < 224) {
c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2
} else {
c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
n += 3
}
}
return t
}
};
Thanks all for your answers. I ended up moving the Base64 decoding process to the Java backend using the java package: java.util.Base64.
In my case, the issue was that the encoded string should have been treated as segments. Just separate the code with the separator "." or whatever your case is. Then, decode only the different parts separately. This resolved my issue and I was able to decode using window.atob(). Hope this helps.

Categories