ManojGeek
ManojGeek

Reputation: 2001

Check WordPress hashed password with plain password

I am building a external application for which user login credentials will be taken from WordPress site database table 'users'

WordPress uses PHPass hashing , I am unable to validate username and password for my external application as the password in database table 'users' is hashed

I am trying to check plain password with hashed password using wp_check_password function but I am failing, nothing is written back with this code

<?php

$password = '965521425';
$hash = '$P$9jWFhEPMfI.KPByiNO9IyUzSTG7EZK0';

require_once('/home/nhtsoft/public_html/project/wp-includes/class-phpass.php');

function wp_check_password($password, $hash) {
    global $wp_hasher;

    if ( empty($wp_hasher) ) {
        $wp_hasher = new PasswordHash(8, true);
    }
    $check = $wp_hasher->CheckPassword($password, $hash);
    return apply_filters('check_password', $check, $password, $hash);
}    
?>

this code is giving me an empty page.

How to check this password so that I can use these WordPress credentials for external app login?

Upvotes: 6

Views: 15411

Answers (6)

Meisam
Meisam

Reputation: 408

what Bhumi Shah wrote is correct you should add

require_once($_SERVER['DOCUMENT_ROOT']."/wp-load.php");

to your code .

but hashed value for any password(number or text) is not one solid thing , it could be many things that's why they can be compared only with wp_check_password

Upvotes: 0

Suganya Rajasekar
Suganya Rajasekar

Reputation: 684

Try this...

I work's fine for me

require_once( ABSPATH . WPINC . '/class-phpass.php');
$wp_hasher = new PasswordHash(8, TRUE);
$plain_password     = trim($_POST['pass_current']); //user type password
$user               = get_user_by('id', get_current_user_id());
$password_hashed    =  $user->user_pass;

if($wp_hasher->CheckPassword($plain_password, $password_hashed)) {
     echo "YES, Matched";
}else{
    echo "No, Wrong Password";
}

Upvotes: 0

Prashant
Prashant

Reputation: 11

                $password_hashed = '$P$Bgf2Hpr5pOVOYAvQZUhUZeLIi/QuPr1';
                $plain_password = '123456';
                if ((wp_check_password($plain_password, $password_hashed)) == 1) {
                    echo "YES, Matched";
                } else {
                    echo "No, Wrong Password";
                }

Upvotes: 1

Stephan B
Stephan B

Reputation: 3701

In your code, you include the wp library and it looks like you redefine a function named wp_check_password but you do not call any function at all. Add the following line before the closing php tag ("?>") and try again.

echo (wp_check_password($password, $hash) ? 'TRUE' : 'FALSE');

Keep an eye on the error logs in case you miss some dependencies.

Upvotes: 2

Bhumi Shah
Bhumi Shah

Reputation: 9476

you have passed wrong hash value , hash value for 965521425 is $P$BmI5G.LOoEx1iH.naNqVhWnSh5sMp31 and you just need to write below code into your file:

require_once($_SERVER['DOCUMENT_ROOT']."/wp-load.php");
 $password = '965521425';
 $hash = '$P$BmI5G.LOoEx1iH.naNqVhWnSh5sMp31';
 var_dump(wp_check_password($password, $hash));

exit;

Upvotes: 6

user2092317
user2092317

Reputation: 3348

i would simply do this <?php wp_check_password( $password, $hash, $user_id ) ?> Refer

Upvotes: 1

Related Questions