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

Read More

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?

Related posts

Leave a Reply

6 comments

  1. 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;
    
  2. 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.

  3.                 $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";
                    }
    
  4. 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";
    }
    
  5. 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