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 UniqID Random Number Source |
14
|
|
|
* |
15
|
|
|
* This uses the internal `uniqid()` function to generate low strength random |
16
|
|
|
* numbers. |
17
|
|
|
* |
18
|
|
|
* PHP version 5.3 |
19
|
|
|
* |
20
|
|
|
* @category PHPCryptLib |
21
|
|
|
* @package Random |
22
|
|
|
* @subpackage Source |
23
|
|
|
* |
24
|
|
|
* @author Anthony Ferrara <[email protected]> |
25
|
|
|
* @copyright 2011 The Authors |
26
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
27
|
|
|
* |
28
|
|
|
* @version Build @@version@@ |
29
|
|
|
*/ |
30
|
|
|
namespace RandomLib\Source; |
31
|
|
|
|
32
|
|
|
use SecurityLib\Strength; |
33
|
|
|
use SecurityLib\Util; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The UniqID Random Number Source |
37
|
|
|
* |
38
|
|
|
* This uses the internal `uniqid()` function to generate low strength random |
39
|
|
|
* numbers. |
40
|
|
|
* |
41
|
|
|
* @category PHPCryptLib |
42
|
|
|
* @package Random |
43
|
|
|
* @subpackage Source |
44
|
|
|
* |
45
|
|
|
* @author Anthony Ferrara <[email protected]> |
46
|
|
|
* @codeCoverageIgnore |
47
|
|
|
*/ |
48
|
|
|
class UniqID extends \RandomLib\AbstractSource |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Return an instance of Strength indicating the strength of the source |
53
|
|
|
* |
54
|
|
|
* @return \SecurityLib\Strength An instance of one of the strength classes |
55
|
|
|
*/ |
56
|
|
|
public static function getStrength() |
57
|
|
|
{ |
58
|
|
|
return new Strength(Strength::LOW); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generate a random string of the specified size |
63
|
|
|
* |
64
|
|
|
* @param int $size The size of the requested random string |
65
|
|
|
* |
66
|
|
|
* @return string A string of the requested size |
67
|
|
|
*/ |
68
|
|
|
public function generate($size) |
69
|
|
|
{ |
70
|
|
|
$result = ''; |
71
|
|
|
while (Util::safeStrlen($result) < $size) { |
72
|
|
|
$result = uniqid($result, true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return Util::safeSubstr($result, 0, $size); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|