|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The RandomLib library for securely generating random numbers and strings in PHP |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anthony Ferrara <[email protected]> |
|
7
|
|
|
* @copyright 2011 The Authors |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
9
|
|
|
* @version Build @@version@@ |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The URandom Random Number Source |
|
14
|
|
|
* |
|
15
|
|
|
* This uses the *nix /dev/urandom device to generate medium strength numbers |
|
16
|
|
|
* |
|
17
|
|
|
* PHP version 5.3 |
|
18
|
|
|
* |
|
19
|
|
|
* @category PHPCryptLib |
|
20
|
|
|
* @package Random |
|
21
|
|
|
* @subpackage Source |
|
22
|
|
|
* |
|
23
|
|
|
* @author Anthony Ferrara <[email protected]> |
|
24
|
|
|
* @copyright 2011 The Authors |
|
25
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
26
|
|
|
* |
|
27
|
|
|
* @version Build @@version@@ |
|
28
|
|
|
*/ |
|
29
|
|
|
namespace RandomLib\Source; |
|
30
|
|
|
|
|
31
|
|
|
use SecurityLib\Strength; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The URandom Random Number Source |
|
35
|
|
|
* |
|
36
|
|
|
* This uses the *nix /dev/urandom device to generate medium strength numbers |
|
37
|
|
|
* |
|
38
|
|
|
* @category PHPCryptLib |
|
39
|
|
|
* @package Random |
|
40
|
|
|
* @subpackage Source |
|
41
|
|
|
* |
|
42
|
|
|
* @author Anthony Ferrara <[email protected]> |
|
43
|
|
|
* @codeCoverageIgnore |
|
44
|
|
|
*/ |
|
45
|
|
|
class URandom extends \RandomLib\AbstractSource |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string The file to read from |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static $file = '/dev/urandom'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return an instance of Strength indicating the strength of the source |
|
55
|
|
|
* |
|
56
|
|
|
* @return \SecurityLib\Strength An instance of one of the strength classes |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getStrength() |
|
59
|
|
|
{ |
|
60
|
|
|
return new Strength(Strength::MEDIUM); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* If the source is currently available. |
|
65
|
|
|
* Reasons might be because the library is not installed |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function isSupported() |
|
70
|
|
|
{ |
|
71
|
|
|
return @file_exists(static::$file); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generate a random string of the specified size |
|
76
|
|
|
* |
|
77
|
|
|
* @param int $size The size of the requested random string |
|
78
|
|
|
* |
|
79
|
|
|
* @return string A string of the requested size |
|
80
|
|
|
*/ |
|
81
|
|
|
public function generate($size) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($size == 0) { |
|
84
|
|
|
return static::emptyValue($size); |
|
85
|
|
|
} |
|
86
|
|
|
$file = fopen(static::$file, 'rb'); |
|
87
|
|
|
if (!$file) { |
|
88
|
|
|
return static::emptyValue($size); |
|
89
|
|
|
} |
|
90
|
|
|
if (function_exists('stream_set_read_buffer')) { |
|
91
|
|
|
stream_set_read_buffer($file, 0); |
|
92
|
|
|
} |
|
93
|
|
|
$result = fread($file, $size); |
|
94
|
|
|
fclose($file); |
|
95
|
|
|
|
|
96
|
|
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|