Token::generateRandomBytes()   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 1
crap 1
1
<?php
2
3
namespace Ds\Authenticate;
4
5
/**
6
 * Class Token
7
 * @package Ds\Authenticate
8
 */
9
class Token implements TokenInterface
10
{
11
12
    /**
13
     * Create a random token.
14
     *
15
     * @param  int $length
16
     * @return string
17
     */
18 9
    public function create($length = 40)
19
    {
20 9
        $token = null;
21 9
        while ($token === null) {
22 9
            $token = $this->generateRandomBytes($length);
23
        }
24 9
        return $token;
25
    }
26
27
    /**
28
     * Generate Random byes.
29
     *
30
     * @param  $length
31
     * @return string
32
     */
33 9
    public function generateRandomBytes($length)
34
    {
35 9
        return \openssl_random_pseudo_bytes($length);
36
    }
37
38
    /**
39
     * Create a token from a string + key.
40
     *
41
     * @param  string $string
42
     * @param  string $key
43
     * @param  string $algo
44
     * @return string
45
     */
46 1
    public function createFromString($string, $key, $algo = 'sha256')
47
    {
48 1
        return \bin2hex(\hash_hmac($algo, $string, $key));
49
    }
50
}
51