function strToLongs($s) { $l = array(); // pad $s to some multiple of 4 $s = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY); while (count($s) % 4 != 0) { $s[] = ' '; } for ($i = 0; $i < ceil(count($s) / 4); $i++) { $l[$i] = ord($s[$i * 4]) + (ord($s[$i * 4 + 1]) << 8) + (ord($s[$i * 4 + 2]) << 16) + (ord($s[$i * 4 + 3]) << 24); } return $l; } // xor all the bytes with a random key $key = rand(21474836, 2126008810); $js = strToLongs($js); for ($i = 0; $i < count($js); $i++) { $js[$i] = $js[$i] ^ $key; } // libs function encapsulation $libs_name = hashcash_random_string(rand(6, 18), $expired); $expired[] = $libs_name; $libs = "function {$libs_name}(){"; // write bytes to javascript, xor with key $data_name = hashcash_random_string(rand(6, 18), $expired); $expired[] = $data_name; $libs .= "var {$data_name} = new Array(" . count($js) . "); "; for ($i = 0; $i < count($js); $i++) { $libs .= $data_name . '[' . $i . '] = ' . $js[$i] . ' ^ ' . $key . '; '; } // convert bytes back to string
/** * Taken from WP-HashCash */ protected function get_jscomputation($funcname, $val) { $js = 'function wphc_compute(){'; switch (rand(0, 3)) { /* Addition of n times of field value / n, + modulus: Time guarantee: 100 iterations or less */ case 0: $inc = rand($val / 100, $val - 1); $n = floor($val / $inc); $r = $val % $inc; $js .= "var wphc_eax = {$inc}; "; for ($i = 0; $i < $n - 1; $i++) { $js .= "wphc_eax += {$inc}; "; } $js .= "wphc_eax += {$r}; "; $js .= 'return wphc_eax; '; break; /* Conversion from binary: Time guarantee: log(n) iterations or less */ /* Conversion from binary: Time guarantee: log(n) iterations or less */ case 1: $binval = strrev(base_convert($val, 10, 2)); $js .= "var wphc_eax = \"{$binval}\"; "; $js .= 'var wphc_ebx = 0; '; $js .= 'var wphc_ecx = 0; '; $js .= 'while(wphc_ecx < wphc_eax.length){ '; $js .= 'if(wphc_eax.charAt(wphc_ecx) == "1") { '; $js .= 'wphc_ebx += Math.pow(2, wphc_ecx); '; $js .= '} '; $js .= 'wphc_ecx++; '; $js .= '} '; $js .= 'return wphc_ebx;'; break; /* Multiplication of square roots: Time guarantee: constant time */ /* Multiplication of square roots: Time guarantee: constant time */ case 2: $sqrt = floor(sqrt($val)); $r = $val - $sqrt * $sqrt; $js .= "return {$sqrt} * {$sqrt} + {$r}; "; break; /* Sum of random numbers to the final value: Time guarantee: log(n) expected value */ /* Sum of random numbers to the final value: Time guarantee: log(n) expected value */ case 3: $js .= 'return '; $i = 0; while ($val > 0) { if ($i++ > 0) { $js .= '+'; } $temp = rand(1, $val); $val -= $temp; $js .= $temp; } $js .= ';'; break; } $js .= '} wphc_compute();'; // pack bytes if (!function_exists('strToLongs')) { function strToLongs($s) { $l = array(); // pad $s to some multiple of 4 $s = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY); while (count($s) % 4 != 0) { $s[] = ' '; } for ($i = 0; $i < ceil(count($s) / 4); $i++) { $l[$i] = ord($s[$i * 4]) + (ord($s[$i * 4 + 1]) << 8) + (ord($s[$i * 4 + 2]) << 16) + (ord($s[$i * 4 + 3]) << 24); } return $l; } } // xor all the bytes with a random key $key = rand(21474836, 2126008810); $js = strToLongs($js); for ($i = 0; $i < count($js); $i++) { $js[$i] = $js[$i] ^ $key; } // libs function encapsulation $libs = "function " . $funcname . "(){\n"; // write bytes to javascript, xor with key $libs .= "\tvar wphc_data = [" . join(',', $js) . "]; \n"; // do the xor with key $libs .= "\n\tfor (var i=0; i<wphc_data.length; i++){\n"; $libs .= "\t\twphc_data[i]=wphc_data[i]^{$key};\n"; $libs .= "\t}\n"; // convert bytes back to string $libs .= "\n\tvar a = new Array(wphc_data.length); \n"; $libs .= "\tfor (var i=0; i<wphc_data.length; i++) { \n"; $libs .= "\t\ta[i] = String.fromCharCode(wphc_data[i] & 0xFF, wphc_data[i]>>>8 & 0xFF, "; $libs .= "wphc_data[i]>>>16 & 0xFF, wphc_data[i]>>>24 & 0xFF);\n"; $libs .= "\t}\n"; $libs .= "\n\treturn eval(a.join('')); \n"; // call libs function $libs .= "}"; // return code return $libs; }