password-hash.sh

Same filename in other branches
  1. 7.x scripts/password-hash.sh
  2. 9 core/scripts/password-hash.sh
  3. 10 core/scripts/password-hash.sh
  4. 11.x core/scripts/password-hash.sh

Drupal hash script - to generate a hash from a plaintext password

@todo Port to a console command. https://www.drupal.org/node/2289409

File

core/scripts/password-hash.sh

View source
  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 <<
  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 <<
  29. Generate Drupal password hashes from the shell.
  30. Usage: {$script} [OPTIONS] ""
  31. Example: {$script} "mynewpassword"
  32. All arguments are long options.
  33. --help Print this page.
  34. "" ["" ["" ...]]
  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");

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.