Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * The Rand Random Number Source | |
4 | * | |
5 | * This source generates low strength random numbers by using the internal | |
6 | * rand() function. By itself it is quite weak. However when combined with | |
7 | * other sources it does provide significant benefit. | |
8 | * | |
9 | * PHP version 5.3 | |
10 | * | |
11 | * @category PHPCryptLib | |
12 | * @package Random | |
13 | * @subpackage Source | |
14 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
15 | * @copyright 2011 The Authors | |
16 | * @license http://www.opensource.org/licenses/mit-license.html MIT License | |
17 | * @version Build @@version@@ | |
18 | */ | |
19 | ||
20 | namespace CryptLib\Random\Source; | |
21 | ||
22 | use CryptLib\Core\Strength; | |
23 | ||
24 | /** | |
25 | * The Rand Random Number Source | |
26 | * | |
27 | * This source generates low strength random numbers by using the internal | |
28 | * rand() function. By itself it is quite weak. However when combined with | |
29 | * other sources it does provide significant benefit. | |
30 | * | |
31 | * @category PHPCryptLib | |
32 | * @package Random | |
33 | * @subpackage Source | |
34 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
35 | * @codeCoverageIgnore | |
36 | */ | |
37 | class Rand implements \CryptLib\Random\Source { | |
38 | ||
39 | /** | |
40 | * Return an instance of Strength indicating the strength of the source | |
41 | * | |
42 | * @return Strength An instance of one of the strength classes | |
43 | */ | |
44 | public static function getStrength() { | |
45 | // Detect if Suhosin Hardened PHP patch is applied | |
46 | if (defined('S_ALL')) { | |
47 | return new Strength(Strength::LOW); | |
48 | } else { | |
49 | return new Strength(Strength::VERYLOW); | |
50 | } | |
51 | } | |
52 | ||
53 | /** | |
54 | * Generate a random string of the specified size | |
55 | * | |
56 | * @param int $size The size of the requested random string | |
57 | * | |
58 | * @return string A string of the requested size | |
59 | */ | |
60 | public function generate($size) { | |
61 | $result = ''; | |
62 | for ($i = 0; $i < $size; $i++) { | |
63 | $result .= chr((rand() ^ rand()) % 256); | |
64 | } | |
65 | return $result; | |
66 | } | |
67 | ||
68 | } |