Completed
Push — master ( 4d4232...ffcfa4 )
by Anthony
14:02 queued 11:53
created

OpenSSL::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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 <[email protected]>
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 RandomLib\Source;
19
20
use SecurityLib\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 <[email protected]>
31
 * @codeCoverageIgnore
32
 */
33
class OpenSSL extends \RandomLib\AbstractSource {
34
35
    /**
36
     * Return an instance of Strength indicating the strength of the source
37
     *
38
     * @return \SecurityLib\Strength An instance of one of the strength classes
39
     */
40
    public static function getStrength() {
41
        /**
42
         * Prior to PHP 5.6.10 (see https://bugs.php.net/bug.php?id=70014) the "openssl_random_pseudo_bytes"
43
         * was using "RAND_pseudo_bytes" (predictable) instead of "RAND_bytes" (unpredictable).
44
         */
45
        if (PHP_VERSION_ID < 50610) {
46
            return new Strength(Strength::MEDIUM);
47
        }
48
49
        return new Strength(Strength::HIGH);
50
    }
51
52
    /**
53
     * If the source is currently available.
54
     * Reasons might be because the library is not installed
55
     *
56
     * @return boolean
57
     */
58
    public static function isSupported() {
59
        return function_exists('openssl_random_pseudo_bytes');
60
    }
61
62
    /**
63
     * Generate a random string of the specified size
64
     *
65
     * @param int $size The size of the requested random string
66
     *
67
     * @return string A string of the requested size
68
     */
69
    public function generate($size) {
70
        if ($size < 1) {
71
            return str_repeat(chr(0), $size);
72
        }
73
        /**
74
         * Note, normally we would check the return of of $crypto_strong to
75
         * ensure that we generated a good random string.  However, since we're
76
         * using this as one part of many sources a low strength random number
77
         * shouldn't be much of an issue.
78
         */
79
        return openssl_random_pseudo_bytes($size);
80
    }
81
82
}
83