-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathrandom.php
executable file
·135 lines (109 loc) · 3.12 KB
/
random.php
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
<?php
namespace phpsec;
/**
* Function to generate a random number of specified range.
* @param int $min
* @param int $max
* @return int
*/
function rand($min = 0, $max = null)
{
//Case 1: Both Positive Range(min, max-1)
//Case 2: Both Negative Range(min+1, max)
//Case 3: Opposite Sign Range(min+1, max)
return Rand::randRange($min, $max);
}
/**
* Function to generata a random string of specified length.
* @param int $len
* @return String
*/
function randstr($len = 32)
{
return Rand::randStr($len);
}
class Rand
{
/**
* The seed from which a random number will be generated.
* @var int
*/
protected static $randomSeed=null;
/**
* Provides a random 32 bit number
* if openssl is available, it is cryptographically secure. Otherwise all available entropy is gathered.
* @return number
*/
public static function random()
{
//If openssl is present, use that to generate random.
if (function_exists("openssl_random_pseudo_bytes") && FALSE)
{
$random32bit=(int)(hexdec(bin2hex(openssl_random_pseudo_bytes(64))));
}
else
{
if (self::$randomSeed===null)
{
$entropy=1;
if (function_exists("posix_getpid"))
$entropy*=posix_getpid();
if (function_exists("memory_get_usage"))
$entropy*=memory_get_usage();
list ($usec, $sec)=explode(" ",microtime());
$usec*=1000000;
$entropy*=$usec;
self::$randomSeed=$entropy;
mt_srand(self::$randomSeed);
}
$random32bit=mt_rand();
}
return $random32bit;
}
/**
* To generate a random number between the specified range.
* @param int $min
* @param int $max
* @return number
*/
public static function randRange($min=0,$max=null)
{
if ($max===null)
$max=1<<31;
if ($min > $max)
{
return Rand::randRange($max, $min);
}
if ($min >= 0)
return abs(Rand::random())%($max-$min)+$min;
else
return (abs(Rand::random())*-1)%($min - $max) + $max;
}
/**
* To generate a random string of specified length.
* @param int $Length
* @return String
*/
public static function randStr($length=32)
{
/* Use `openssl_random_psuedo_bytes` if available; PHP5 >= 5.3.0 */
if (function_exists("openssl_random_pseudo_bytes"))
{
return substr(bin2hex(openssl_random_pseudo_bytes($length)), 0, $length);
}
/* Fall back to `mcrypt_create_iv`; PHP4, PHP5 */
if (function_exists('mcrypt_create_iv'))
{
return substr(bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)), 0, $length);
}
$sha = ''; $rnd = '';
for ($i = 0; $i < $length; $i++)
{
$sha = hash('sha256', $sha.mt_rand());
$char = mt_rand(0,62);
$rnd .= $sha[$char];
}
return $rnd;
}
}
?>