GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — 3.0.x ( 74e535...484b00 )
by Nicolas
03:47
created

Cryptography::randomBytes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package toolkit
4
 */
5
/**
6
 * Cryptography is a utility class that offers a number of general purpose cryptography-
7
 * related functions for message digestation as well as (backwards-)compatibility
8
 * checking. The message digestation algorithms are placed in the subclasses
9
 * `SHA1` and `PBKDF2`.
10
 *
11
 * @since Symphony 2.3.1
12
 * @see cryptography.SHA1
13
 * @see cryptography.PBKDF2
14
 */
15
16
class Cryptography
17
{
18
    /**
19
     * Uses an instance of `PBKDF2` to create a hash. If you require other
20
     * hashes, see the related functions of the `MD5` or `SHA1` classes
21
     *
22
     * @see cryptography.MD5#hash()
23
     * @see cryptography.SHA1#hash()
24
     * @see cryptography.PBKDF2#hash()
25
     *
26
     * @param string $input
27
     * the string to be hashed
28
     * @return string
29
     * the hashed string
30
     */
31
    public static function hash($input)
32
    {
33
        return PBKDF2::hash($input);
34
    }
35
36
    /**
37
     * Compares a given hash with a clean text password by figuring out the
38
     * algorithm that has been used and then calling the appropriate sub-class
39
     *
40
     * @see cryptography.SHA1#compare()
41
     * @see cryptography.PBKDF2#compare()
42
     *
43
     * @param string $input
44
     *  the cleartext password
45
     * @param string $hash
46
     *  the hash the password should be checked against
47
     * @param boolean $isHash
48
     * @return boolean
49
     *  the result of the comparison
50
     */
51
    public static function compare($input, $hash, $isHash = false)
52
    {
53
        $version = substr($hash, 0, 8);
54
55
        if ($isHash === true) {
56
            return $input == $hash;
57
        } elseif ($version == 'PBKDF2v1') { // salted PBKDF2
58
            return PBKDF2::compare($input, $hash);
59
        } elseif (strlen($hash) == 40) { // legacy, unsalted SHA1
60
            return SHA1::compare($input, $hash);
61
        } else { // the hash provided doesn't make any sense
62
            return false;
63
        }
64
    }
65
66
    /**
67
     * Checks if provided hash has been computed by most recent algorithm
68
     * returns true if otherwise
69
     *
70
     * @param string $hash
71
     * the hash to be checked
72
     * @return boolean
73
     * whether the hash should be re-computed
74
     */
75
    public static function requiresMigration($hash)
76
    {
77
        $version = substr($hash, 0, 8);
78
79
        if ($version == PBKDF2::PREFIX) { // salted PBKDF2, let the responsible class decide
80
            return PBKDF2::requiresMigration($hash);
81
        } else { // everything else
82
            return true;
83
        }
84
    }
85
86
    /**
87
     * Generates a salt to be used in message digestation.
88
     *
89
     * @param integer $length
90
     * the length of the salt
91
     * @return string
92
     * a hexadecimal string
93
     */
94
    public static function generateSalt($length)
95
    {
96
        mt_srand(intval(microtime(true)*100000 + memory_get_usage(true)));
97
        return substr(sha1(uniqid(mt_rand(), true)), 0, $length);
98
    }
99
100
    /**
101
     * Returns a string generated from random bytes.
102
     * It requires a minimum length of 16.
103
     * It first tries to call PHP's random_byte.
104
     * If not available, it will try openssl_random_pseudo_bytes.
105
     * If not available, it will revert to `Cryptography::generateSalt()`.
106
     *
107
     * @uses Cryptography::generateSalt()
108
     * @param integer $length
109
     *  The number of random bytes to get.
110
     *  The minimum is 16.
111
     *  Defaults to 40, which is 160 bits of entropy.
112
     * @return string
113
     * @throws Exception
114
     *  If the requested length is smaller than 16.
115
     */
116
    public static function randomBytes($length = 40)
117
    {
118
        if ($length < 16) {
119
            throw new Exception('Can not generate less than 16 random bytes');
120
        }
121
        if (function_exists('random_bytes')) {
122
            return bin2hex(random_bytes($length / 2));
123
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
124
            return bin2hex(openssl_random_pseudo_bytes($length / 2));
125
        }
126
        return self::generateSalt($length);
127
    }
128
}
129