PageRenderTime 1ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/PasswordLib/Random/Source/UniqID.php

http://github.com/ircmaxell/PHP-PasswordLib
PHP | 61 lines | 15 code | 6 blank | 40 comment | 1 complexity | 3bfdaf56b580ebd09bcd2f7c149bc183 MD5 | raw file
 1<?php
 2/**
 3 * The UniqID Random Number Source
 4 *
 5 * This uses the internal `uniqid()` function to generate low strength random
 6 * numbers.
 7 *
 8 * PHP version 5.3
 9 *
10 * @category   PHPPasswordLib
11 * @package    Random
12 * @subpackage Source
13 * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
14 * @copyright  2011 The Authors
15 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
16 * @version    Build @@version@@
17 */
18
19namespace PasswordLib\Random\Source;
20
21use PasswordLib\Core\Strength;
22
23/**
24 * The UniqID Random Number Source
25 *
26 * This uses the internal `uniqid()` function to generate low strength random
27 * numbers.
28 *
29 * @category   PHPPasswordLib
30 * @package    Random
31 * @subpackage Source
32 * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
33 * @codeCoverageIgnore
34 */
35class UniqID implements \PasswordLib\Random\Source {
36
37    /**
38     * Return an instance of Strength indicating the strength of the source
39     *
40     * @return Strength An instance of one of the strength classes
41     */
42    public static function getStrength() {
43        return new Strength(Strength::LOW);
44    }
45
46    /**
47     * Generate a random string of the specified size
48     *
49     * @param int $size The size of the requested random string
50     *
51     * @return string A string of the requested size
52     */
53    public function generate($size) {
54        $result = '';
55        while (strlen($result) < $size) {
56            $result = uniqid($result, true);
57        }
58        return substr($result, 0, $size);
59    }
60
61}