PHP 7
Password-hash.sh Guest on 20th November 2020 05:21:48 AM
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /**
  5.  * @file
  6.  * Drupal hash script - to generate a hash from a plaintext password
  7.  *
  8.  * @param password1 [password2 [password3 ...]]
  9.  *  Plain-text passwords in quotes (or with spaces backslash escaped).
  10.  */
  11.  
  12. use Drupal\Core\DrupalKernel;
  13. use Symfony\Component\HttpFoundation\Request;
  14.  
  15. if (PHP_SAPI !== 'cli') {
  16.   return;
  17. }
  18.  
  19. if (version_compare(PHP_VERSION, '5.4.5') < 0) {
  20.   $version  = PHP_VERSION;
  21.   echo <<<EOF
  22.  
  23. ERROR: This script requires at least PHP version 5.4.5. You invoked it with
  24.        PHP version {$version}.
  25. \n
  26. EOF;
  27.   exit;
  28. }
  29.  
  30. $script = basename(array_shift($_SERVER['argv']));
  31.  
  32. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  33.   echo <<<EOF
  34.  
  35. Generate Drupal password hashes from the shell.
  36.  
  37. Usage:        {$script} [OPTIONS] "<plan-text password>"
  38. Example:      {$script} "mynewpassword"
  39.  
  40. All arguments are long options.
  41.  
  42.   --help      Print this page.
  43.  
  44.   "<password1>" ["<password2>" ["<password3>" ...]]
  45.  
  46.               One or more plan-text passwords enclosed by double quotes. The
  47.               output hash may be manually entered into the
  48.               {users_field_data}.pass field to change a password via SQL to a
  49.               known value.
  50.  
  51.  
  52. EOF;
  53.   exit;
  54. }
  55.  
  56. // Password list to be processed.
  57. $passwords = $_SERVER['argv'];
  58.  
  59. $autoloader = require __DIR__ . '/../../autoload.php';
  60.  
  61. $request = Request::createFromGlobals();
  62. $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
  63. $kernel->boot();
  64.  
  65. $password_hasher = $kernel->getContainer()->get('password');
  66.  
  67. foreach ($passwords as $password) {
  68.   print("\npassword: $password \t\thash: " . $password_hasher->hash($password) . "\n");
  69. }
  70. print("\n");
  71.  
  72. ?>

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.