You are here

password-hash.sh in Drupal 8

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