password-hash.sh

Same filename in other branches
  1. 7.x scripts/password-hash.sh
  2. 9 core/scripts/password-hash.sh
  3. 8.9.x core/scripts/password-hash.sh
  4. 10 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. $script = basename(array_shift($_SERVER['argv']));
  18. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  19. echo <<
  20. Generate Drupal password hashes from the shell.
  21. Usage: {$script} [OPTIONS] ""
  22. Example: {$script} "my-new-password"
  23. All arguments are long options.
  24. --help Print this page.
  25. "" ["" ["" ...]]
  26. One or more plaintext passwords enclosed by double quotes. The
  27. output hash may be manually entered into the
  28. {users_field_data}.pass field to change a password via SQL to a
  29. known value.
  30. EOF;
  31. exit;
  32. }
  33. // Password list to be processed.
  34. $passwords = $_SERVER['argv'];
  35. $autoloader = require __DIR__ . '/../../autoload.php';
  36. $request = Request::createFromGlobals();
  37. $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
  38. $kernel->boot();
  39. $password_hasher = $kernel->getContainer()->get('password');
  40. foreach ($passwords as $password) {
  41. print("\npassword: $password \t\thash: " . $password_hasher->hash($password) . "\n");
  42. }
  43. print("\n");

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