1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
<?php
/**
* CryptUtil: A suite of wrapper utility functions for the OpenID
* library.
*
* PHP versions 4 and 5
*
* LICENSE: See the COPYING file included in this distribution.
*
* @package OpenID
* @author JanRain, Inc. <openid@janrain.com>
* @copyright 2005 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*/
/**
* Require the HMAC/SHA-1 implementation for creating such hashes.
*/
require('HMACSHA1.php');
if (!defined('Net_OpenID_RAND_SOURCE')) {
/**
* The filename for a source of random bytes. Define this yourself
* if you have a different source of randomness.
*/
define('Net_OpenID_RAND_SOURCE', '/dev/urandom');
}
/**
* An array of duplicate information for the randrange function.
*/
$Net_OpenID_CryptUtil_duplicate_cache = array();
/**
* Net_OpenID_CryptUtil houses static utility functions.
*
* @package OpenID
*/
class Net_OpenID_CryptUtil {
/**
* Get the specified number of random bytes.
*
* Attempts to use a cryptographically secure (not predictable)
* source of randomness if available. If there is no high-entropy
* randomness source available, it will fail. As a last resort,
* for non-critical systems, define
* <code>Net_OpenID_USE_INSECURE_RAND</code>, and the code will
* fall back on a pseudo-random number generator.
*
* @static
* @param int $num_bytes The length of the return value
* @return string $num_bytes random bytes
*/
function getBytes($num_bytes) {
$f = @fopen("/dev/urandom", "r");
if ($f === FALSE) {
if (!defined(Net_OpenID_USE_INSECURE_RAND)) {
trigger_error('Set Net_OpenID_USE_INSECURE_RAND to ' .
'continue with insecure random.',
E_USER_ERROR);
}
$bytes = '';
for ($i = 0; $i < $num_bytes; $i += 4) {
$bytes .= pack('L', mt_rand());
}
$bytes = substr($bytes, 0, $num_bytes);
} else {
$bytes = fread($f, $num_bytes);
fclose($f);
}
return $bytes;
}
/**
* Computes the SHA1 hash.
*
* @param string $str The input string.
* @static
* @return string The resulting SHA1 hash.
*/
function sha1($str) {
return sha1($str, true);
}
/**
* Computes an HMAC-SHA1 digest.
*/
function hmacSha1($key, $text) {
return Net_OpenID_HMACSHA1($key, $text);
}
function fromBase64($str) {
return base64_decode($str);
}
function toBase64($str) {
return base64_encode($str);
}
function longToBinary($long) {
return pack("L", $long);
}
function binaryToLong($str) {
return unpack($str, "L");
}
function base64ToLong($str) {
return Net_OpenID_CryptUtil::binaryToLong(Net_OpenID_CryptUtil::fromBase64($str));
}
function longToBase64($long) {
return Net_OpenID_CryptUtil::toBase64(Net_OpenID_CryptUtil::longToBinary($long));
}
function strxor($x, $y) {
if (strlen($x) != strlen($y)) {
return null;
}
$str = "";
for ($i = 0; $i < strlen($x); $i++) {
$str .= chr(ord($x[$i]) ^ ord($y[$i]));
}
return $str;
}
function reversed($list) {
if (is_string($list)) {
return strrev($list);
} else if (is_array($list)) {
return array_reverse($list);
} else {
return null;
}
}
function randrange($start, $stop = null, $step = 1) {
global $Net_OpenID_CryptUtil_duplicate_cache;
if ($stop == null) {
$stop = $start;
$start = 0;
}
$r = ($stop - $start);
if (array_key_exists($r, $Net_OpenID_CryptUtil_duplicate_cache)) {
list($duplicate, $nbytes) = $Net_OpenID_CryptUtil_duplicate_cache[$r];
} else {
$rbytes = Net_OpenID_CryptUtil::longToBinary($r);
if ($rbytes[0] == '\x00') {
$nbytes = strlen($rbytes) - 1;
} else {
$nbytes = strlen($rbytes);
}
$mxrand = (pow(256, $nbytes));
// If we get a number less than this, then it is in the
// duplicated range.
$duplicate = $mxrand % $r;
if (count($Net_OpenID_CryptUtil_duplicate_cache) > 10) {
$Net_OpenID_CryptUtil_duplicate_cache = array();
}
$Net_OpenID_CryptUtil_duplicate_cache[$r] = array($duplicate, $nbytes);
}
while (1) {
$bytes = '\x00' + Net_OpenID_CryptUtil::getBytes($nbytes);
$n = Net_OpenID_CryptUtil::binaryToLong($bytes);
// Keep looping if this value is in the low duplicated range
if ($n >= $duplicate) {
break;
}
}
return $start + ($n % $r) * $step;
}
/**
* Produce a string of length random bytes, chosen from chrs.
*/
function randomString($length, $chrs = null) {
if ($chrs == null) {
return getBytes($length);
} else {
$n = strlen($chrs);
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= $chrs[Net_OpenID_CryptUtil::randrange($n)];
}
return $str;
}
}
}
/**
* Net_OpenID_MathWrapper is a base class that defines the interface
* to whatever large number math library is available, if any.
*/
class Net_OpenID_MathWrapper {
var $type = 'dumb';
function random($min, $max) {
return mt_rand($min, $max);
}
function cmp($x, $y) {
if ($x > $y) {
return 1;
} else if ($x < $y) {
return -1;
} else {
return 0;
}
}
function init($number, $base = 10) {
return $number;
}
function mod($base, $modulus) {
return $base % $modulus;
}
function mul($x, $y) {
return $x * $y;
}
function div($x, $y) {
return $x / $y;
}
function powmod($base, $exponent, $modulus) {
$square = $this->mod($base, $modulus);
$result = '1';
while($this->cmp($exponent, 0) > 0) {
if ($this->mod($exponent, 2)) {
$result = $this->mod($this->mul($result, $square), $modulus);
}
$square = $this->mod($this->mul($square, $square), $modulus);
$exponent = $this->div($exponent, 2);
}
return $result;
}
}
/**
* Net_OpenID_BcMathWrapper implements the Net_OpenID_MathWrapper
* interface and wraps the functionality provided by the BCMath
* library.
*/
class Net_OpenID_BcMathWrapper extends Net_OpenID_MathWrapper {
var $type = 'bcmath';
function random($min, $max) {
return mt_rand($min, $max);
}
function cmp($x, $y) {
return bccomp($x, $y);
}
function init($number, $base = 10) {
return $number;
}
function mod($base, $modulus) {
return bcmod($base, $modulus);
}
function mul($x, $y) {
return bcmul($x, $y);
}
function div($x, $y) {
return bcdiv($x, $y);
}
}
/**
* Net_OpenID_GmpMathWrapper implements the Net_OpenID_MathWrapper
* interface and wraps the functionality provided by the GMP library.
*/
class Net_OpenID_GmpMathWrapper extends Net_OpenID_MathWrapper {
var $type = 'gmp';
function random($min, $max) {
return gmp_random($max);
}
function cmp($x, $y) {
return gmp_cmp($x, $y);
}
function init($number, $base = 10) {
return gmp_init($number, $base);
}
function mod($base, $modulus) {
return gmp_mod($base, $modulus);
}
function mul($x, $y) {
return gmp_mul($x, $y);
}
function div($x, $y) {
return gmp_div($x, $y);
}
function powmod($base, $exponent, $modulus) {
return gmp_powm($base, $exponent, $modulus);
}
}
/**
* Net_OpenID_MathLibrary implements the Singleton pattern to generate
* the appropriate Net_OpenID_MathWrapper instance for use by crypto
* code.
*/
class Net_OpenID_MathLibrary {
var $extensions = array(
array('modules' => array('gmp', 'php_gmp'),
'extension' => 'gmp',
'class' => 'Net_OpenID_GmpMathWrapper'),
array('modules' => array('bcmath', 'php_bcmath'),
'extension' => 'bcmath',
'class' => 'Net_OpenID_BcMathWrapper')
);
function &getLibWrapper() {
static $lib = null;
if (!$lib) {
$loaded = false;
foreach ($extensions as $extension) {
if ($extension['extension'] &&
extension_loaded($extension['extension'])) {
$loaded = true;
}
if (!$loaded) {
foreach ($extension['modules'] as $module) {
if (@dl($module . "." . PHP_SHLIB_SUFFIX)) {
$loaded = true;
break;
}
}
}
if ($loaded) {
$classname = $extension['class'];
$lib =& new $classname();
break;
}
}
if (!$lib) {
$lib =& new Net_OpenID_MathWrapper();
}
}
return $lib;
}
}
?>
|