key::tokenGenerate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\core\keys
11
 *
12
 * @author Vince scarpa <[email protected]>
13
 *
14
 */
15
namespace responsible\core\keys;
16
17
class key
18
{
19
    /**
20
     * [accountIdGenerate Generate a user account id]
21
     * @return string
22
     */
23
    public function accountIdGenerate($length = 8)
24
    {
25
        $uq = uniqid(mt_rand(), true);
26
        $sb = substr($uq, 0, $length);
27
        $hd = hexdec($sb);
28
        return substr($hd, 0, $length);
29
    }
30
31
    /**
32
     * [apiKeyGenerate Generate an API key for the new user account]
33
     * @param  integer $length
34
     * @return string
35
     */
36
    public function apiKeyGenerate($length = 32)
37
    {
38
        $randInt = microtime().rand(1000, 9999);
39
        $messageDigest = md5($randInt);
40
        return implode(
41
            '-',
42
            str_split(substr(strtolower($messageDigest), 0, $length), 6)
0 ignored issues
show
Bug introduced by
It seems like str_split(substr(strtolo...igest), 0, $length), 6) can also be of type true; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            /** @scrutinizer ignore-type */ str_split(substr(strtolower($messageDigest), 0, $length), 6)
Loading history...
43
        );
44
    }
45
46
    /**
47
     * [secretGenerate Generate a strong secret key]
48
     * @return string
49
     */
50
    public function secretGenerate($length = 32)
51
    {
52
        $chars =
53
            'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' .
54
            '0123456789`-=!@#$%^&*()_+,./?;[]{}\|'
55
        ;
56
57
        $str = '';
58
        $max = strlen($chars) - 1;
59
60
        for ($i = 0; $i < $length; $i++) {
61
            $str .= $chars[mt_rand(0, $max)];
62
        }
63
64
        return $str;
65
    }
66
67
    /**
68
     * [tokenGenerate This is a JWT this is a pseudo token used for the leaky bucket]
69
     * @return string
70
     */
71
    public function tokenGenerate($ACCOUNT_ID, $key = '')
72
    {
73
        return hash_hmac('sha256', $ACCOUNT_ID, $key);
74
    }
75
}
76