Passed
Branch develop (e3abb6)
by Pieter van der
06:20
created

Tiqr_Random::randomHexString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
     * @param int $length the number of bytes to generate.
35
     * @return string containing $length cryptographically secure pseudo-random bytes
36
     *
37
     * @throws Exception, Error, TypeError
38
     */
39 5
    public static function randomBytes($length)
40
    {
41
        // Get $length cryptographically secure pseudo-random bytes
42 5
        $rnd=\random_bytes($length);
43
44 5
        if (strlen($rnd) !== $length) {
45
            throw new Exception("random_bytes did not return the requested number of bytes");
46
        }
47
48 5
        return $rnd;
49
    }
50
51
}