bootstrap.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. require dirname(__FILE__) . '/RNCryptor/Autoloader.php';
  3. RNCryptor_Autoloader::register();
  4. if (!function_exists('hash_pbkdf2')) {
  5. /**
  6. * Based on pbkdf2() from https://defuse.ca/php-pbkdf2.htm. Made signature-compatible with hash_pbkdf2() in PHP5.5
  7. *
  8. * PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
  9. * $algorithm - The hash algorithm to use. Recommended: SHA256
  10. * $password - The password.
  11. * $salt - A salt that is unique to the password.
  12. * $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
  13. * $key_length - The length of the derived key in bytes.
  14. * $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
  15. * Returns: A $key_length-byte key derived from the password and salt.
  16. *
  17. * Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
  18. *
  19. * This implementation of PBKDF2 was originally created by https://defuse.ca
  20. * With improvements by http://www.variations-of-shadow.com
  21. */
  22. function hash_pbkdf2($algorithm, $password, $salt, $count, $key_length = 0, $raw_output = false)
  23. {
  24. $algorithm = strtolower($algorithm);
  25. if(!in_array($algorithm, hash_algos(), true))
  26. die('PBKDF2 ERROR: Invalid hash algorithm.');
  27. if($count <= 0 || $key_length <= 0)
  28. die('PBKDF2 ERROR: Invalid parameters.');
  29. $hash_length = strlen(hash($algorithm, "", true));
  30. $block_count = ceil($key_length / $hash_length);
  31. $output = "";
  32. for($i = 1; $i <= $block_count; $i++) {
  33. // $i encoded as 4 bytes, big endian.
  34. $last = $salt . pack("N", $i);
  35. // first iteration
  36. $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
  37. // perform the other $count - 1 iterations
  38. for ($j = 1; $j < $count; $j++) {
  39. $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
  40. }
  41. $output .= $xorsum;
  42. }
  43. if($raw_output)
  44. return substr($output, 0, $key_length);
  45. else
  46. return bin2hex(substr($output, 0, $key_length));
  47. }
  48. }