password-hash.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file
  5. * Drupal hash script - to generate a hash from a plaintext password
  6. *
  7. * @param password1 [password2 [password3 ...]]
  8. * Plain-text passwords in quotes (or with spaces backslash escaped).
  9. */
  10. use Drupal\Core\DrupalKernel;
  11. use Symfony\Component\HttpFoundation\Request;
  12. if (PHP_SAPI !== 'cli') {
  13. return;
  14. }
  15. if (version_compare(PHP_VERSION, '5.4.5') < 0) {
  16. $version = PHP_VERSION;
  17. echo <<<EOF
  18. ERROR: This script requires at least PHP version 5.4.5. You invoked it with
  19. PHP version {$version}.
  20. \n
  21. EOF;
  22. exit;
  23. }
  24. $script = basename(array_shift($_SERVER['argv']));
  25. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  26. echo <<<EOF
  27. Generate Drupal password hashes from the shell.
  28. Usage: {$script} [OPTIONS] "<plan-text password>"
  29. Example: {$script} "mynewpassword"
  30. All arguments are long options.
  31. --help Print this page.
  32. "<password1>" ["<password2>" ["<password3>" ...]]
  33. One or more plan-text passwords enclosed by double quotes. The
  34. output hash may be manually entered into the
  35. {users_field_data}.pass field to change a password via SQL to a
  36. known value.
  37. EOF;
  38. exit;
  39. }
  40. // Password list to be processed.
  41. $passwords = $_SERVER['argv'];
  42. $autoloader = require __DIR__ . '/../../autoload.php';
  43. $request = Request::createFromGlobals();
  44. $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
  45. $kernel->boot();
  46. $password_hasher = $kernel->getContainer()->get('password');
  47. foreach ($passwords as $password) {
  48. print("\npassword: $password \t\thash: " . $password_hasher->hash($password) . "\n");
  49. }
  50. print("\n");