Tiqr_Random::randomBytes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
/**
4
 * This file is part of the tiqr project.
5
 * 
6
 * The tiqr project aims to provide an open implementation for 
7
 * authentication using mobile devices. It was initiated by 
8
 * SURFnet and developed by Egeniq.
9
 *
10
 * More information: http://www.tiqr.org
11
 *
12
 * @author Ivo Jansch <[email protected]>
13
 * 
14
 * @package tiqr
15
 *
16
 * @license New BSD License - See LICENSE file for details.
17
 *
18
 * @copyright (C) 2010-2011 SURFnet BV
19
 */
20
21
22
/**
23
 * A class implementing secure random number generation.
24
 *
25
 * @author ivo
26
 *
27
 */
28
class Tiqr_Random
29
{
30
    /**
31
     * Generate $length cryptographically secure pseudo-random bytes
32
     * Throws when requested number of bytes cannot be generated
33
     *
34
     * Note that this function generates random BYTES, it uses the string type as an array of bytes with values
35
     * from 0 to 255.
36
     *
37
     * @param int $length the number of bytes to generate.
38
     * @return string containing $length cryptographically secure pseudo-random bytes
39
     *
40
     * @throws Exception, Error, TypeError
41
     */
42 34
    public static function randomBytes(int $length): string
43
    {
44
        // Get $length cryptographically secure pseudo-random bytes
45 34
        $rnd=\random_bytes($length);
46
47 34
        if (strlen($rnd) !== $length) {
48
            throw new Exception("random_bytes did not return the requested number of bytes");
49
        }
50
51 34
        return $rnd;
52
    }
53
54
}