password-hash.sh

#!/usr/bin/php
<?php

/**
 * Drupal hash script - to generate a hash from a plaintext password
 *
 * Check for your PHP interpreter - on Windows you'll probably have to
 * replace line 1 with
 *   #!c:/program files/php/php.exe
 *
 * @param password1 [password2 [password3 ...]]
 *  Plain-text passwords in quotes (or with spaces backslash escaped).
 */

if (version_compare(PHP_VERSION, "5.2.0", "<")) {
  $version  = PHP_VERSION;
  echo <<<EOF

ERROR: This script requires at least PHP version 5.2.0. You invoked it with
       PHP version {$version}.
\n
EOF;
  exit;
}

$script = basename(array_shift($_SERVER['argv']));

if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  echo <<<EOF

Generate Drupal password hashes from the shell.

Usage:        {$script} [OPTIONS] "<plan-text password>"
Example:      {$script} "mynewpassword"

All arguments are long options.

  --help      Print this page.

  --root <path>

              Set the working directory for the script to the specified path.
              To execute this script this has to be the root directory of your
              Drupal installation, e.g. /home/www/foo/drupal (assuming Drupal
              running on Unix). Use surrounding quotation marks on Windows.

  "<password1>" ["<password2>" ["<password3>" ...]]

              One or more plan-text passwords enclosed by double quotes. The
              output hash may be manually entered into the {users}.pass field to
              change a password via SQL to a known value.

To run this script without the --root argument invoke it from the root directory
of your Drupal installation as

  ./scripts/{$script}
\n
EOF;
  exit;
}

$passwords = array();

// Parse invocation arguments.
while ($param = array_shift($_SERVER['argv'])) {
  switch ($param) {
    case '--root':
      // Change the working directory.
      $path = array_shift($_SERVER['argv']);
      if (is_dir($path)) {
        chdir($path);
      }
      break;
    default:
      // Add a password to the list to be processed.
      $passwords[] = $param;
      break;
  }
}

chdir('..');
define('DRUPAL_ROOT', getcwd());

include_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';

$password_hasher = drupal_container()->get('password');

foreach ($passwords as $password) {
  print("\npassword: $password \t\thash: ". $password_hasher->hash($password) ."\n");
}
print("\n");

File

drupal/core/scripts/password-hash.sh
View source
  1. #!/usr/bin/php
  2. /**
  3. * Drupal hash script - to generate a hash from a plaintext password
  4. *
  5. * Check for your PHP interpreter - on Windows you'll probably have to
  6. * replace line 1 with
  7. * #!c:/program files/php/php.exe
  8. *
  9. * @param password1 [password2 [password3 ...]]
  10. * Plain-text passwords in quotes (or with spaces backslash escaped).
  11. */
  12. if (version_compare(PHP_VERSION, "5.2.0", "<")) {
  13. $version = PHP_VERSION;
  14. echo <<
  15. ERROR: This script requires at least PHP version 5.2.0. You invoked it with
  16. PHP version {$version}.
  17. \n
  18. EOF;
  19. exit;
  20. }
  21. $script = basename(array_shift($_SERVER['argv']));
  22. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  23. echo <<
  24. Generate Drupal password hashes from the shell.
  25. Usage: {$script} [OPTIONS] ""
  26. Example: {$script} "mynewpassword"
  27. All arguments are long options.
  28. --help Print this page.
  29. --root
  30. Set the working directory for the script to the specified path.
  31. To execute this script this has to be the root directory of your
  32. Drupal installation, e.g. /home/www/foo/drupal (assuming Drupal
  33. running on Unix). Use surrounding quotation marks on Windows.
  34. "" ["" ["" ...]]
  35. One or more plan-text passwords enclosed by double quotes. The
  36. output hash may be manually entered into the {users}.pass field to
  37. change a password via SQL to a known value.
  38. To run this script without the --root argument invoke it from the root directory
  39. of your Drupal installation as
  40. ./scripts/{$script}
  41. \n
  42. EOF;
  43. exit;
  44. }
  45. $passwords = array();
  46. // Parse invocation arguments.
  47. while ($param = array_shift($_SERVER['argv'])) {
  48. switch ($param) {
  49. case '--root':
  50. // Change the working directory.
  51. $path = array_shift($_SERVER['argv']);
  52. if (is_dir($path)) {
  53. chdir($path);
  54. }
  55. break;
  56. default:
  57. // Add a password to the list to be processed.
  58. $passwords[] = $param;
  59. break;
  60. }
  61. }
  62. chdir('..');
  63. define('DRUPAL_ROOT', getcwd());
  64. include_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
  65. $password_hasher = drupal_container()->get('password');
  66. foreach ($passwords as $password) {
  67. print("\npassword: $password \t\thash: ". $password_hasher->hash($password) ."\n");
  68. }
  69. print("\n");