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