Completed
Push — master ( 425dec...a1466a )
by Anthony
06:54
created

Sodium   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A isSupported() 0 4 1
A getStrength() 0 4 1
A generate() 0 8 3
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 libsodium Random Number Source
14
 *
15
 * This uses the libsodium secure generator to generate high strength numbers
16
 *
17
 * PHP version 5.3
18
 *
19
 * @category   PHPCryptLib
20
 * @package    Random
21
 * @subpackage Source
22
 *
23
 * @author     Anthony Ferrara <[email protected]>
24
 * @author     Ben Ramsey <[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
 * @link       https://paragonie.com/book/pecl-libsodium
31
 * @link       http://pecl.php.net/package/libsodium
32
 */
33
namespace RandomLib\Source;
34
35
use SecurityLib\Strength;
36
37
/**
38
 * The libsodium Random Number Source
39
 *
40
 * This uses the libsodium secure generator to generate high strength numbers
41
 *
42
 * @category   PHPCryptLib
43
 * @package    Random
44
 * @subpackage Source
45
 *
46
 * @author     Anthony Ferrara <[email protected]>
47
 * @author     Ben Ramsey <[email protected]>
48
 */
49
class Sodium extends \RandomLib\AbstractSource
50
{
51
52
    /**
53
     * A property that may be forcibly set to `false` in the constructor, for
54
     * the purpose of testing this source
55
     *
56
     * @var bool
57
     */
58
    private $hasLibsodium = false;
59
60
    /**
61
     * Constructs a libsodium Random Number Source
62
     *
63
     * @param bool $useLibsodium May be set to `false` to disable libsodium for
64
     *                           testing purposes
65
     */
66
    public function __construct($useLibsodium = true)
67
    {
68
        if ($useLibsodium && extension_loaded('libsodium')) {
69
            $this->hasLibsodium = true;
70
        }
71
    }
72
73
    /**
74
     * If the source is currently available.
75
     * Reasons might be because the library is not installed
76
     *
77
     * @return bool
78
     */
79
    public static function isSupported()
80
    {
81
        return function_exists('Sodium\\randombytes_buf');
82
    }
83
84
    /**
85
     * Return an instance of Strength indicating the strength of the source
86
     *
87
     * @return Strength An instance of one of the strength classes
88
     */
89
    public static function getStrength()
90
    {
91
        return new Strength(Strength::HIGH);
92
    }
93
94
    /**
95
     * Generate a random string of the specified size
96
     *
97
     * @param int $size The size of the requested random string
98
     *
99
     * @return string A string of the requested size
100
     */
101
    public function generate($size)
102
    {
103
        if (!$this->hasLibsodium || $size < 1) {
104
            return str_repeat(chr(0), $size);
105
        }
106
107
        return \Sodium\randombytes_buf($size);
108
    }
109
}
110