1 | <?php |
||
34 | final class Hash extends Support |
||
35 | { |
||
36 | |||
37 | const ALGO = 'sha256'; |
||
38 | |||
39 | /** |
||
40 | * Internal function used to build the actual hash. |
||
41 | * |
||
42 | * @param string $input Data to hash |
||
43 | * @param string $password Password to use in HMAC call |
||
44 | * @param integer $cost Number of iterations to use |
||
45 | * @param string|null $salt Initialization vector to use in HMAC calls |
||
46 | * @return string |
||
47 | */ |
||
48 | 5 | private static function build($input, $password, $cost, $salt = null) |
|
49 | { |
||
50 | // Generate salt if needed |
||
51 | 5 | $salt = $salt === null ? Random::bytes(16) : $salt; |
|
52 | |||
53 | // Verify and normalize cost value |
||
54 | 5 | $cost = self::cost($cost); |
|
55 | |||
56 | // Create key to use for hmac operations |
||
57 | 5 | $key = \hash_hmac(self::ALGO, $salt, $password, true); |
|
58 | |||
59 | // Perform hash iterations. Get a 32 byte output value |
||
60 | 5 | $hash = self::ihmac($input, $key, $cost, self::ALGO); |
|
61 | |||
62 | // Return the salt + cost blob + hmac |
||
63 | 5 | return $salt . self::costHash($cost, $salt, $password) . $hash; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Return a normalized cost value. |
||
68 | * |
||
69 | * @param int $cost Number of iterations to use. |
||
70 | * |
||
71 | * @return int |
||
72 | */ |
||
73 | 5 | private static function cost($cost) |
|
77 | |||
78 | 5 | private static function costHash($cost, $salt, $password) |
|
88 | |||
89 | /** |
||
90 | * Perform a raw iterative HMAC operation with a configurable algo. |
||
91 | * |
||
92 | * This class always performs at least one hash to prevent the input from |
||
93 | * being passed back unchanged if bad parameters are set. |
||
94 | * |
||
95 | * @param string $data Data to hash. |
||
96 | * @param string $key Key to use to authenticate the hash. |
||
97 | * @param integer $iter Number of times to iteratate the hash |
||
98 | * @param string $algo Name of algo (sha256 or sha512 recommended) |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 16 | public static function ihmac($data, $key, $iter, $algo = 'sha256') |
|
116 | |||
117 | /** |
||
118 | * Hash an input string into a salted 512 byte hash. |
||
119 | * |
||
120 | * @param string $input Data to hash. |
||
121 | * @param string $password HMAC validation password. |
||
122 | * @param integer $cost Cost value of the hash. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public static function make($input, $password, $cost = 250000) |
|
130 | |||
131 | /** |
||
132 | * Check the validity of a hash. |
||
133 | * |
||
134 | * @param string $input Input to test. |
||
135 | * @param string $hash Known hash to validate against. |
||
136 | * @param string $password HMAC password to use during iterative hash. |
||
137 | * |
||
138 | * @return boolean |
||
139 | */ |
||
140 | 3 | public static function verify($input, $hash, $password) |
|
158 | |||
159 | } |
||
160 |