Yaffs site version 1.1
[yaffs-website] / vendor / symfony / polyfill-php55 / Php55.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Polyfill\Php55;
13
14 /**
15  * @internal
16  */
17 final class Php55
18 {
19     public static function boolval($val)
20     {
21         return (bool) $val;
22     }
23
24     public static function json_last_error_msg()
25     {
26         switch (json_last_error()) {
27             case JSON_ERROR_NONE: return 'No error';
28             case JSON_ERROR_DEPTH: return 'Maximum stack depth exceeded';
29             case JSON_ERROR_STATE_MISMATCH: return 'State mismatch (invalid or malformed JSON)';
30             case JSON_ERROR_CTRL_CHAR: return 'Control character error, possibly incorrectly encoded';
31             case JSON_ERROR_SYNTAX: return 'Syntax error';
32             case JSON_ERROR_UTF8: return 'Malformed UTF-8 characters, possibly incorrectly encoded';
33             default: return 'Unknown error';
34         }
35     }
36
37     /**
38      * @author Sebastiaan Stok <s.stok@rollerscapes.net>
39      */
40     public static function hash_pbkdf2($algorithm, $password, $salt, $iterations, $length = 0, $rawOutput = false)
41     {
42         // Number of blocks needed to create the derived key
43         $blocks = ceil($length / strlen(hash($algorithm, null, true)));
44         $digest = '';
45
46         for ($i = 1; $i <= $blocks; ++$i) {
47             $ib = $block = hash_hmac($algorithm, $salt.pack('N', $i), $password, true);
48
49             // Iterations
50             for ($j = 1; $j < $iterations; ++$j) {
51                 $ib ^= ($block = hash_hmac($algorithm, $block, $password, true));
52             }
53
54             $digest .= $ib;
55         }
56
57         if (!$rawOutput) {
58             $digest = bin2hex($digest);
59         }
60
61         return substr($digest, 0, $length);
62     }
63 }