Mcrypt::isSupported()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Riimu\Kit\SecureRandom\Generator;
4
5
/**
6
 * Generates bytes using mcrypt extension.
7
 *
8
 * Mcrypt generator creates secure random byte using the mcrypt_create_iv
9
 * function. The generator can either use /dev/urandom or /dev/random as the
10
 * randomness source for the function. Note that on windows based systems, the
11
 * function resorts to windows specific random generator.
12
 *
13
 * @author Riikka Kalliomäki <[email protected]>
14
 * @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
15
 * @license http://opensource.org/licenses/mit-license.php MIT License
16
 */
17
class Mcrypt extends AbstractGenerator
18
{
19
    /** @var int Random source for mcrypt_create_iv */
20
    private $mode;
21
22
    /**
23
     * Creates new instance of Mcrypt generator.
24
     * @param bool $urandom True to use /dev/urandom and false for /dev/random
25
     */
26 4
    public function __construct($urandom = true)
27
    {
28 4
        $this->mode = $urandom ? MCRYPT_DEV_URANDOM : MCRYPT_DEV_RANDOM;
29 4
    }
30
31 4
    public function isSupported()
32
    {
33 4
        return function_exists('mcrypt_create_iv');
34
    }
35
36 4
    protected function readBytes($count)
37
    {
38 4
        return mcrypt_create_iv($count, $this->mode);
39
    }
40
}
41