Php55.php
2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Polyfill\Php55;
/**
* @internal
*/
final class Php55
{
public static function boolval($val)
{
return (bool) $val;
}
public static function json_last_error_msg()
{
switch (json_last_error()) {
case JSON_ERROR_NONE: return 'No error';
case JSON_ERROR_DEPTH: return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH: return 'State mismatch (invalid or malformed JSON)';
case JSON_ERROR_CTRL_CHAR: return 'Control character error, possibly incorrectly encoded';
case JSON_ERROR_SYNTAX: return 'Syntax error';
case JSON_ERROR_UTF8: return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default: return 'Unknown error';
}
}
/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
* @author Scott <scott@paragonie.com>
*/
public static function hash_pbkdf2($algorithm, $password, $salt, $iterations, $length = 0, $rawOutput = false)
{
// Pre-hash for optimization if password length > hash length
$hashLength = \strlen(hash($algorithm, '', true));
switch ($algorithm) {
case 'sha224':
case 'sha256':
$blockSize = 64;
break;
case 'sha384':
case 'sha512':
$blockSize = 128;
break;
default:
$blockSize = $hashLength;
break;
}
if ($length < 1) {
$length = $hashLength;
if (!$rawOutput) {
$length <<= 1;
}
}
// Number of blocks needed to create the derived key
$blocks = ceil($length / $hashLength);
$digest = '';
if (\strlen($password) > $blockSize) {
$password = hash($algorithm, $password, true);
}
for ($i = 1; $i <= $blocks; ++$i) {
$ib = $block = hash_hmac($algorithm, $salt.pack('N', $i), $password, true);
// Iterations
for ($j = 1; $j < $iterations; ++$j) {
$ib ^= ($block = hash_hmac($algorithm, $block, $password, true));
}
$digest .= $ib;
}
if (!$rawOutput) {
$digest = bin2hex($digest);
}
return substr($digest, 0, $length);
}
}