| 1 | <?php |
| 2 | /** |
| 3 | * The OpenSSL Random Number Source |
| 4 | * |
| 5 | * This uses the OS's secure generator to generate high strength numbers |
| 6 | * |
| 7 | * PHP version 5.3 |
| 8 | * |
| 9 | * @category PHPCryptLib |
| 10 | * @package Random |
| 11 | * @subpackage Source |
| 12 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> |
| 13 | * @copyright 2011 The Authors |
| 14 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 15 | * @version Build @@version@@ |
| 16 | */ |
| 17 | |
| 18 | namespace CryptLib\Random\Source; |
| 19 | |
| 20 | use CryptLib\Core\Strength; |
| 21 | |
| 22 | /** |
| 23 | * The OpenSSL Random Number Source |
| 24 | * |
| 25 | * This uses the OS's secure generator to generate high strength numbers |
| 26 | * |
| 27 | * @category PHPCryptLib |
| 28 | * @package Random |
| 29 | * @subpackage Source |
| 30 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> |
| 31 | * @codeCoverageIgnore |
| 32 | */ |
| 33 | class OpenSSL implements \CryptLib\Random\Source { |
| 34 | |
| 35 | /** |
| 36 | * Return an instance of Strength indicating the strength of the source |
| 37 | * |
| 38 | * @return Strength An instance of one of the strength classes |
| 39 | */ |
| 40 | public static function getStrength() { |
| 41 | return new Strength(Strength::HIGH); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Generate a random string of the specified size |
| 46 | * |
| 47 | * @param int $size The size of the requested random string |
| 48 | * |
| 49 | * @return string A string of the requested size |
| 50 | */ |
| 51 | public function generate($size) { |
| 52 | if (!function_exists('openssl_random_pseudo_bytes') || $size < 1) { |
| 53 | return str_repeat(chr(0), $size); |
| 54 | } |
| 55 | /** |
| 56 | * Note, normally we would check the return of of $crypto_strong to |
| 57 | * ensure that we generated a good random string. However, since we're |
| 58 | * using this as one part of many sources a low strength random number |
| 59 | * shouldn't be much of an issue. |
| 60 | */ |
| 61 | return openssl_random_pseudo_bytes($size); |
| 62 | } |
| 63 | |
| 64 | } |