|
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
|
|
|
* `MD5`, `SHA1` and `PBKDF2`. |
|
10
|
|
|
* |
|
11
|
|
|
* @since Symphony 2.3.1 |
|
12
|
|
|
* @see cryptography.MD5 |
|
13
|
|
|
* @see cryptography.SHA1 |
|
14
|
|
|
* @see cryptography.PBKDF2 |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
class Cryptography |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Uses an instance of `PBKDF2` to create a hash. If you require other |
|
21
|
|
|
* hashes, see the related functions of the `MD5` or `SHA1` classes |
|
22
|
|
|
* |
|
23
|
|
|
* @see cryptography.MD5#hash() |
|
24
|
|
|
* @see cryptography.SHA1#hash() |
|
25
|
|
|
* @see cryptography.PBKDF2#hash() |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $input |
|
28
|
|
|
* the string to be hashed |
|
29
|
|
|
* @return string |
|
30
|
|
|
* the hashed string |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function hash($input) |
|
33
|
|
|
{ |
|
34
|
|
|
return PBKDF2::hash($input); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Compares a given hash with a clean text password by figuring out the |
|
39
|
|
|
* algorithm that has been used and then calling the appropriate sub-class |
|
40
|
|
|
* |
|
41
|
|
|
* @see cryptography.MD5#compare() |
|
42
|
|
|
* @see cryptography.SHA1#compare() |
|
43
|
|
|
* @see cryptography.PBKDF2#compare() |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $input |
|
46
|
|
|
* the cleartext password |
|
47
|
|
|
* @param string $hash |
|
48
|
|
|
* the hash the password should be checked against |
|
49
|
|
|
* @param boolean $isHash |
|
50
|
|
|
* @return boolean |
|
51
|
|
|
* the result of the comparison |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function compare($input, $hash, $isHash = false) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$version = substr($hash, 0, 8); |
|
56
|
|
|
|
|
57
|
|
|
if ($isHash === true) { |
|
58
|
|
|
return $input == $hash; |
|
59
|
|
|
} elseif ($version == 'PBKDF2v1') { // salted PBKDF2 |
|
60
|
|
|
return PBKDF2::compare($input, $hash); |
|
61
|
|
|
} elseif (strlen($hash) == 40) { // legacy, unsalted SHA1 |
|
62
|
|
|
return SHA1::compare($input, $hash); |
|
63
|
|
|
} elseif (strlen($hash) == 32) { // legacy, unsalted MD5 |
|
64
|
|
|
return MD5::compare($input, $hash); |
|
65
|
|
|
} else { // the hash provided doesn't make any sense |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if provided hash has been computed by most recent algorithm |
|
72
|
|
|
* returns true if otherwise |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $hash |
|
75
|
|
|
* the hash to be checked |
|
76
|
|
|
* @return boolean |
|
77
|
|
|
* whether the hash should be re-computed |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function requiresMigration($hash) |
|
80
|
|
|
{ |
|
81
|
|
|
$version = substr($hash, 0, 8); |
|
82
|
|
|
|
|
83
|
|
|
if ($version == PBKDF2::PREFIX) { // salted PBKDF2, let the responsible class decide |
|
84
|
|
|
return PBKDF2::requiresMigration($hash); |
|
85
|
|
|
} else { // everything else |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Generates a salt to be used in message digestation. |
|
92
|
|
|
* |
|
93
|
|
|
* @param integer $length |
|
94
|
|
|
* the length of the salt |
|
95
|
|
|
* @return string |
|
96
|
|
|
* a hexadecimal string |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function generateSalt($length) |
|
99
|
|
|
{ |
|
100
|
|
|
mt_srand(intval(microtime(true)*100000 + memory_get_usage(true))); |
|
101
|
|
|
return substr(sha1(uniqid(mt_rand(), true)), 0, $length); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|