Php55.php 1.8 KB

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