Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created September 4, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarleton/01ecc057abff874727cd to your computer and use it in GitHub Desktop.
Save jtarleton/01ecc057abff874727cd to your computer and use it in GitHub Desktop.
Drupal-style password hashing routine
<?php
/**
* A general purpose password hashing routine (from Drupal)
*
* Requires two standard drupal "include" files (available for download at https://www.drupal.org/):
* 1. drupal_bootstrap.inc
* 2. drupal_password.inc
*
* You might also want to use it to manually "reset" a password in a Drupal site's "user" MySQL table
*
* Assuming you have proper MySQL credentials for the Drupal user table, and want to reset user 0's password to "bosco"
*
* Simply replace the user's password hash with this one:
*
* UPDATE users SET pass='[result of script]' WHERE uid=0;
*/
require_once( dirname(__FILE__) . '/drupal_bootstrap.inc');
require_once( dirname(__FILE__) . '/drupal_password.inc' );
class DrupalUtils
{
private function __construct()
{
}
static public function hashForDrupal($password)
{
return user_hash_password($password);
}
}
$newuser = new stdClass();
$newuser->name = 'user@drupal.org';
$newuser->pass = DrupalUtils::hashForDrupal('bosco');
echo sprintf('Your hash : %s %s', $newuser->pass , PHP_EOL);
//echo sprintf('Drupal\'s Hash: %s %s', $drupalhash, PHP_EOL);
echo (user_check_password('bosco', $newuser))? 'Hash is OK!': 'Hash doesn\'t match drupal\'s!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment