3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2016 Spring Signage Ltd
5
* (PasswordStorage.php)
13
* @package Xibo\Helper
17
// These constants may be changed without breaking existing hashes.
18
const PBKDF2_HASH_ALGORITHM = 'sha256';
19
const PBKDF2_ITERATIONS = 1000;
20
const PBKDF2_SALT_BYTES = 24;
21
const PBKDF2_HASH_BYTES = 24;
23
// These constants define the encoding and may not be changed.
24
const HASH_SECTIONS = 4;
25
const HASH_ALGORITHM_INDEX = 0;
26
const HASH_ITERATION_INDEX = 1;
27
const HASH_SALT_INDEX = 2;
28
const HASH_PBKDF2_INDEX = 3;
31
* @param string $password
35
public static function verifyPassword($password, $hash)
37
$params = explode(':', $hash);
38
if (count($params) < self::HASH_SECTIONS)
39
throw new \InvalidArgumentException('Invalid password hash - not enough hash sections');
41
$pbkdf2 = base64_decode($params[self::HASH_PBKDF2_INDEX]);
43
// Check to see if the hash created from the provided password is the same as the hash we have stored already
44
return (self::slowEquals(
47
$params[self::HASH_ALGORITHM_INDEX],
49
$params[self::HASH_SALT_INDEX],
50
(int)$params[self::HASH_ITERATION_INDEX],
58
* Compares two strings $a and $b in length-constant time.
63
private static function slowEquals($a, $b)
65
$diff = strlen($a) ^ strlen($b);
66
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
68
$diff |= ord($a[$i]) ^ ord($b[$i]);
74
* PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
76
* Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
78
* This implementation of PBKDF2 was originally created by https://defuse.ca
79
* With improvements by http://www.variations-of-shadow.com
81
* @param string $algorithm The hash algorithm to use. Recommended: SHA256
82
* @param string $password The password.
83
* @param string $salt A salt that is unique to the password.
84
* @param int $count Iteration count. Higher is better, but slower. Recommended: At least 1000.
85
* @param int $key_length The length of the derived key in bytes.
86
* @param bool $raw_output If true, the key is returned in raw binary format. Hex encoded otherwise.
87
* @return string A $key_length-byte key derived from the password and salt.
89
public static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
91
$algorithm = strtolower($algorithm);
92
if (!in_array($algorithm, hash_algos(), true))
93
throw new \InvalidArgumentException('PBKDF2 ERROR: Invalid hash algorithm.');
94
if ($count <= 0 || $key_length <= 0)
95
throw new \InvalidArgumentException('PBKDF2 ERROR: Invalid parameters.');
97
$hash_length = strlen(hash($algorithm, "", true));
98
$block_count = ceil($key_length / $hash_length);
101
for ($i = 1; $i <= $block_count; $i++) {
102
// $i encoded as 4 bytes, big endian.
103
$last = $salt . pack("N", $i);
105
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
106
// perform the other $count - 1 iterations
107
for ($j = 1; $j < $count; $j++) {
108
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
114
return substr($output, 0, $key_length);
116
return bin2hex(substr($output, 0, $key_length));
b'\\ No newline at end of file'