password-hash.sh 1.7 KB

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