summaryrefslogtreecommitdiffstats
path: root/Net/OpenID/CryptUtil.php
blob: 379f2d4aa4bdc047b342063cd26caa211bdedd06 (plain)
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
<?php

class Net_OpenID_CryptUtil {
    function _getFourBytes() {
        $x = mt_rand();
        
        $sources = array(
            time(),
            getmypid(),
            getmygid(),
            getmyuid(),
            disk_free_space(__FILE__)
            );

        foreach ($sources as $ent) {
            $x ^= $ent;
            mt_srand($x);
            $x = mt_rand();
        }
        return $x;
    }

    function getBytes($num_bytes) {
        $f = @fopen("/dev/urandom", "r");
        if ($f === FALSE) {
            $bytes = '';
            for ($i = 0; $i < $num_bytes; $i += 4) {
                $bytes .= Net_OpenID_CryptUtil::_getFourBytes();
            }
            $bytes = substr($bytes, 0, $num_bytes);
        } else {
            $bytes = fread($f, $num_bytes);
            fclose($f);
        }
        return $bytes;
    }
}

?>