Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1694 ddelon 1
<?php
2
 
3
require_once(realpath(dirname(__FILE__) . '/') . '/secret/wp-hashcash.lib');
4
 
5
header("Pragma: no-cache");
6
header("Expires: 0");
7
header("Cache-Control: no-store, no-cache, must-revalidate");
8
header("Cache-Control: post-check=0, pre-check=0", false);
9
$expired = array();
10
 
11
$function_name = hashcash_random_string(rand(6,18));
12
$expired [] = $function_name;
13
 
14
$js = "function $function_name (){";
15
 
16
$type = rand(0, 3) * 0;
17
switch($type){
18
	/* Addition of n times of field value / n, + modulus:
19
	Time guarantee:  100 iterations or less */
20
	case 0:
21
		$eax = hashcash_random_string(rand(8,10), $expired);
22
		$expired [] = $eax;
23
 
24
		$val = hashcash_field_value();
25
		$inc = rand($val / 100, $val - 1);
26
		$n = floor($val / $inc);
27
		$r = $val % $inc;
28
 
29
		$js .= "var $eax = $inc; ";
30
		for($i = 0; $i < $n - 1; $i++){
31
			$js .= "$eax += $inc; ";
32
		}
33
 
34
		$js .= "$eax += $r; ";
35
		$js .= "return $eax; ";
36
	break;
37
 
38
	/* Conversion from binary:
39
	Time guarantee:  log(n) iterations or less */
40
	case 1:
41
		$eax = hashcash_random_string(rand(8,10), $expired);
42
		$expired [] = $eax;
43
 
44
		$ebx = hashcash_random_string(rand(8,10), $expired);
45
		$expired [] = $ebx;
46
 
47
		$ecx = hashcash_random_string(rand(8,10), $expired);
48
		$expired [] = $ecx;
49
 
50
		$val = hashcash_field_value();
51
		$binval = strrev(base_convert($val, 10, 2));
52
			$js .= "var $eax = \"$binval\"; ";
53
		$js .= "var $ebx = 0; ";
54
		$js .= "var $ecx = 0; ";
55
		$js .= "while($ecx < $eax.length){ ";
56
		$js .= "if($eax.charAt($ecx) == \"1\") { ";
57
		$js .= "$ebx += Math.pow(2, $ecx); ";
58
		$js .= "} ";
59
		$js .= "$ecx++; ";
60
		$js .= "} ";
61
		$js .= "return $ebx; ";
62
 
63
	break;
64
 
65
	/* Multiplication of square roots:
66
	Time guarantee:  constant time */
67
	case 2:
68
		$val = hashcash_field_value();
69
		$sqrt = floor(sqrt($val));
70
		$r = $val - ($sqrt * $sqrt);
71
		$js .= "return $sqrt * $sqrt + $r; ";
72
	break;
73
 
74
	/* Sum of random numbers to the final value:
75
	Time guarantee:  log(n) expected value */
76
	case 3:
77
		$val = hashcash_field_value();
78
		$js .= "return ";
79
 
80
		$i = 0;
81
		while($val > 0){
82
			if($i++ > 0)
83
				$js .= "+";
84
 
85
			$temp = rand(1, $val);
86
			$val -= $temp;
87
			$js .= $temp;
88
		}
89
 
90
		$js .= ";";
91
	break;
92
}
93
 
94
$js .= "} $function_name ();";
95
 
96
// pack bytes
97
function strToLongs($s) {
98
	$l = array();
99
 
100
	// pad $s to some multiple of 4
101
	$s = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
102
 
103
	while(count($s) % 4 != 0){
104
		$s [] = ' ';
105
	}
106
 
107
	for ($i = 0; $i < ceil(count($s)/4); $i++) {
108
		$l[$i] = ord($s[$i*4]) + (ord($s[$i*4+1]) << 8) + (ord($s[$i*4+2]) << 16) + (ord($s[$i*4+3]) << 24);
109
    	}
110
 
111
	return $l;
112
}
113
 
114
// xor all the bytes with a random key
115
$key = rand(21474836, 2126008810);
116
$js = strToLongs($js);
117
 
118
for($i = 0; $i < count($js); $i++){
119
	$js[$i] = $js[$i] ^ $key;
120
}
121
 
122
// libs function encapsulation
123
$libs_name = hashcash_random_string(rand(6,18), $expired);
124
$expired [] = $libs_name;
125
 
126
$libs = "function $libs_name(){";
127
 
128
// write bytes to javascript, xor with key
129
$data_name = hashcash_random_string(rand(6,18), $expired);
130
$expired [] = $data_name;
131
 
132
$libs .= "var $data_name = new Array(" . count($js) . "); ";
133
for($i = 0; $i < count($js); $i++){
134
	$libs .= $data_name . '[' . $i . '] = ' . $js[$i] . ' ^ ' . $key .'; ';
135
}
136
 
137
// convert bytes back to string
138
$libs .= " var a = new Array($data_name.length); ";
139
$libs .= "for (var i=0; i<" . $data_name . ".length; i++) { ";
140
$libs .= 'a[i] = String.fromCharCode(' . $data_name .'[i] & 0xFF, ' . $data_name . '[i]>>>8 & 0xFF, ';
141
$libs .= $data_name . '[i]>>>16 & 0xFF, ' . $data_name . '[i]>>>24 & 0xFF); } ';
142
$libs .= "return eval(a.join('')); ";
143
 
144
// call libs function
145
$libs .= "} $libs_name();";
146
 
147
// return code
148
echo $libs;
149
?>