Random   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 9 2
A auth() 0 18 3
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP;
13
14
/**
15
 * Pseudo random helpers.
16
 */
17
class Random
18
{
19
    /**
20
     * Not so secure, but good enough for client transaction ids.
21
     *
22
     * @param int    $max_length
23
     * @param string $prefix
24
     */
25
    public static function id($max_length = 64, $prefix = '')
26
    {
27
        $prefix = (string) $prefix;
28
        if ($prefix !== '') {
29
            $prefix .= '-';
30
        }
31
32
        return substr(uniqid($prefix), 0, $max_length);
33
    }
34
35
    /**
36
     * Generate random auth key.
37
     *
38
     * @todo this should be based on templates according to registry requirements!
39
     *
40
     * @param int $len
41
     *
42
     * @return string
43
     */
44
    public static function auth($len)
45
    {
46
        // All code bellow does the same - generate as safe as possible random string
47
        // no need for full coverage test ;)
48
        // @codeCoverageIgnoreStart
49
        if (function_exists('random_bytes')) {
50
            $randomBytes = random_bytes($len);
51
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
52
            $randomBytes = openssl_random_pseudo_bytes($len);
53
        } else {
54
            $randomBytes = mcrypt_create_iv($len, MCRYPT_DEV_URANDOM);
55
        }
56
        // @codeCoverageIgnoreEnd
57
58
        $randomBytes = base64_encode($randomBytes);
59
60
        return substr(rtrim($randomBytes, '='), 0, $len);
61
    }
62
}
63