Created
September 4, 2014 20:17
-
-
Save jtarleton/01ecc057abff874727cd to your computer and use it in GitHub Desktop.
Drupal-style password hashing routine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 = '[email protected]'; | |
$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