Passed
Branch master (776013)
by payever
03:51
created

PseudoRandomStringGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 23
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 29 7
A binToHex() 0 3 1
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\Core
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\Core;
12
13
/**
14
 * PHP version 5.4 and 7
15
 *
16
 * @package   Payever\Core
17
 * @author    Hennadii.Shymanskyi <[email protected]>
18
 * @copyright 2017-2019 payever GmbH
19
 * @license   MIT <https://opensource.org/licenses/MIT>
20
 */
21
class PseudoRandomStringGenerator
22
{
23
    protected static $fallbackKeyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
24
25
    /**
26
     * Generates a random string, using a cryptographically secure pseudo-random number generator.
27
     * Falling back to non-CSPRNG generation when no source of random available.
28
     *
29
     * @param int $length
30
     *
31
     * @return string
32
     *
33
     * @throws \Exception when CSPRNG source failed to provide random bytes
34
     */
35
    public function generate($length = 64)
36
    {
37
        $binaryString = false;
38
39
        if (function_exists('random_bytes')) {
40
            $binaryString = random_bytes($length);
41
        } elseif (function_exists('mcrypt_create_iv')) {
42
            $binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_create_iv() has been deprecated: 7.1 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

42
            $binaryString = /** @scrutinizer ignore-deprecated */ mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
44
            $binaryString = openssl_random_pseudo_bytes($length);
45
        } elseif (($stream = fopen('/dev/urandom', 'rb'))) {
46
            stream_set_read_buffer($stream, 0);
47
            $binaryString = fread($stream, $length);
48
            fclose($stream);
49
        } else {
50
            // well, we've tried
51
            $chain = '';
52
            $max = mb_strlen(static::$fallbackKeyspace, '8bit') - 1;
53
            for ($i = 0; $i < $length; ++$i) {
54
                $chain .= static::$fallbackKeyspace[rand(0, $max)];
55
            }
56
            $binaryString = hex2bin($chain);
57
        }
58
59
        if ($binaryString === false) {
60
            throw new \RuntimeException("Unable to generate random string.");
61
        }
62
63
        return $this->binToHex($binaryString, $length);
64
    }
65
66
    /**
67
     * @param $binaryData
68
     * @param $length
69
     *
70
     * @return bool|string
71
     */
72
    protected function binToHex($binaryData, $length)
73
    {
74
        return substr(bin2hex($binaryData), 0, $length);
75
    }
76
}
77